Total Complexity | 40 |
Total Lines | 222 |
Duplicated Lines | 0 % |
Changes | 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 |
||
44 | class EntityURLGenerator |
||
45 | { |
||
46 | /** |
||
47 | * @var UrlGeneratorInterface |
||
48 | */ |
||
49 | protected $urlGenerator; |
||
50 | |||
51 | public function __construct(UrlGeneratorInterface $urlGenerator) |
||
52 | { |
||
53 | $this->urlGenerator = $urlGenerator; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Generates an URL to the page using the given page type and element. |
||
58 | * For the given types, the [type]URL() functions are called (e.g. infoURL()). |
||
59 | * Not all entity class and $type combinations are supported. |
||
60 | * |
||
61 | * @param $entity mixed The element for which the page should be generated. |
||
62 | * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' |
||
63 | * @return string The link to the desired page. |
||
64 | * @throws EntityNotSupported Thrown if the entity is not supported for the given type. |
||
65 | * @throws \InvalidArgumentException Thrown if the givent type is not existing. |
||
66 | */ |
||
67 | public function getURL($entity, string $type) |
||
68 | { |
||
69 | switch ($type) { |
||
70 | case 'info': |
||
71 | return $this->infoURL($entity); |
||
72 | case 'edit': |
||
73 | return $this->editURL($entity); |
||
74 | case 'create': |
||
75 | return $this->createURL($entity); |
||
76 | case 'clone': |
||
77 | return $this->cloneURL($entity); |
||
78 | case 'list': |
||
79 | case 'list_parts': |
||
80 | return $this->listPartsURL($entity); |
||
81 | case 'delete': |
||
82 | return $this->deleteURL($entity); |
||
83 | } |
||
84 | |||
85 | throw new \InvalidArgumentException('Method is not supported!'); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Generates an URL to a page, where info about this entity can be viewed. |
||
90 | * |
||
91 | * @param $entity mixed The entity for which the info should be generated. |
||
92 | * @return string The URL to the info page |
||
93 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
94 | */ |
||
95 | public function infoURL($entity): string |
||
96 | { |
||
97 | if ($entity instanceof Part) { |
||
98 | return $this->urlGenerator->generate('part_info', ['id' => $entity->getID()]); |
||
99 | } |
||
100 | |||
101 | //Otherwise throw an error |
||
102 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Generates an URL to a page, where this entity can be edited. |
||
107 | * |
||
108 | * @param $entity mixed The entity for which the edit link should be generated. |
||
109 | * @return string The URL to the edit page. |
||
110 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
111 | */ |
||
112 | public function editURL($entity): string |
||
113 | { |
||
114 | if ($entity instanceof Part) { |
||
115 | return $this->urlGenerator->generate('part_edit', ['id' => $entity->getID()]); |
||
116 | } |
||
117 | |||
118 | if ($entity instanceof AttachmentType) { |
||
119 | return $this->urlGenerator->generate('attachment_type_edit', ['id' => $entity->getID()]); |
||
120 | } |
||
121 | |||
122 | if ($entity instanceof Category) { |
||
123 | return $this->urlGenerator->generate("category_edit", ['id' => $entity->getID()]); |
||
124 | } |
||
125 | |||
126 | if ($entity instanceof Device) { |
||
127 | return $this->urlGenerator->generate("device_edit", ['id' => $entity->getID()]); |
||
128 | } |
||
129 | |||
130 | if ($entity instanceof Supplier) { |
||
131 | return $this->urlGenerator->generate("supplier_edit", ['id' => $entity->getID()]); |
||
132 | } |
||
133 | |||
134 | if ($entity instanceof Manufacturer) { |
||
135 | return $this->urlGenerator->generate("manufacturer_edit", ['id' => $entity->getID()]); |
||
136 | } |
||
137 | |||
138 | if ($entity instanceof Storelocation) { |
||
139 | return $this->urlGenerator->generate("store_location_edit", ['id' => $entity->getID()]); |
||
140 | } |
||
141 | |||
142 | //Otherwise throw an error |
||
143 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Generates an URL to a page, where a entity of this type can be created. |
||
148 | * |
||
149 | * @param $entity mixed The entity for which the link should be generated. |
||
150 | * @return string The URL to the page. |
||
151 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
152 | */ |
||
153 | public function createURL($entity): string |
||
154 | { |
||
155 | if ($entity instanceof Part) { |
||
156 | return $this->urlGenerator->generate('part_new'); |
||
157 | } |
||
158 | |||
159 | if ($entity instanceof AttachmentType) { |
||
160 | return $this->urlGenerator->generate('attachment_type_new'); |
||
161 | } |
||
162 | |||
163 | if ($entity instanceof Category) { |
||
164 | return $this->urlGenerator->generate('category_new'); |
||
165 | } |
||
166 | |||
167 | if ($entity instanceof Device) { |
||
168 | return $this->urlGenerator->generate('device_new'); |
||
169 | } |
||
170 | |||
171 | if ($entity instanceof Supplier) { |
||
172 | return $this->urlGenerator->generate('supplier_new'); |
||
173 | } |
||
174 | |||
175 | if ($entity instanceof Manufacturer) { |
||
176 | return $this->urlGenerator->generate('manufacturer_new'); |
||
177 | } |
||
178 | |||
179 | if ($entity instanceof Storelocation) { |
||
180 | return $this->urlGenerator->generate('store_location_new'); |
||
181 | } |
||
182 | |||
183 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Generates an URL to a page, where a new entity can be created, that has the same informations as the |
||
188 | * given entity (element cloning) |
||
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 cloneURL($entity): string |
||
195 | { |
||
196 | if ($entity instanceof Part) { |
||
197 | return $this->urlGenerator->generate('part_clone', ['id' => $entity->getID()]); |
||
198 | } |
||
199 | |||
200 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Generates an URL to a page, where all parts are listed, which are contained in the given element. |
||
205 | * |
||
206 | * @param $entity mixed The entity for which the link should be generated. |
||
207 | * @return string The URL to the page. |
||
208 | * @throws EntityNotSupported If the method is not supported for the given Entity |
||
209 | */ |
||
210 | public function listPartsURL($entity) : string |
||
216 | |||
217 | } |
||
218 | |||
219 | public function deleteURL($entity) : string |
||
220 | { |
||
221 | if ($entity instanceof AttachmentType) { |
||
222 | return $this->urlGenerator->generate('attachment_type_delete', ['id' => $entity->getID()]); |
||
223 | } |
||
224 | |||
225 | if ($entity instanceof Category) { |
||
226 | return $this->urlGenerator->generate('category_delete', ['id' => $entity->getID()]); |
||
227 | } |
||
228 | |||
229 | if ($entity instanceof Device) { |
||
230 | return $this->urlGenerator->generate('device_delete', ['id' => $entity->getID()]); |
||
231 | } |
||
232 | |||
233 | if ($entity instanceof Supplier) { |
||
234 | return $this->urlGenerator->generate('supplier_delete', ['id' => $entity->getID()]); |
||
235 | } |
||
236 | |||
237 | if ($entity instanceof Manufacturer) { |
||
238 | return $this->urlGenerator->generate('manufacturer_new', ['id' => $entity->getID()]); |
||
239 | } |
||
240 | |||
241 | if ($entity instanceof Storelocation) { |
||
242 | return $this->urlGenerator->generate('store_location_new', ['id' => $entity->getID()]); |
||
243 | } |
||
244 | |||
245 | throw new EntityNotSupported('The given entity is not supported yet!'); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Generates an HTML link to the info page about the given entity. |
||
250 | * |
||
251 | * @param $entity mixed The entity for which the info link should be generated. |
||
252 | * |
||
253 | * @return string The HTML of the info page link |
||
254 | * |
||
255 | * @throws EntityNotSupported |
||
256 | */ |
||
257 | public function infoHTML($entity): string |
||
266 | } |
||
267 | } |
||
268 |