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