| Total Complexity | 66 |
| Total Lines | 446 |
| Duplicated Lines | 0 % |
| Changes | 16 | ||
| Bugs | 0 | Features | 0 |
Complex classes like map 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 map, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | class map extends route\router implements interfaces\optionsInterface |
||
| 25 | { |
||
| 26 | use \responsible\core\traits\optionsTrait; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * [$BASE_ENDPOINTS] |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | private $BASE_ENDPOINTS = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * [$BASE_ENDPOINTS] |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | private $NAMESPACE_ENDPOINTS = array(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * [$registry] |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | private $registry = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * [$middleWareClass Holds middleware class object] |
||
| 48 | * @var object |
||
| 49 | */ |
||
| 50 | private static $middleWareClass; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * [$SYSTEM_ENDPOINTS Reserved system Endpoints] |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | public const SYSTEM_ENDPOINTS = [ |
||
| 57 | 'token' => '/token/access_token', |
||
| 58 | 'user' => [ |
||
| 59 | '/user/create', |
||
| 60 | '/user/load', |
||
| 61 | ], |
||
| 62 | ]; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * [__construct Silence...] |
||
| 66 | */ |
||
| 67 | public function __construct() |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * [register Scan and register endpoints defined in services] |
||
| 73 | * @return array |
||
| 74 | */ |
||
| 75 | public function register() |
||
| 76 | { |
||
| 77 | $options = $this->options; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Check if a custom directory was set in the Responsible API options |
||
| 81 | */ |
||
| 82 | if ( |
||
| 83 | (isset($options['classRoute']) && !empty($options['classRoute'])) && |
||
| 84 | (isset($options['classRoute']['directory']) && isset($options['classRoute']['namespace'])) |
||
| 85 | ) { |
||
| 86 | $customService = $this->options['classRoute']; |
||
| 87 | $directory = $customService['directory']; |
||
| 88 | $middleware = $customService['namespace']; |
||
| 89 | } else { |
||
| 90 | $middleware = 'responsible'; |
||
| 91 | |||
| 92 | $endpoint = str_replace( |
||
| 93 | array('core', '/', '\\'), |
||
| 94 | array('service', DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), |
||
| 95 | __NAMESPACE__ |
||
| 96 | ); |
||
| 97 | |||
| 98 | $directory = $this->route()->base['root'] . '/' . str_replace('responsible/', '', $endpoint); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!is_dir($directory)) { |
||
| 102 | (new exception\errorException()) |
||
| 103 | ->setOptions($this->getOptions()) |
||
| 104 | ->message('Directory Error:: responsible\service needs to exist. See documentation on setting up a service.') |
||
| 105 | ->error('NOT_EXTENDED'); |
||
| 106 | } |
||
| 107 | |||
| 108 | $scanned = ''; |
||
| 109 | $scanDir = scandir($directory); |
||
| 110 | |||
| 111 | if (!empty($scanDir)) { |
||
| 112 | $scanned = array_values( |
||
| 113 | array_diff( |
||
| 114 | $scanDir, |
||
| 115 | array('..', '.', '.DS_Store') |
||
| 116 | ) |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | if (empty($scanned)) { |
||
| 121 | (new exception\errorException()) |
||
| 122 | ->setOptions($this->getOptions()) |
||
| 123 | ->message('Class Error:: responsible\service\endpoints needs at least 1 class file. See documentation on setting up a service.') |
||
| 124 | ->error('NOT_EXTENDED'); |
||
| 125 | } |
||
| 126 | |||
| 127 | foreach ($scanned as $e => $point) { |
||
| 128 | if (substr($point, -4) == '.php') { |
||
| 129 | $point = str_replace('.php', '', $point); |
||
| 130 | |||
| 131 | $this->BASE_ENDPOINTS[] = $point; |
||
| 132 | |||
| 133 | $endpoint = str_replace('core', 'service', __NAMESPACE__) . '\\' . $point; |
||
| 134 | |||
| 135 | if ($middleware !== 'responsible') { |
||
| 136 | $endpoint = $middleware . '\\service\\endpoints\\' . $point; |
||
| 137 | } |
||
| 138 | |||
| 139 | $child = $endpoint; |
||
| 140 | |||
| 141 | $this->NAMESPACE_ENDPOINTS[$point] = $endpoint; |
||
| 142 | |||
| 143 | if (class_exists($child) && method_exists($child, 'middleware')) { |
||
| 144 | self::$middleWareClass = new $child(); |
||
| 145 | $this->registry[$point] = self::$middleWareClass->middleware(); |
||
| 146 | } elseif (class_exists($child) && method_exists($child, 'register')) { |
||
| 147 | self::$middleWareClass = new $child(); |
||
| 148 | $this->registry[$point] = self::$middleWareClass->register(); |
||
| 149 | } else { |
||
| 150 | (new exception\errorException()) |
||
| 151 | ->setOptions($this->getOptions()) |
||
| 152 | ->message("Class Error:: class {$child} needs to exist. See documentation on setting up a service.") |
||
| 153 | ->error('NOT_EXTENDED'); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | return $this->registry; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * [isSystemEndpoint Check if the endpoint request is a ResponsibleAPI reserved endpoint] |
||
| 162 | * @param string $api |
||
| 163 | * @param string $endpoint |
||
| 164 | * @return object|null |
||
| 165 | */ |
||
| 166 | private function isSystemEndpoint($api, $endpoint) |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * [isEndpoint Check the requested endpoint, scope and tier parts] |
||
| 207 | * @param string $api |
||
| 208 | * @param string $endpoint |
||
| 209 | * @return object|null |
||
| 210 | */ |
||
| 211 | public function isEndpoint($api, $endpoint) |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * [isWildcardEndpoint Check if the requested route matches a wildcard endpoint] |
||
| 396 | * @param string $registeredRoute |
||
| 397 | * @param string $requestedRoute |
||
| 398 | * @return bool|null |
||
| 399 | */ |
||
| 400 | private function isWildcardEndpoint($registeredRoute, $requestedRoute) |
||
| 401 | { |
||
| 402 | // Check if the registered route ends with '*' |
||
| 403 | if (substr($registeredRoute, -1) === '*') { |
||
| 404 | // Remove the trailing '*' and trim any trailing slash |
||
| 405 | $prefix = rtrim(substr($registeredRoute, 0, -1), '/'); |
||
| 406 | // Also trim any trailing slash from requested route for comparison |
||
| 407 | $requestedTrimmed = rtrim($requestedRoute, '/'); |
||
| 408 | // Check if requested route starts with the prefix |
||
| 409 | return strpos($requestedTrimmed, $prefix) === 0; |
||
| 410 | } |
||
| 411 | return null; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * [filterParts Prepare routed parts] |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | private function filterParts($uri, $parts) |
||
| 419 | { |
||
| 420 | $filter = array(); |
||
| 421 | |||
| 422 | foreach ($parts as $p => $part) { |
||
| 423 | if (is_array($part)) { |
||
| 424 | foreach ($part as $i => $parti) { |
||
| 425 | if ($parti !== $uri) { |
||
| 426 | $filter[] = $parti; |
||
| 427 | } |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } |
||
| 431 | |||
| 432 | return $filter; |
||
| 433 | } |
||
| 434 | |||
| 435 | /** |
||
| 436 | * [uriCheckSize] |
||
| 437 | * |
||
| 438 | * Compare the current request endpoint with the registered endpoint |
||
| 439 | * only return the same tier sizes |
||
| 440 | * |
||
| 441 | * @return boolean |
||
| 442 | */ |
||
| 443 | private function uriCheckSize($endpointRegister, $endpoint) |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * [getClassModel Class, Method] |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | private function getClassModel($request_path) |
||
| 473 |