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 | /** |
||
| 38 | * @deprecated since 6.0.0. |
||
| 39 | */ |
||
| 40 | const LOCATION_VIEW_CONTROLLER = 'ez_content:viewLocation'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @since 6.0.0 |
||
| 44 | */ |
||
| 45 | const VIEW_ACTION = 'ez_content:viewAction'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Symfony\Component\Routing\RequestContext |
||
| 49 | */ |
||
| 50 | protected $requestContext; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var \eZ\Publish\API\Repository\LocationService |
||
| 54 | */ |
||
| 55 | protected $locationService; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \eZ\Publish\API\Repository\URLAliasService |
||
| 59 | */ |
||
| 60 | protected $urlAliasService; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var \eZ\Publish\API\Repository\ContentService |
||
| 64 | */ |
||
| 65 | protected $contentService; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var \eZ\Publish\Core\MVC\Symfony\Routing\Generator\UrlAliasGenerator |
||
| 69 | */ |
||
| 70 | protected $generator; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Holds current root Location id. |
||
| 74 | * |
||
| 75 | * @var int|string |
||
| 76 | */ |
||
| 77 | protected $rootLocationId; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \Psr\Log\LoggerInterface |
||
| 81 | */ |
||
| 82 | protected $logger; |
||
| 83 | |||
| 84 | public function __construct( |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Injects current root Location id. |
||
| 102 | * |
||
| 103 | * @param int|string $rootLocationId |
||
| 104 | */ |
||
| 105 | public function setRootLocationId($rootLocationId) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Tries to match a request with a set of routes. |
||
| 112 | * |
||
| 113 | * If the matcher can not find information, it must throw one of the exceptions documented |
||
| 114 | * below. |
||
| 115 | * |
||
| 116 | * @param Request $request The request to match |
||
| 117 | * |
||
| 118 | * @return array An array of parameters |
||
| 119 | * |
||
| 120 | * @throws \Symfony\Component\Routing\Exception\ResourceNotFoundException If no matching resource could be found |
||
| 121 | */ |
||
| 122 | public function matchRequest(Request $request) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Removes prefix from path. |
||
| 210 | * |
||
| 211 | * Checks for presence of $prefix and removes it from $path if found. |
||
| 212 | * |
||
| 213 | * @param string $path |
||
| 214 | * @param string $prefix |
||
| 215 | * |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | protected function removePathPrefix($path, $prefix) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns true of false on comparing $urlAlias->path and $path with case sensitivity. |
||
| 229 | * |
||
| 230 | * Used to determine if redirect is needed because requested path is case-different |
||
| 231 | * from the stored one. |
||
| 232 | * |
||
| 233 | * @param \eZ\Publish\API\Repository\Values\Content\URLAlias $loadedUrlAlias |
||
| 234 | * @param string $requestedPath |
||
| 235 | * @param string $pathPrefix |
||
| 236 | * |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | protected function needsCaseRedirect(URLAlias $loadedUrlAlias, $requestedPath, $pathPrefix) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Returns the UrlAlias object to use, starting from the request. |
||
| 257 | * |
||
| 258 | * @param $pathinfo |
||
| 259 | * |
||
| 260 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the path does not exist or is not valid for the given language |
||
| 261 | * |
||
| 262 | * @return URLAlias |
||
| 263 | */ |
||
| 264 | protected function getUrlAlias($pathinfo) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Gets the RouteCollection instance associated with this Router. |
||
| 271 | * |
||
| 272 | * @return RouteCollection A RouteCollection instance |
||
| 273 | */ |
||
| 274 | public function getRouteCollection() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Generates a URL for a location, from the given parameters. |
||
| 281 | * |
||
| 282 | * It is possible to directly pass a Location object as the route name, as the ChainRouter allows it through ChainedRouterInterface. |
||
| 283 | * |
||
| 284 | * 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. |
||
| 285 | * "locationId" can also be provided. |
||
| 286 | * |
||
| 287 | * If the generator is not able to generate the url, it must throw the RouteNotFoundException |
||
| 288 | * as documented below. |
||
| 289 | * |
||
| 290 | * @see UrlAliasRouter::supports() |
||
| 291 | * |
||
| 292 | * @param string|\eZ\Publish\API\Repository\Values\Content\Location $name The name of the route or a Location instance |
||
| 293 | * @param mixed $parameters An array of parameters |
||
| 294 | * @param int $referenceType The type of reference to be generated (one of the constants) |
||
| 295 | * |
||
| 296 | * @throws \LogicException |
||
| 297 | * @throws \Symfony\Component\Routing\Exception\RouteNotFoundException |
||
| 298 | * @throws \InvalidArgumentException |
||
| 299 | * |
||
| 300 | * @return string The generated URL |
||
| 301 | * |
||
| 302 | * @api |
||
| 303 | */ |
||
| 304 | public function generate($name, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
| 349 | |||
| 350 | public function setContext(RequestContext $context) |
||
| 355 | |||
| 356 | public function getContext() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Not supported. Please use matchRequest() instead. |
||
| 363 | * |
||
| 364 | * @param $pathinfo |
||
| 365 | * |
||
| 366 | * @throws \RuntimeException |
||
| 367 | */ |
||
| 368 | public function match($pathinfo) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Whether the router supports the thing in $name to generate a route. |
||
| 375 | * |
||
| 376 | * This check does not need to look if the specific instance can be |
||
| 377 | * resolved to a route, only whether the router can generate routes from |
||
| 378 | * objects of this class. |
||
| 379 | * |
||
| 380 | * @param mixed $name The route name or route object |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | */ |
||
| 384 | public function supports($name) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @see Symfony\Cmf\Component\Routing\VersatileGeneratorInterface::getRouteDebugMessage() |
||
| 391 | */ |
||
| 392 | public function getRouteDebugMessage($name, array $parameters = array()) |
||
| 404 | } |
||
| 405 |