Total Complexity | 52 |
Total Lines | 270 |
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 |
||
47 | class EntityURLGenerator |
||
48 | { |
||
49 | /** |
||
50 | * @var UrlGeneratorInterface |
||
51 | */ |
||
52 | protected $urlGenerator; |
||
53 | |||
54 | public function __construct(UrlGeneratorInterface $urlGenerator) |
||
55 | { |
||
56 | $this->urlGenerator = $urlGenerator; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Generates an URL to the page using the given page type and element. |
||
61 | * For the given types, the [type]URL() functions are called (e.g. infoURL()). |
||
62 | * Not all entity class and $type combinations are supported. |
||
63 | * |
||
64 | * @param $entity mixed The element for which the page should be generated. |
||
65 | * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
||
66 | * @return string The link to the desired page. |
||
67 | * @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
||
68 | * @throws \InvalidArgumentException Thrown if the givent type is not existing. |
||
69 | */ |
||
70 | public function getURL($entity, string $type) |
||
71 | { |
||
72 | switch ($type) { |
||
73 | case 'info': |
||
74 | return $this->infoURL($entity); |
||
75 | case 'edit': |
||
76 | return $this->editURL($entity); |
||
77 | case 'create': |
||
78 | return $this->createURL($entity); |
||
79 | case 'clone': |
||
80 | return $this->cloneURL($entity); |
||
81 | case 'list': |
||
82 | case 'list_parts': |
||
83 | return $this->listPartsURL($entity); |
||
84 | case 'delete': |
||
85 | return $this->deleteURL($entity); |
||
86 | case 'file_download': |
||
87 | return $this->downloadURL($entity); |
||
88 | case 'file_view': |
||
89 | return $this->viewURL($entity); |
||
90 | } |
||
91 | |||
92 | throw new \InvalidArgumentException('Method is not supported!'); |
||
93 | } |
||
94 | |||
95 | public function viewURL($entity) : string |
||
96 | { |
||
97 | if ($entity instanceof Attachment) { |
||
98 | return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]); |
||
99 | } |
||
100 | |||
101 | //Otherwise throw an error |
||
102 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
103 | } |
||
104 | |||
105 | public function downloadURL($entity) : string |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Generates an URL to a page, where info about this entity can be viewed. |
||
117 | * |
||
118 | * @param $entity mixed The entity for which the info should be generated. |
||
119 | * @return string The URL to the info page |
||
120 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
121 | */ |
||
122 | public function infoURL($entity): string |
||
123 | { |
||
124 | if ($entity instanceof Part) { |
||
125 | return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
||
126 | } |
||
127 | |||
128 | //Otherwise throw an error |
||
129 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Generates an URL to a page, where this entity can be edited. |
||
134 | * |
||
135 | * @param $entity mixed The entity for which the edit link should be generated. |
||
136 | * @return string The URL to the edit page. |
||
137 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
138 | */ |
||
139 | public function editURL($entity): string |
||
140 | { |
||
141 | if ($entity instanceof Part) { |
||
142 | return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
||
143 | } |
||
144 | |||
145 | if ($entity instanceof AttachmentType) { |
||
146 | return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
||
147 | } |
||
148 | |||
149 | if ($entity instanceof Category) { |
||
150 | return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
||
151 | } |
||
152 | |||
153 | if ($entity instanceof Device) { |
||
154 | return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
||
155 | } |
||
156 | |||
157 | if ($entity instanceof Supplier) { |
||
158 | return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
||
159 | } |
||
160 | |||
161 | if ($entity instanceof Manufacturer) { |
||
162 | return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
||
163 | } |
||
164 | |||
165 | if ($entity instanceof Storelocation) { |
||
166 | return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
||
167 | } |
||
168 | |||
169 | if ($entity instanceof Footprint) { |
||
170 | return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]); |
||
171 | } |
||
172 | |||
173 | if($entity instanceof User) { |
||
174 | return $this->urlGenerator->generate('user_edit', ['id' => $entity->getID()]); |
||
175 | } |
||
176 | |||
177 | //Otherwise throw an error |
||
178 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Generates an URL to a page, where a entity of this type can be created. |
||
183 | * |
||
184 | * @param $entity mixed The entity for which the link should be generated. |
||
185 | * @return string The URL to the page. |
||
186 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
187 | */ |
||
188 | public function createURL($entity): string |
||
189 | { |
||
190 | if ($entity instanceof Part) { |
||
191 | return $this->urlGenerator->generate('part_new'); |
||
192 | } |
||
193 | |||
194 | if ($entity instanceof AttachmentType) { |
||
195 | return $this->urlGenerator->generate('attachment_type_new'); |
||
196 | } |
||
197 | |||
198 | if ($entity instanceof Category) { |
||
199 | return $this->urlGenerator->generate('category_new'); |
||
200 | } |
||
201 | |||
202 | if ($entity instanceof Device) { |
||
203 | return $this->urlGenerator->generate('device_new'); |
||
204 | } |
||
205 | |||
206 | if ($entity instanceof Supplier) { |
||
207 | return $this->urlGenerator->generate('supplier_new'); |
||
208 | } |
||
209 | |||
210 | if ($entity instanceof Manufacturer) { |
||
211 | return $this->urlGenerator->generate('manufacturer_new'); |
||
212 | } |
||
213 | |||
214 | if ($entity instanceof Storelocation) { |
||
215 | return $this->urlGenerator->generate('store_location_new'); |
||
216 | } |
||
217 | |||
218 | if ($entity instanceof Footprint) { |
||
219 | return $this->urlGenerator->generate('footprint_new'); |
||
220 | } |
||
221 | |||
222 | if ($entity instanceof User) { |
||
223 | return $this->urlGenerator->generate('user_new'); |
||
224 | } |
||
225 | |||
226 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Generates an URL to a page, where a new entity can be created, that has the same informations as the |
||
231 | * given entity (element cloning) |
||
232 | * |
||
233 | * @param $entity mixed The entity for which the link should be generated. |
||
234 | * @return string The URL to the page. |
||
235 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
236 | */ |
||
237 | public function cloneURL($entity): string |
||
238 | { |
||
239 | if ($entity instanceof Part) { |
||
240 | return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
||
241 | } |
||
242 | |||
243 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Generates an URL to a page, where all parts are listed, which are contained in the given element. |
||
248 | * |
||
249 | * @param $entity mixed The entity for which the link should be generated. |
||
250 | * @return string The URL to the page. |
||
251 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
252 | */ |
||
253 | public function listPartsURL($entity) : string |
||
259 | |||
260 | } |
||
261 | |||
262 | public function deleteURL($entity) : string |
||
263 | { |
||
264 | if ($entity instanceof AttachmentType) { |
||
265 | return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
||
266 | } |
||
267 | |||
268 | if ($entity instanceof Category) { |
||
269 | return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
||
270 | } |
||
271 | |||
272 | if ($entity instanceof Device) { |
||
273 | return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
||
274 | } |
||
275 | |||
276 | if ($entity instanceof Supplier) { |
||
277 | return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
||
278 | } |
||
279 | |||
280 | if ($entity instanceof Manufacturer) { |
||
281 | return $this->urlGenerator->generate('manufacturer_delete', ['id' => $entity->getID()]); |
||
282 | } |
||
283 | |||
284 | if ($entity instanceof Storelocation) { |
||
285 | return $this->urlGenerator->generate('store_location_delete', ['id' => $entity->getID()]); |
||
286 | } |
||
287 | |||
288 | if ($entity instanceof Footprint) { |
||
289 | return $this->urlGenerator->generate('footprint_delete', ['id' => $entity->getID()]); |
||
290 | } |
||
291 | |||
292 | if ($entity instanceof User) { |
||
293 | return $this->urlGenerator->generate('user_delete', ['id' => $entity->getID()]); |
||
294 | } |
||
295 | |||
296 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * Generates an HTML link to the info page about the given entity. |
||
301 | * |
||
302 | * @param $entity mixed The entity for which the info link should be generated. |
||
303 | * |
||
304 | * @return string The HTML of the info page link |
||
305 | * |
||
306 | * @throws EntityNotSupported |
||
307 | */ |
||
308 | public function infoHTML($entity): string |
||
317 | } |
||
318 | } |
||
319 |