Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Manager |
||
| 26 | { |
||
| 27 | use HttpManagerTrait; |
||
| 28 | use EventManagerTrait; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Doctrine Entity Manager Registry |
||
| 32 | * @var EntityManagerRegistry $emr |
||
| 33 | */ |
||
| 34 | protected $emr; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Drest configuration object |
||
| 38 | * @var Configuration $config |
||
| 39 | */ |
||
| 40 | protected $config; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Metadata manager object |
||
| 44 | * @var Manager\Metadata $metadataManager |
||
| 45 | */ |
||
| 46 | protected $metadataManager; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Representation manager |
||
| 50 | * @var Manager\Representation |
||
| 51 | */ |
||
| 52 | protected $representationManager; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Drest router |
||
| 56 | * @var \Drest\Router $router |
||
| 57 | */ |
||
| 58 | protected $router; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * A service object used to handle service actions |
||
| 62 | * @var Service $service |
||
| 63 | */ |
||
| 64 | protected $service; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Error handler object |
||
| 68 | * @var AbstractHandler $error_handler |
||
| 69 | */ |
||
| 70 | protected $error_handler; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The name of an explicit route call |
||
| 74 | * @var string $named_route |
||
| 75 | */ |
||
| 76 | protected $named_route; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Optional route parameter that may have been passed |
||
| 80 | * @var array $route_params |
||
| 81 | */ |
||
| 82 | protected $route_params; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Creates an instance of the Drest Manager using the passed configuration object |
||
| 86 | * Can also pass in a Event Manager instance |
||
| 87 | * |
||
| 88 | * @param EntityManagerRegistry $entityManagerRegistry |
||
| 89 | * @param Configuration $config |
||
| 90 | * @param Event\Manager $eventManager |
||
| 91 | * @param ServiceActionRegistry $serviceActionRegistry |
||
| 92 | */ |
||
| 93 | 31 | private function __construct( |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Static call to create the Drest Manager instance |
||
| 114 | * |
||
| 115 | * @param EntityManagerRegistry $entityManagerRegistry |
||
| 116 | * @param Configuration $config |
||
| 117 | * @param Event\Manager|null $eventManager |
||
| 118 | * @param ServiceActionRegistry $serviceActionRegistry |
||
| 119 | * @return Manager $manager |
||
| 120 | */ |
||
| 121 | 31 | public static function create( |
|
| 141 | |||
| 142 | |||
| 143 | /** |
||
| 144 | * Dispatch a REST request |
||
| 145 | * @param object $request - Framework request object |
||
| 146 | * @param object $response - Framework response object |
||
| 147 | * @param string $namedRoute - Define the named Route to be dispatch - by passes the internal router |
||
| 148 | * @param array $routeParams - Route parameters to be used when dispatching a namedRoute request |
||
| 149 | * @return Response $response - returns a Drest response object which can be sent calling toString() |
||
| 150 | * @throws \Exception - Upon failure |
||
| 151 | */ |
||
| 152 | 31 | public function dispatch($request = null, $response = null, $namedRoute = null, array $routeParams = []) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Execute a dispatched request |
||
| 186 | * @throws Route\NoMatchException|\Exception |
||
| 187 | */ |
||
| 188 | 31 | protected function execute() |
|
| 206 | |||
| 207 | |||
| 208 | /** |
||
| 209 | * Determine the matched route from either the router or namedRoute |
||
| 210 | * Returns false no route could be matched (ideally the response should be returned in this instance - fail fast) |
||
| 211 | * @throws Route\NoMatchException|\Exception |
||
| 212 | * @return RouteMetaData|false $route |
||
| 213 | */ |
||
| 214 | 31 | protected function determineRoute() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Get a route based on Entity::route_name. eg Entities\User::get_users |
||
| 256 | * Syntax checking is performed |
||
| 257 | * @throws DrestException on invalid syntax or unmatched named route |
||
| 258 | * @return RouteMetaData $route |
||
| 259 | */ |
||
| 260 | 3 | protected function getNamedRoute() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Was the last dispatch request called with a named route? |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | 25 | public function calledWithANamedRoute() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Check if the client has requested the CG classes with an OPTIONS call |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | 5 | protected function doCGOptionsCheck() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * No match on route has occurred. Check the HTTP verb used for an options response |
||
| 329 | * Returns true if it is, and option information was successfully written to the response object |
||
| 330 | * @return boolean $success |
||
| 331 | */ |
||
| 332 | 4 | protected function doOptionsCheck() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Runs through all the registered routes and returns a single match |
||
| 363 | * @param boolean $matchVerb - Whether you want to match the route using the request HTTP verb |
||
| 364 | * @throws NoMatchException if no routes are found |
||
| 365 | * @throws MultipleRoutesException If there are multiple matches |
||
| 366 | * @return RouteMetaData $route |
||
| 367 | */ |
||
| 368 | 28 | protected function getMatchedRoute($matchVerb = true) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Get all possible match routes for this request |
||
| 387 | * @param boolean $matchVerb - Whether you want to match the route using the request HTTP verb |
||
| 388 | * @return array of Drest\Mapping\RouteMetaData object |
||
| 389 | */ |
||
| 390 | 2 | protected function getMatchedRoutes($matchVerb = true) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Handle an error by passing the exception to the registered error handler |
||
| 397 | * @param \Exception $e |
||
| 398 | * @throws \Exception |
||
| 399 | */ |
||
| 400 | 1 | private function handleError(\Exception $e) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get Configuration object used |
||
| 420 | * @return Configuration |
||
| 421 | */ |
||
| 422 | 31 | public function getConfiguration() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Get the entity manager registry |
||
| 429 | * @return EntityManagerRegistry |
||
| 430 | */ |
||
| 431 | 25 | public function getEntityManagerRegistry() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Get the service action registry |
||
| 438 | * @return ServiceActionRegistry |
||
| 439 | */ |
||
| 440 | public function getServiceActionRegistry() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the error handler object, if none has been injected use default from config |
||
| 447 | * @return AbstractHandler $error_handler |
||
| 448 | */ |
||
| 449 | 26 | public function getErrorHandler() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Set the error handler to use |
||
| 462 | * @param AbstractHandler $error_handler |
||
| 463 | */ |
||
| 464 | public function setErrorHandler(AbstractHandler $error_handler) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get metadata for an entity class |
||
| 471 | * @param string $className |
||
| 472 | * @return Mapping\ClassMetaData |
||
| 473 | */ |
||
| 474 | 4 | public function getClassMetadata($className) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Iterates through mapping definitions, any exceptions thrown will bubble up. |
||
| 481 | */ |
||
| 482 | public function checkDefinitions() |
||
| 488 | } |
||
| 489 |