Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
DownloadEndpointTrait
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 getEntityData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 updateEntityWithData
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 persistUploads
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 editUploads
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEntityById
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Olz\Service\Endpoints;
4
5use Olz\Entity\Service\Download;
6use Olz\Utils\WithUtilsTrait;
7use PhpTypeScriptApi\HttpError;
8
9/**
10 * @phpstan-type OlzDownloadId int
11 * @phpstan-type OlzDownloadData array{
12 *   name: non-empty-string,
13 *   position?: ?int,
14 *   fileId?: ?non-empty-string,
15 * }
16 */
17trait DownloadEndpointTrait {
18    use WithUtilsTrait;
19
20    /** @return OlzDownloadData */
21    public function getEntityData(Download $entity): array {
22        $file_ids = $entity->getStoredFileUploadIds();
23        return [
24            'name' => $entity->getName() ?: '-',
25            'position' => $entity->getPosition(),
26            'fileId' => $file_ids[0] ?? null,
27        ];
28    }
29
30    /** @param OlzDownloadData $input_data */
31    public function updateEntityWithData(Download $entity, array $input_data): void {
32        $entity->setName($input_data['name']);
33        $entity->setPosition($input_data['position'] ?? 0);
34    }
35
36    /** @param OlzDownloadData $input_data */
37    public function persistUploads(Download $entity, array $input_data): void {
38        $file_id = $input_data['fileId'] ?? null;
39        if ($file_id) {
40            $this->persistOlzFiles($entity, [$file_id]);
41        }
42    }
43
44    public function editUploads(Download $entity): void {
45        $this->editOlzFiles($entity);
46    }
47
48    protected function getEntityById(int $id): Download {
49        $repo = $this->entityManager()->getRepository(Download::class);
50        $entity = $repo->findOneBy(['id' => $id]);
51        if (!$entity) {
52            throw new HttpError(404, "Nicht gefunden.");
53        }
54        return $entity;
55    }
56}