Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
TerminEndpointTrait | |
0.00% |
0 / 72 |
|
0.00% |
0 / 6 |
272 | |
0.00% |
0 / 1 |
getEntityData | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
20 | |||
updateEntityWithData | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
30 | |||
persistUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
editUploads | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getEntityById | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
getTypesForApi | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace Olz\Termine\Endpoints; |
4 | |
5 | use Olz\Entity\Termine\Termin; |
6 | use Olz\Entity\Termine\TerminLabel; |
7 | use Olz\Entity\Termine\TerminLocation; |
8 | use Olz\Entity\Termine\TerminTemplate; |
9 | use Olz\Utils\WithUtilsTrait; |
10 | use PhpTypeScriptApi\HttpError; |
11 | use PhpTypeScriptApi\PhpStan\IsoDate; |
12 | use PhpTypeScriptApi\PhpStan\IsoDateTime; |
13 | use PhpTypeScriptApi\PhpStan\IsoTime; |
14 | |
15 | /** |
16 | * @phpstan-type OlzTerminId int |
17 | * @phpstan-type OlzTerminData array{ |
18 | * fromTemplateId?: ?int, |
19 | * startDate?: ?IsoDate, |
20 | * startTime?: ?IsoTime, |
21 | * endDate?: ?IsoDate, |
22 | * endTime?: ?IsoTime, |
23 | * title?: ?non-empty-string, |
24 | * text: string, |
25 | * deadline?: ?IsoDateTime, |
26 | * shouldPromote: bool, |
27 | * newsletter: bool, |
28 | * solvId?: ?int, |
29 | * go2olId?: ?non-empty-string, |
30 | * types: array<non-empty-string>, |
31 | * locationId?: ?int, |
32 | * coordinateX?: ?int, |
33 | * coordinateY?: ?int, |
34 | * imageIds: array<non-empty-string>, |
35 | * fileIds: array<non-empty-string>, |
36 | * } |
37 | */ |
38 | trait TerminEndpointTrait { |
39 | use WithUtilsTrait; |
40 | |
41 | /** @return OlzTerminData */ |
42 | public function getEntityData(Termin $entity): array { |
43 | $types_for_api = $this->getTypesForApi($entity->getLabels()); |
44 | |
45 | $valid_image_ids = $this->uploadUtils()->getValidUploadIds($entity->getImageIds()); |
46 | $file_ids = $entity->getStoredFileUploadIds(); |
47 | |
48 | return [ |
49 | 'fromTemplateId' => $entity->getFromTemplate()?->getId(), |
50 | 'startDate' => IsoDate::fromDateTime($entity->getStartDate()), |
51 | 'startTime' => IsoTime::fromDateTime($entity->getStartTime()), |
52 | 'endDate' => IsoDate::fromDateTime($entity->getEndDate()), |
53 | 'endTime' => IsoTime::fromDateTime($entity->getEndTime()), |
54 | 'title' => $entity->getTitle() ?: '-', |
55 | 'text' => $entity->getText() ?? '', |
56 | 'deadline' => IsoDateTime::fromDateTime($entity->getDeadline()), |
57 | 'shouldPromote' => $entity->getShouldPromote(), |
58 | 'newsletter' => $entity->getNewsletter(), |
59 | 'solvId' => $entity->getSolvId() ?: null, |
60 | 'go2olId' => $entity->getGo2olId() ?: null, |
61 | 'types' => $types_for_api, |
62 | 'locationId' => $entity->getLocation()?->getId(), |
63 | 'coordinateX' => $entity->getCoordinateX(), |
64 | 'coordinateY' => $entity->getCoordinateY(), |
65 | 'imageIds' => $valid_image_ids, |
66 | 'fileIds' => $file_ids, |
67 | ]; |
68 | } |
69 | |
70 | /** @param OlzTerminData $input_data */ |
71 | public function updateEntityWithData(Termin $entity, array $input_data): void { |
72 | $valid_image_ids = $this->uploadUtils()->getValidUploadIds($input_data['imageIds']); |
73 | $termin_template_repo = $this->entityManager()->getRepository(TerminTemplate::class); |
74 | $from_template_id = $input_data['fromTemplateId'] ?? null; |
75 | $termin_template = $termin_template_repo->findOneBy(['id' => $from_template_id]); |
76 | $termin_label_repo = $this->entityManager()->getRepository(TerminLabel::class); |
77 | $termin_location_repo = $this->entityManager()->getRepository(TerminLocation::class); |
78 | $location_id = $input_data['locationId'] ?? null; |
79 | $termin_location = $termin_location_repo->findOneBy(['id' => $location_id]); |
80 | |
81 | $entity->setFromTemplate($termin_template); |
82 | $entity->setStartDate($input_data['startDate'] ?? new \DateTime()); |
83 | $entity->setStartTime($input_data['startTime'] ?? null); |
84 | $entity->setEndDate($input_data['endDate'] ?? null); |
85 | $entity->setEndTime($input_data['endTime'] ?? null); |
86 | $entity->setTitle($input_data['title'] ?? null); |
87 | $entity->setText($input_data['text']); |
88 | $entity->setDeadline($input_data['deadline'] ?? null); |
89 | if (count($valid_image_ids) > 0) { |
90 | $entity->setShouldPromote($input_data['shouldPromote']); |
91 | } else { |
92 | $entity->setShouldPromote(false); |
93 | } |
94 | $entity->setNewsletter($input_data['newsletter']); |
95 | $entity->setSolvId($input_data['solvId'] ?? null); |
96 | $entity->setGo2olId($input_data['go2olId'] ?? null); |
97 | $entity->clearLabels(); |
98 | foreach ($input_data['types'] as $ident) { |
99 | $termin_label = $termin_label_repo->findOneBy(['ident' => $ident]); |
100 | if (!$termin_label) { |
101 | throw new HttpError(400, "No such TerminLabel: {$ident}"); |
102 | } |
103 | $entity->addLabel($termin_label); |
104 | } |
105 | $entity->setLocation($termin_location); |
106 | $entity->setCoordinateX($input_data['coordinateX'] ?? null); |
107 | $entity->setCoordinateY($input_data['coordinateY'] ?? null); |
108 | $entity->setImageIds($valid_image_ids); |
109 | |
110 | if ($entity->getSolvId() !== null) { |
111 | $this->termineUtils()->updateTerminFromSolvEvent($entity); |
112 | } |
113 | } |
114 | |
115 | /** @param OlzTerminData $input_data */ |
116 | public function persistUploads(Termin $entity, array $input_data): void { |
117 | $this->persistOlzImages($entity, $entity->getImageIds()); |
118 | $this->persistOlzFiles($entity, $input_data['fileIds']); |
119 | } |
120 | |
121 | public function editUploads(Termin $entity): void { |
122 | $this->editOlzImages($entity, $entity->getImageIds()); |
123 | $this->editOlzFiles($entity); |
124 | } |
125 | |
126 | protected function getEntityById(int $id): Termin { |
127 | $repo = $this->entityManager()->getRepository(Termin::class); |
128 | $entity = $repo->findOneBy(['id' => $id]); |
129 | if (!$entity) { |
130 | throw new HttpError(404, "Nicht gefunden."); |
131 | } |
132 | return $entity; |
133 | } |
134 | |
135 | // --- |
136 | |
137 | /** |
138 | * @param ?iterable<TerminLabel> $labels |
139 | * |
140 | * @return array<non-empty-string> |
141 | */ |
142 | protected function getTypesForApi(?iterable $labels): array { |
143 | $types_for_api = []; |
144 | foreach ($labels ?? [] as $label) { |
145 | $ident = $label->getIdent(); |
146 | if ($ident) { |
147 | $types_for_api[] = $ident; |
148 | } |
149 | } |
150 | return $types_for_api; |
151 | } |
152 | } |