Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
TerminLabelEndpointTrait
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 getEntityData
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 updateEntityWithData
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 persistUploads
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 editUploads
100.00% covered (success)
100.00%
3 / 3
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
 listEntities
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Olz\Termine\Endpoints;
4
5use Olz\Entity\Termine\TerminLabel;
6use Olz\Utils\WithUtilsTrait;
7use PhpTypeScriptApi\HttpError;
8
9/**
10 * @phpstan-type OlzTerminLabelId int
11 * @phpstan-type OlzTerminLabelData array{
12 *   ident: non-empty-string,
13 *   name: non-empty-string,
14 *   details: string,
15 *   icon?: ?non-empty-string,
16 *   position?: ?int,
17 *   imageIds: array<non-empty-string>,
18 *   fileIds: array<non-empty-string>,
19 * }
20 */
21trait TerminLabelEndpointTrait {
22    use WithUtilsTrait;
23
24    /** @return OlzTerminLabelData */
25    public function getEntityData(TerminLabel $entity): array {
26        return [
27            'ident' => $entity->getIdent() ? $entity->getIdent() : '-',
28            'name' => $entity->getName() ? $entity->getName() : '-',
29            'details' => $entity->getDetails() ?? '',
30            'icon' => $entity->getIcon() ? $entity->getIcon() : null,
31            'position' => $entity->getPosition(),
32            'imageIds' => $entity->getStoredImageUploadIds(),
33            'fileIds' => $entity->getStoredFileUploadIds(),
34        ];
35    }
36
37    /** @param OlzTerminLabelData $input_data */
38    public function updateEntityWithData(TerminLabel $entity, array $input_data): void {
39        $valid_icon_file_id = $this->uploadUtils()->getValidUploadId($input_data['icon'] ?? null);
40
41        $entity->setIdent($input_data['ident']);
42        $entity->setName($input_data['name']);
43        $entity->setDetails($input_data['details']);
44        $entity->setIcon($valid_icon_file_id);
45        $entity->setPosition($input_data['position'] ?? 0);
46    }
47
48    /** @param OlzTerminLabelData $input_data */
49    public function persistUploads(TerminLabel $entity, array $input_data): void {
50        $this->persistOlzImages($entity, $input_data['imageIds']);
51        $this->persistOlzFiles($entity, $input_data['fileIds']);
52        $icon = $input_data['icon'] ?? null;
53        if ($icon) {
54            $this->persistOlzFiles($entity, [$icon]);
55        }
56    }
57
58    public function editUploads(TerminLabel $entity): void {
59        $image_ids = $this->uploadUtils()->getStoredUploadIds("{$entity->getImagesPathForStorage()}img/");
60        $this->editOlzImages($entity, $image_ids);
61        $this->editOlzFiles($entity);
62    }
63
64    protected function getEntityById(int $id): TerminLabel {
65        $repo = $this->entityManager()->getRepository(TerminLabel::class);
66        $entity = $repo->findOneBy(['id' => $id]);
67        if (!$entity) {
68            throw new HttpError(404, "Nicht gefunden.");
69        }
70        return $entity;
71    }
72
73    /** @return array<TerminLabel> */
74    protected function listEntities(): array {
75        $repo = $this->entityManager()->getRepository(TerminLabel::class);
76        return $repo->findBy([], ['position' => 'ASC']);
77    }
78}