Complex classes like UrlAliasRouter 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 UrlAliasRouter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class UrlAliasRouter implements ChainedRouterInterface, RequestMatcherInterface |
||
34 | { |
||
35 | const URL_ALIAS_ROUTE_NAME = 'ez_urlalias'; |
||
36 | |||
37 | const VIEW_ACTION = 'ez_content:viewAction'; |
||
38 | |||
39 | /** @var \Symfony\Component\Routing\RequestContext */ |
||
40 | protected $requestContext; |
||
41 | |||
42 | /** @var \eZ\Publish\API\Repository\LocationService */ |
||
43 | protected $locationService; |
||
44 | |||
45 | /** @var \eZ\Publish\API\Repository\URLAliasService */ |
||
46 | protected $urlAliasService; |
||
47 | |||
48 | /** @var \eZ\Publish\API\Repository\ContentService */ |
||
49 | protected $contentService; |
||
50 | |||
51 | /** @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator */ |
||
52 | protected $generator; |
||
53 | |||
54 | /** |
||
55 | * Holds current root Location id. |
||
56 | * |
||
57 | * @var int|string |
||
58 | */ |
||
59 | protected $rootLocationId; |
||
60 | |||
61 | /** @var \Psr\Log\LoggerInterface */ |
||
62 | protected $logger; |
||
63 | |||
64 | public function __construct( |
||
79 | |||
80 | /** |
||
81 | * Injects current root Location id. |
||
82 | * |
||
83 | * @param int|string $rootLocationId |
||
84 | */ |
||
85 | public function setRootLocationId($rootLocationId) |
||
89 | |||
90 | /** |
||
91 | * Tries to match a request with a set of routes. |
||
92 | * |
||
93 | * If the matcher can not find information, it must throw one of the exceptions documented |
||
94 | * below. |
||
95 | * |
||
96 | * @param Request $request The request to match |
||
97 | * |
||
98 | * @return array An array of parameters |
||
99 | * |
||
100 | * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException If no matching resource could be found |
||
101 | */ |
||
102 | public function matchRequest(Request $request) |
||
200 | |||
201 | /** |
||
202 | * Removes prefix from path. |
||
203 | * |
||
204 | * Checks for presence of $prefix and removes it from $path if found. |
||
205 | * |
||
206 | * @param string $path |
||
207 | * @param string $prefix |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | protected function removePathPrefix($path, $prefix) |
||
219 | |||
220 | /** |
||
221 | * Returns true of false on comparing $urlAlias->path and $path with case sensitivity. |
||
222 | * |
||
223 | * Used to determine if redirect is needed because requested path is case-different |
||
224 | * from the stored one. |
||
225 | * |
||
226 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias $loadedUrlAlias |
||
227 | * @param string $requestedPath |
||
228 | * @param string $pathPrefix |
||
229 | * |
||
230 | * @return bool |
||
231 | */ |
||
232 | protected function needsCaseRedirect(URLAlias $loadedUrlAlias, $requestedPath, $pathPrefix) |
||
247 | |||
248 | /** |
||
249 | * Returns the UrlAlias object to use, starting from the request. |
||
250 | * |
||
251 | * @param $pathinfo |
||
252 | * |
||
253 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the path does not exist or is not valid for the given language |
||
254 | * |
||
255 | * @return URLAlias |
||
256 | */ |
||
257 | protected function getUrlAlias($pathinfo) |
||
261 | |||
262 | /** |
||
263 | * Gets the RouteCollection instance associated with this Router. |
||
264 | * |
||
265 | * @return RouteCollection A RouteCollection instance |
||
266 | */ |
||
267 | public function getRouteCollection() |
||
271 | |||
272 | /** |
||
273 | * Generates a URL for a location, from the given parameters. |
||
274 | * |
||
275 | * It is possible to directly pass a Location object as the route name, as the ChainRouter allows it through ChainedRouterInterface. |
||
276 | * |
||
277 | * If $name is a route name, the "location" key in $parameters must be set to a valid eZ\Publish\API\Repository\Values\Content\Location object. |
||
278 | * "locationId" can also be provided. |
||
279 | * |
||
280 | * If the generator is not able to generate the url, it must throw the RouteNotFoundException |
||
281 | * as documented below. |
||
282 | * |
||
283 | * @see UrlAliasRouter::supports() |
||
284 | * |
||
285 | * @param string $name The name of the route or a Location instance |
||
286 | * @param array $parameters An array of parameters |
||
287 | * @param int $referenceType The type of reference to be generated (one of the constants) |
||
288 | * |
||
289 | * @throws \LogicException |
||
290 | * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException |
||
291 | * @throws \InvalidArgumentException |
||
292 | * |
||
293 | * @return string The generated URL |
||
294 | * |
||
295 | * @api |
||
296 | */ |
||
297 | public function generate(string $name, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH): string |
||
347 | |||
348 | public function setContext(RequestContext $context) |
||
353 | |||
354 | public function getContext() |
||
358 | |||
359 | /** |
||
360 | * Not supported. Please use matchRequest() instead. |
||
361 | * |
||
362 | * @param $pathinfo |
||
363 | * |
||
364 | * @throws \RuntimeException |
||
365 | */ |
||
366 | public function match($pathinfo) |
||
370 | |||
371 | /** |
||
372 | * Whether the router supports the thing in $name to generate a route. |
||
373 | * |
||
374 | * This check does not need to look if the specific instance can be |
||
375 | * resolved to a route, only whether the router can generate routes from |
||
376 | * objects of this class. |
||
377 | * |
||
378 | * @param mixed $name The route name or route object |
||
379 | * |
||
380 | * @return bool |
||
381 | */ |
||
382 | public function supports($name) |
||
386 | |||
387 | private function supportsObject($object): bool |
||
391 | |||
392 | /** |
||
393 | * @see \Symfony\Cmf\Component\Routing\VersatileGeneratorInterface::getRouteDebugMessage() |
||
394 | */ |
||
395 | public function getRouteDebugMessage($name, array $parameters = []) |
||
407 | } |
||
408 |