Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| DownloadEndpointTrait | |
0.00% |
0 / 17 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| getEntityData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| updateEntityWithData | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| persistUploads | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| editUploads | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Service\Endpoints; |
| 4 | |
| 5 | use Olz\Entity\Service\Download; |
| 6 | use Olz\Utils\WithUtilsTrait; |
| 7 | use PhpTypeScriptApi\HttpError; |
| 8 | |
| 9 | /** |
| 10 | * @phpstan-type OlzDownloadId int |
| 11 | * @phpstan-type OlzDownloadData array{ |
| 12 | * name: non-empty-string, |
| 13 | * position?: ?float, |
| 14 | * fileId?: ?non-empty-string, |
| 15 | * } |
| 16 | */ |
| 17 | trait 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 | } |