1 | <?php |
||
2 | /** |
||
3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
4 | * |
||
5 | * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU Affero General Public License as published |
||
9 | * by the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | * GNU Affero General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU Affero General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | */ |
||
20 | |||
21 | declare(strict_types=1); |
||
22 | |||
23 | namespace App\Services; |
||
24 | |||
25 | use App\Entity\Attachments\Attachment; |
||
26 | use App\Entity\Attachments\AttachmentType; |
||
27 | use App\Entity\Attachments\PartAttachment; |
||
28 | use App\Entity\Base\AbstractDBElement; |
||
29 | use App\Entity\ProjectSystem\Project; |
||
30 | use App\Entity\LabelSystem\LabelProfile; |
||
31 | use App\Entity\Parts\Category; |
||
32 | use App\Entity\Parts\Footprint; |
||
33 | use App\Entity\Parts\Manufacturer; |
||
34 | use App\Entity\Parts\MeasurementUnit; |
||
35 | use App\Entity\Parts\Part; |
||
36 | use App\Entity\Parts\PartLot; |
||
37 | use App\Entity\Parts\Storelocation; |
||
38 | use App\Entity\Parts\Supplier; |
||
39 | use App\Entity\PriceInformations\Currency; |
||
40 | use App\Entity\PriceInformations\Orderdetail; |
||
41 | use App\Entity\PriceInformations\Pricedetail; |
||
42 | use App\Entity\UserSystem\Group; |
||
43 | use App\Entity\UserSystem\User; |
||
44 | use App\Exceptions\EntityNotSupportedException; |
||
45 | use App\Services\Attachments\AttachmentURLGenerator; |
||
46 | use DateTime; |
||
47 | use function array_key_exists; |
||
48 | use function get_class; |
||
49 | use InvalidArgumentException; |
||
50 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
51 | |||
52 | /** |
||
53 | * This service can be used to generate links to controllers for different aspects of an entity |
||
54 | * (like info, edit, delete, etc.) |
||
55 | * Useful for Twig, where you can generate a link to an entity using a filter. |
||
56 | */ |
||
57 | class EntityURLGenerator |
||
58 | { |
||
59 | protected UrlGeneratorInterface $urlGenerator; |
||
60 | protected AttachmentURLGenerator $attachmentURLGenerator; |
||
61 | |||
62 | public function __construct(UrlGeneratorInterface $urlGenerator, AttachmentURLGenerator $attachmentURLGenerator) |
||
63 | { |
||
64 | $this->urlGenerator = $urlGenerator; |
||
65 | $this->attachmentURLGenerator = $attachmentURLGenerator; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Generates an URL to the page using the given page type and element. |
||
70 | * For the given types, the [type]URL() functions are called (e.g. infoURL()). |
||
71 | * Not all entity class and $type combinations are supported. |
||
72 | * |
||
73 | * @param mixed $entity The element for which the page should be generated |
||
74 | * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
||
75 | * |
||
76 | * @return string the link to the desired page |
||
77 | * |
||
78 | * @throws EntityNotSupportedException thrown if the entity is not supported for the given type |
||
79 | * @throws InvalidArgumentException thrown if the givent type is not existing |
||
80 | */ |
||
81 | public function getURL($entity, string $type): string |
||
82 | { |
||
83 | switch ($type) { |
||
84 | case 'info': |
||
85 | return $this->infoURL($entity); |
||
86 | case 'edit': |
||
87 | return $this->editURL($entity); |
||
88 | case 'create': |
||
89 | return $this->createURL($entity); |
||
90 | case 'clone': |
||
91 | return $this->cloneURL($entity); |
||
92 | case 'list': |
||
93 | case 'list_parts': |
||
94 | return $this->listPartsURL($entity); |
||
95 | case 'delete': |
||
96 | return $this->deleteURL($entity); |
||
97 | case 'file_download': |
||
98 | return $this->downloadURL($entity); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
99 | case 'file_view': |
||
100 | return $this->viewURL($entity); |
||
0 ignored issues
–
show
|
|||
101 | } |
||
102 | |||
103 | throw new InvalidArgumentException('Method is not supported!'); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Gets the URL to view the given element at a given timestamp. |
||
108 | */ |
||
109 | public function timeTravelURL(AbstractDBElement $entity, DateTime $dateTime): string |
||
110 | { |
||
111 | $map = [ |
||
112 | Part::class => 'part_info', |
||
113 | //As long we does not have own things for it use edit page |
||
114 | AttachmentType::class => 'attachment_type_edit', |
||
115 | Category::class => 'category_edit', |
||
116 | Project::class => 'project_edit', |
||
117 | Supplier::class => 'supplier_edit', |
||
118 | Manufacturer::class => 'manufacturer_edit', |
||
119 | Storelocation::class => 'store_location_edit', |
||
120 | Footprint::class => 'footprint_edit', |
||
121 | User::class => 'user_edit', |
||
122 | Currency::class => 'currency_edit', |
||
123 | MeasurementUnit::class => 'measurement_unit_edit', |
||
124 | Group::class => 'group_edit', |
||
125 | LabelProfile::class => 'label_profile_edit', |
||
126 | ]; |
||
127 | |||
128 | try { |
||
129 | return $this->urlGenerator->generate( |
||
130 | $this->mapToController($map, $entity), |
||
131 | [ |
||
132 | 'id' => $entity->getID(), |
||
133 | 'timestamp' => $dateTime->getTimestamp(), |
||
134 | ] |
||
135 | ); |
||
136 | } catch (EntityNotSupportedException $exception) { |
||
137 | if ($entity instanceof PartLot) { |
||
138 | return $this->urlGenerator->generate('part_info', [ |
||
139 | 'id' => $entity->getPart()->getID(), |
||
140 | 'timestamp' => $dateTime->getTimestamp(), |
||
141 | ]); |
||
142 | } |
||
143 | if ($entity instanceof PartAttachment) { |
||
144 | return $this->urlGenerator->generate('part_info', [ |
||
145 | 'id' => $entity->getElement()->getID(), |
||
146 | 'timestamp' => $dateTime->getTimestamp(), |
||
147 | ]); |
||
148 | } |
||
149 | if ($entity instanceof Orderdetail) { |
||
150 | return $this->urlGenerator->generate('part_info', [ |
||
151 | 'id' => $entity->getPart()->getID(), |
||
152 | 'timestamp' => $dateTime->getTimestamp(), |
||
153 | ]); |
||
154 | } |
||
155 | if ($entity instanceof Pricedetail) { |
||
156 | return $this->urlGenerator->generate('part_info', [ |
||
157 | 'id' => $entity->getOrderdetail()->getPart()->getID(), |
||
158 | 'timestamp' => $dateTime->getTimestamp(), |
||
159 | ]); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | //Otherwise throw an error |
||
164 | throw new EntityNotSupportedException('The given entity is not supported yet!'); |
||
165 | } |
||
166 | |||
167 | public function viewURL(Attachment $entity): ?string |
||
168 | { |
||
169 | if ($entity->isExternal()) { //For external attachments, return the link to external path |
||
170 | return $entity->getURL(); |
||
171 | } |
||
172 | //return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]); |
||
173 | return $this->attachmentURLGenerator->getViewURL($entity); |
||
174 | } |
||
175 | |||
176 | public function downloadURL($entity): ?string |
||
177 | { |
||
178 | if ($entity instanceof Attachment) { |
||
179 | if ($entity->isExternal()) { //For external attachments, return the link to external path |
||
180 | return $entity->getURL(); |
||
181 | } |
||
182 | |||
183 | return $this->attachmentURLGenerator->getDownloadURL($entity); |
||
184 | } |
||
185 | |||
186 | //Otherwise throw an error |
||
187 | throw new EntityNotSupportedException(sprintf('The given entity is not supported yet! Passed class type: %s', get_class($entity))); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Generates an URL to a page, where info about this entity can be viewed. |
||
192 | * |
||
193 | * @param AbstractDBElement $entity The entity for which the info should be generated |
||
194 | * |
||
195 | * @return string The URL to the info page |
||
196 | * |
||
197 | * @throws EntityNotSupportedException If the method is not supported for the given Entity |
||
198 | */ |
||
199 | public function infoURL(AbstractDBElement $entity): string |
||
200 | { |
||
201 | $map = [ |
||
202 | Part::class => 'part_info', |
||
203 | |||
204 | //As long we does not have own things for it use edit page |
||
205 | AttachmentType::class => 'attachment_type_edit', |
||
206 | Category::class => 'category_edit', |
||
207 | Project::class => 'project_info', |
||
208 | Supplier::class => 'supplier_edit', |
||
209 | Manufacturer::class => 'manufacturer_edit', |
||
210 | Storelocation::class => 'store_location_edit', |
||
211 | Footprint::class => 'footprint_edit', |
||
212 | User::class => 'user_edit', |
||
213 | Currency::class => 'currency_edit', |
||
214 | MeasurementUnit::class => 'measurement_unit_edit', |
||
215 | Group::class => 'group_edit', |
||
216 | LabelProfile::class => 'label_profile_edit', |
||
217 | ]; |
||
218 | |||
219 | return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Generates an URL to a page, where this entity can be edited. |
||
224 | * |
||
225 | * @param mixed $entity The entity for which the edit link should be generated |
||
226 | * |
||
227 | * @return string the URL to the edit page |
||
228 | * |
||
229 | * @throws EntityNotSupportedException If the method is not supported for the given Entity |
||
230 | */ |
||
231 | public function editURL($entity): string |
||
232 | { |
||
233 | $map = [ |
||
234 | Part::class => 'part_edit', |
||
235 | AttachmentType::class => 'attachment_type_edit', |
||
236 | Category::class => 'category_edit', |
||
237 | Project::class => 'project_edit', |
||
238 | Supplier::class => 'supplier_edit', |
||
239 | Manufacturer::class => 'manufacturer_edit', |
||
240 | Storelocation::class => 'store_location_edit', |
||
241 | Footprint::class => 'footprint_edit', |
||
242 | User::class => 'user_edit', |
||
243 | Currency::class => 'currency_edit', |
||
244 | MeasurementUnit::class => 'measurement_unit_edit', |
||
245 | Group::class => 'group_edit', |
||
246 | LabelProfile::class => 'label_profile_edit', |
||
247 | ]; |
||
248 | |||
249 | return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Generates an URL to a page, where a entity of this type can be created. |
||
254 | * |
||
255 | * @param mixed $entity The entity for which the link should be generated |
||
256 | * |
||
257 | * @return string the URL to the page |
||
258 | * |
||
259 | * @throws EntityNotSupportedException If the method is not supported for the given Entity |
||
260 | */ |
||
261 | public function createURL($entity): string |
||
262 | { |
||
263 | $map = [ |
||
264 | Part::class => 'part_new', |
||
265 | AttachmentType::class => 'attachment_type_new', |
||
266 | Category::class => 'category_new', |
||
267 | Project::class => 'project_new', |
||
268 | Supplier::class => 'supplier_new', |
||
269 | Manufacturer::class => 'manufacturer_new', |
||
270 | Storelocation::class => 'store_location_new', |
||
271 | Footprint::class => 'footprint_new', |
||
272 | User::class => 'user_new', |
||
273 | Currency::class => 'currency_new', |
||
274 | MeasurementUnit::class => 'measurement_unit_new', |
||
275 | Group::class => 'group_new', |
||
276 | LabelProfile::class => 'label_profile_new', |
||
277 | ]; |
||
278 | |||
279 | return $this->urlGenerator->generate($this->mapToController($map, $entity)); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Generates an URL to a page, where a new entity can be created, that has the same informations as the |
||
284 | * given entity (element cloning). |
||
285 | * |
||
286 | * @param AbstractDBElement $entity The entity for which the link should be generated |
||
287 | * |
||
288 | * @return string the URL to the page |
||
289 | * |
||
290 | * @throws EntityNotSupportedException If the method is not supported for the given Entity |
||
291 | */ |
||
292 | public function cloneURL(AbstractDBElement $entity): string |
||
293 | { |
||
294 | $map = [ |
||
295 | Part::class => 'part_clone', |
||
296 | AttachmentType::class => 'attachment_type_clone', |
||
297 | Category::class => 'category_clone', |
||
298 | Project::class => 'device_clone', |
||
299 | Supplier::class => 'supplier_clone', |
||
300 | Manufacturer::class => 'manufacturer_clone', |
||
301 | Storelocation::class => 'store_location_clone', |
||
302 | Footprint::class => 'footprint_clone', |
||
303 | User::class => 'user_clone', |
||
304 | Currency::class => 'currency_clone', |
||
305 | MeasurementUnit::class => 'measurement_unit_clone', |
||
306 | Group::class => 'group_clone', |
||
307 | LabelProfile::class => 'label_profile_clone', |
||
308 | ]; |
||
309 | |||
310 | return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Generates an URL to a page, where all parts are listed, which are contained in the given element. |
||
315 | * |
||
316 | * @param AbstractDBElement $entity The entity for which the link should be generated |
||
317 | * |
||
318 | * @return string the URL to the page |
||
319 | * |
||
320 | * @throws EntityNotSupportedException If the method is not supported for the given Entity |
||
321 | */ |
||
322 | public function listPartsURL(AbstractDBElement $entity): string |
||
323 | { |
||
324 | $map = [ |
||
325 | Project::class => 'project_info', |
||
326 | |||
327 | Category::class => 'part_list_category', |
||
328 | Footprint::class => 'part_list_footprint', |
||
329 | Manufacturer::class => 'part_list_manufacturer', |
||
330 | Supplier::class => 'part_list_supplier', |
||
331 | Storelocation::class => 'part_list_store_location', |
||
332 | ]; |
||
333 | |||
334 | return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]); |
||
335 | } |
||
336 | |||
337 | public function deleteURL(AbstractDBElement $entity): string |
||
338 | { |
||
339 | $map = [ |
||
340 | Part::class => 'part_delete', |
||
341 | AttachmentType::class => 'attachment_type_delete', |
||
342 | Category::class => 'category_delete', |
||
343 | Project::class => 'project_delete', |
||
344 | Supplier::class => 'supplier_delete', |
||
345 | Manufacturer::class => 'manufacturer_delete', |
||
346 | Storelocation::class => 'store_location_delete', |
||
347 | Footprint::class => 'footprint_delete', |
||
348 | User::class => 'user_delete', |
||
349 | Currency::class => 'currency_delete', |
||
350 | MeasurementUnit::class => 'measurement_unit_delete', |
||
351 | Group::class => 'group_delete', |
||
352 | LabelProfile::class => 'label_profile_delete', |
||
353 | ]; |
||
354 | |||
355 | return $this->urlGenerator->generate($this->mapToController($map, $entity), ['id' => $entity->getID()]); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * Finds the controller name for the class of the entity using the given map. |
||
360 | * Throws an exception if the entity class is not known to the map. |
||
361 | * |
||
362 | * @param array $map The map that should be used for determing the controller |
||
363 | * @param mixed $entity The entity for which the controller name should be determined |
||
364 | * |
||
365 | * @return string The name of the controller fitting the entity class |
||
366 | * |
||
367 | * @throws EntityNotSupportedException |
||
368 | */ |
||
369 | protected function mapToController(array $map, $entity): string |
||
370 | { |
||
371 | $class = get_class($entity); |
||
372 | |||
373 | //Check if we have an direct mapping for the given class |
||
374 | if (!array_key_exists($class, $map)) { |
||
375 | //Check if we need to check inheritance by looping through our map |
||
376 | foreach (array_keys($map) as $key) { |
||
377 | if (is_a($entity, $key)) { |
||
378 | return $map[$key]; |
||
379 | } |
||
380 | } |
||
381 | |||
382 | throw new EntityNotSupportedException(sprintf('The given entity is not supported yet! Passed class type: %s', get_class($entity))); |
||
383 | } |
||
384 | |||
385 | return $map[$class]; |
||
386 | } |
||
387 | } |
||
388 |