Total Complexity | 61 |
Total Lines | 304 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like EntityURLGenerator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EntityURLGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class EntityURLGenerator |
||
50 | { |
||
51 | /** |
||
52 | * @var UrlGeneratorInterface |
||
53 | */ |
||
54 | protected $urlGenerator; |
||
55 | |||
56 | public function __construct(UrlGeneratorInterface $urlGenerator) |
||
57 | { |
||
58 | $this->urlGenerator = $urlGenerator; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Generates an URL to the page using the given page type and element. |
||
63 | * For the given types, the [type]URL() functions are called (e.g. infoURL()). |
||
64 | * Not all entity class and $type combinations are supported. |
||
65 | * |
||
66 | * @param $entity mixed The element for which the page should be generated. |
||
67 | * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
||
68 | * @return string The link to the desired page. |
||
69 | * @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
||
70 | * @throws \InvalidArgumentException Thrown if the givent type is not existing. |
||
71 | */ |
||
72 | public function getURL($entity, string $type) |
||
73 | { |
||
74 | switch ($type) { |
||
75 | case 'info': |
||
76 | return $this->infoURL($entity); |
||
77 | case 'edit': |
||
78 | return $this->editURL($entity); |
||
79 | case 'create': |
||
80 | return $this->createURL($entity); |
||
81 | case 'clone': |
||
82 | return $this->cloneURL($entity); |
||
83 | case 'list': |
||
84 | case 'list_parts': |
||
85 | return $this->listPartsURL($entity); |
||
86 | case 'delete': |
||
87 | return $this->deleteURL($entity); |
||
88 | case 'file_download': |
||
89 | return $this->downloadURL($entity); |
||
90 | case 'file_view': |
||
91 | return $this->viewURL($entity); |
||
92 | } |
||
93 | |||
94 | throw new \InvalidArgumentException('Method is not supported!'); |
||
95 | } |
||
96 | |||
97 | public function viewURL($entity) : string |
||
98 | { |
||
99 | if ($entity instanceof Attachment) { |
||
100 | if ($entity->isExternal()) { //For external attachments, return the link to external path |
||
101 | return $entity->getURL(); |
||
|
|||
102 | } |
||
103 | return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]); |
||
104 | } |
||
105 | |||
106 | //Otherwise throw an error |
||
107 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
108 | } |
||
109 | |||
110 | public function downloadURL($entity) : string |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Generates an URL to a page, where info about this entity can be viewed. |
||
125 | * |
||
126 | * @param $entity mixed The entity for which the info should be generated. |
||
127 | * @return string The URL to the info page |
||
128 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
129 | */ |
||
130 | public function infoURL($entity): string |
||
131 | { |
||
132 | if ($entity instanceof Part) { |
||
133 | return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
||
134 | } |
||
135 | |||
136 | //Otherwise throw an error |
||
137 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Generates an URL to a page, where this entity can be edited. |
||
142 | * |
||
143 | * @param $entity mixed The entity for which the edit link should be generated. |
||
144 | * @return string The URL to the edit page. |
||
145 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
146 | */ |
||
147 | public function editURL($entity): string |
||
148 | { |
||
149 | if ($entity instanceof Part) { |
||
150 | return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
||
151 | } |
||
152 | |||
153 | if ($entity instanceof AttachmentType) { |
||
154 | return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
||
155 | } |
||
156 | |||
157 | if ($entity instanceof Category) { |
||
158 | return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
||
159 | } |
||
160 | |||
161 | if ($entity instanceof Device) { |
||
162 | return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
||
163 | } |
||
164 | |||
165 | if ($entity instanceof Supplier) { |
||
166 | return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
||
167 | } |
||
168 | |||
169 | if ($entity instanceof Manufacturer) { |
||
170 | return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
||
171 | } |
||
172 | |||
173 | if ($entity instanceof Storelocation) { |
||
174 | return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
||
175 | } |
||
176 | |||
177 | if ($entity instanceof Footprint) { |
||
178 | return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]); |
||
179 | } |
||
180 | |||
181 | if ($entity instanceof User) { |
||
182 | return $this->urlGenerator->generate('user_edit', ['id' => $entity->getID()]); |
||
183 | } |
||
184 | |||
185 | if ($entity instanceof Currency) { |
||
186 | return $this->urlGenerator->generate('currency_edit', ['id' => $entity->getID()]); |
||
187 | } |
||
188 | |||
189 | if ($entity instanceof MeasurementUnit) { |
||
190 | return $this->urlGenerator->generate('measurement_unit_edit', ['id' => $entity->getID()]); |
||
191 | } |
||
192 | |||
193 | //Otherwise throw an error |
||
194 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Generates an URL to a page, where a entity of this type can be created. |
||
199 | * |
||
200 | * @param $entity mixed The entity for which the link should be generated. |
||
201 | * @return string The URL to the page. |
||
202 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
203 | */ |
||
204 | public function createURL($entity): string |
||
205 | { |
||
206 | if ($entity instanceof Part) { |
||
207 | return $this->urlGenerator->generate('part_new'); |
||
208 | } |
||
209 | |||
210 | if ($entity instanceof AttachmentType) { |
||
211 | return $this->urlGenerator->generate('attachment_type_new'); |
||
212 | } |
||
213 | |||
214 | if ($entity instanceof Category) { |
||
215 | return $this->urlGenerator->generate('category_new'); |
||
216 | } |
||
217 | |||
218 | if ($entity instanceof Device) { |
||
219 | return $this->urlGenerator->generate('device_new'); |
||
220 | } |
||
221 | |||
222 | if ($entity instanceof Supplier) { |
||
223 | return $this->urlGenerator->generate('supplier_new'); |
||
224 | } |
||
225 | |||
226 | if ($entity instanceof Manufacturer) { |
||
227 | return $this->urlGenerator->generate('manufacturer_new'); |
||
228 | } |
||
229 | |||
230 | if ($entity instanceof Storelocation) { |
||
231 | return $this->urlGenerator->generate('store_location_new'); |
||
232 | } |
||
233 | |||
234 | if ($entity instanceof Footprint) { |
||
235 | return $this->urlGenerator->generate('footprint_new'); |
||
236 | } |
||
237 | |||
238 | if ($entity instanceof User) { |
||
239 | return $this->urlGenerator->generate('user_new'); |
||
240 | } |
||
241 | |||
242 | if ($entity instanceof Currency) { |
||
243 | return $this->urlGenerator->generate('currency_new'); |
||
244 | } |
||
245 | |||
246 | if ($entity instanceof MeasurementUnit) { |
||
247 | return $this->urlGenerator->generate('measurement_unit_new'); |
||
248 | } |
||
249 | |||
250 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Generates an URL to a page, where a new entity can be created, that has the same informations as the |
||
255 | * given entity (element cloning) |
||
256 | * |
||
257 | * @param $entity mixed The entity for which the link should be generated. |
||
258 | * @return string The URL to the page. |
||
259 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
260 | */ |
||
261 | public function cloneURL($entity): string |
||
262 | { |
||
263 | if ($entity instanceof Part) { |
||
264 | return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
||
265 | } |
||
266 | |||
267 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Generates an URL to a page, where all parts are listed, which are contained in the given element. |
||
272 | * |
||
273 | * @param $entity mixed The entity for which the link should be generated. |
||
274 | * @return string The URL to the page. |
||
275 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
276 | */ |
||
277 | public function listPartsURL($entity) : string |
||
283 | |||
284 | } |
||
285 | |||
286 | public function deleteURL($entity) : string |
||
287 | { |
||
288 | if ($entity instanceof Part) { |
||
289 | return $this->urlGenerator->generate('part_delete', ['id' => $entity->getID()]); |
||
290 | } |
||
291 | |||
292 | if ($entity instanceof AttachmentType) { |
||
293 | return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
||
294 | } |
||
295 | |||
296 | if ($entity instanceof Category) { |
||
297 | return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
||
298 | } |
||
299 | |||
300 | if ($entity instanceof Device) { |
||
301 | return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
||
302 | } |
||
303 | |||
304 | if ($entity instanceof Supplier) { |
||
305 | return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
||
306 | } |
||
307 | |||
308 | if ($entity instanceof Manufacturer) { |
||
309 | return $this->urlGenerator->generate('manufacturer_delete', ['id' => $entity->getID()]); |
||
310 | } |
||
311 | |||
312 | if ($entity instanceof Storelocation) { |
||
313 | return $this->urlGenerator->generate('store_location_delete', ['id' => $entity->getID()]); |
||
314 | } |
||
315 | |||
316 | if ($entity instanceof Footprint) { |
||
317 | return $this->urlGenerator->generate('footprint_delete', ['id' => $entity->getID()]); |
||
318 | } |
||
319 | |||
320 | if ($entity instanceof User) { |
||
321 | return $this->urlGenerator->generate('user_delete', ['id' => $entity->getID()]); |
||
322 | } |
||
323 | |||
324 | if ($entity instanceof Currency) { |
||
325 | return $this->urlGenerator->generate('currency_delete', ['id' => $entity->getID()]); |
||
326 | } |
||
327 | |||
328 | if ($entity instanceof MeasurementUnit) { |
||
329 | return $this->urlGenerator->generate('measurement_unit_delete', ['id' => $entity->getID()]); |
||
330 | } |
||
331 | |||
332 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Generates an HTML link to the info page about the given entity. |
||
337 | * |
||
338 | * @param $entity mixed The entity for which the info link should be generated. |
||
339 | * |
||
340 | * @return string The HTML of the info page link |
||
341 | * |
||
342 | * @throws EntityNotSupported |
||
343 | */ |
||
344 | public function infoHTML($entity): string |
||
353 | } |
||
354 | } |
||
355 |