Total Complexity | 54 |
Total Lines | 276 |
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) |
||
93 | } |
||
94 | |||
95 | public function viewURL($entity) : string |
||
96 | { |
||
97 | if ($entity instanceof Attachment) { |
||
98 | if ($entity->isExternal()) { //For external attachments, return the link to external path |
||
99 | return $entity->getURL(); |
||
|
|||
100 | } |
||
101 | return $this->urlGenerator->generate('attachment_view', ['id' => $entity->getID()]); |
||
102 | } |
||
103 | |||
104 | //Otherwise throw an error |
||
105 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
106 | } |
||
107 | |||
108 | public function downloadURL($entity) : string |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Generates an URL to a page, where info about this entity can be viewed. |
||
123 | * |
||
124 | * @param $entity mixed The entity for which the info should be generated. |
||
125 | * @return string The URL to the info page |
||
126 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
127 | */ |
||
128 | public function infoURL($entity): string |
||
129 | { |
||
130 | if ($entity instanceof Part) { |
||
131 | return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
||
132 | } |
||
133 | |||
134 | //Otherwise throw an error |
||
135 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Generates an URL to a page, where this entity can be edited. |
||
140 | * |
||
141 | * @param $entity mixed The entity for which the edit link should be generated. |
||
142 | * @return string The URL to the edit page. |
||
143 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
144 | */ |
||
145 | public function editURL($entity): string |
||
146 | { |
||
147 | if ($entity instanceof Part) { |
||
148 | return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
||
149 | } |
||
150 | |||
151 | if ($entity instanceof AttachmentType) { |
||
152 | return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
||
153 | } |
||
154 | |||
155 | if ($entity instanceof Category) { |
||
156 | return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
||
157 | } |
||
158 | |||
159 | if ($entity instanceof Device) { |
||
160 | return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
||
161 | } |
||
162 | |||
163 | if ($entity instanceof Supplier) { |
||
164 | return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
||
165 | } |
||
166 | |||
167 | if ($entity instanceof Manufacturer) { |
||
168 | return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
||
169 | } |
||
170 | |||
171 | if ($entity instanceof Storelocation) { |
||
172 | return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
||
173 | } |
||
174 | |||
175 | if ($entity instanceof Footprint) { |
||
176 | return $this->urlGenerator->generate("footprint_edit", ['id' => $entity->getID()]); |
||
177 | } |
||
178 | |||
179 | if($entity instanceof User) { |
||
180 | return $this->urlGenerator->generate('user_edit', ['id' => $entity->getID()]); |
||
181 | } |
||
182 | |||
183 | //Otherwise throw an error |
||
184 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Generates an URL to a page, where a entity of this type can be created. |
||
189 | * |
||
190 | * @param $entity mixed The entity for which the link should be generated. |
||
191 | * @return string The URL to the page. |
||
192 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
193 | */ |
||
194 | public function createURL($entity): string |
||
195 | { |
||
196 | if ($entity instanceof Part) { |
||
197 | return $this->urlGenerator->generate('part_new'); |
||
198 | } |
||
199 | |||
200 | if ($entity instanceof AttachmentType) { |
||
201 | return $this->urlGenerator->generate('attachment_type_new'); |
||
202 | } |
||
203 | |||
204 | if ($entity instanceof Category) { |
||
205 | return $this->urlGenerator->generate('category_new'); |
||
206 | } |
||
207 | |||
208 | if ($entity instanceof Device) { |
||
209 | return $this->urlGenerator->generate('device_new'); |
||
210 | } |
||
211 | |||
212 | if ($entity instanceof Supplier) { |
||
213 | return $this->urlGenerator->generate('supplier_new'); |
||
214 | } |
||
215 | |||
216 | if ($entity instanceof Manufacturer) { |
||
217 | return $this->urlGenerator->generate('manufacturer_new'); |
||
218 | } |
||
219 | |||
220 | if ($entity instanceof Storelocation) { |
||
221 | return $this->urlGenerator->generate('store_location_new'); |
||
222 | } |
||
223 | |||
224 | if ($entity instanceof Footprint) { |
||
225 | return $this->urlGenerator->generate('footprint_new'); |
||
226 | } |
||
227 | |||
228 | if ($entity instanceof User) { |
||
229 | return $this->urlGenerator->generate('user_new'); |
||
230 | } |
||
231 | |||
232 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Generates an URL to a page, where a new entity can be created, that has the same informations as the |
||
237 | * given entity (element cloning) |
||
238 | * |
||
239 | * @param $entity mixed The entity for which the link should be generated. |
||
240 | * @return string The URL to the page. |
||
241 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
242 | */ |
||
243 | public function cloneURL($entity): string |
||
244 | { |
||
245 | if ($entity instanceof Part) { |
||
246 | return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
||
247 | } |
||
248 | |||
249 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Generates an URL to a page, where all parts are listed, which are contained in the given element. |
||
254 | * |
||
255 | * @param $entity mixed The entity for which the link should be generated. |
||
256 | * @return string The URL to the page. |
||
257 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
258 | */ |
||
259 | public function listPartsURL($entity) : string |
||
265 | |||
266 | } |
||
267 | |||
268 | public function deleteURL($entity) : string |
||
269 | { |
||
270 | if ($entity instanceof AttachmentType) { |
||
271 | return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
||
272 | } |
||
273 | |||
274 | if ($entity instanceof Category) { |
||
275 | return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
||
276 | } |
||
277 | |||
278 | if ($entity instanceof Device) { |
||
279 | return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
||
280 | } |
||
281 | |||
282 | if ($entity instanceof Supplier) { |
||
283 | return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
||
284 | } |
||
285 | |||
286 | if ($entity instanceof Manufacturer) { |
||
287 | return $this->urlGenerator->generate('manufacturer_delete', ['id' => $entity->getID()]); |
||
288 | } |
||
289 | |||
290 | if ($entity instanceof Storelocation) { |
||
291 | return $this->urlGenerator->generate('store_location_delete', ['id' => $entity->getID()]); |
||
292 | } |
||
293 | |||
294 | if ($entity instanceof Footprint) { |
||
295 | return $this->urlGenerator->generate('footprint_delete', ['id' => $entity->getID()]); |
||
296 | } |
||
297 | |||
298 | if ($entity instanceof User) { |
||
299 | return $this->urlGenerator->generate('user_delete', ['id' => $entity->getID()]); |
||
300 | } |
||
301 | |||
302 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Generates an HTML link to the info page about the given entity. |
||
307 | * |
||
308 | * @param $entity mixed The entity for which the info link should be generated. |
||
309 | * |
||
310 | * @return string The HTML of the info page link |
||
311 | * |
||
312 | * @throws EntityNotSupported |
||
313 | */ |
||
314 | public function infoHTML($entity): string |
||
323 | } |
||
324 | } |
||
325 |