| Total Complexity | 44 |
| Total Lines | 285 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Dispatch 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 Dispatch, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class Dispatch |
||
| 12 | { |
||
| 13 | /** @var bool|string */ |
||
| 14 | protected $projectUrl; |
||
| 15 | |||
| 16 | /** @var string */ |
||
| 17 | protected $patch; |
||
| 18 | |||
| 19 | /** @var string */ |
||
| 20 | protected $separator; |
||
| 21 | |||
| 22 | /** @var string */ |
||
| 23 | protected $httpMethod; |
||
| 24 | |||
| 25 | /** @var array */ |
||
| 26 | protected $routes; |
||
| 27 | |||
| 28 | /** @var null|string */ |
||
| 29 | protected $group; |
||
| 30 | |||
| 31 | /** @var null|array */ |
||
| 32 | protected $route; |
||
| 33 | |||
| 34 | /** @var null|string */ |
||
| 35 | protected $namespace; |
||
| 36 | |||
| 37 | /** @var null|array */ |
||
| 38 | protected $data; |
||
| 39 | |||
| 40 | /** @var int */ |
||
| 41 | protected $error; |
||
| 42 | |||
| 43 | /** @const int Bad Request */ |
||
| 44 | public const BAD_REQUEST = 400; |
||
| 45 | |||
| 46 | /** @const int Not Found */ |
||
| 47 | public const NOT_FOUND = 404; |
||
| 48 | |||
| 49 | /** @const int Method Not Allowed */ |
||
| 50 | public const METHOD_NOT_ALLOWED = 405; |
||
| 51 | |||
| 52 | /** @const int Not Implemented */ |
||
| 53 | public const NOT_IMPLEMENTED = 501; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Dispatch constructor. |
||
| 57 | * |
||
| 58 | * @param string $projectUrl |
||
| 59 | * @param null|string $separator |
||
| 60 | */ |
||
| 61 | public function __construct(string $projectUrl, ?string $separator = ":") |
||
| 62 | { |
||
| 63 | $this->projectUrl = (substr($projectUrl, "-1") == "/" ? substr($projectUrl, 0, -1) : $projectUrl); |
||
| 64 | $this->patch = (filter_input(INPUT_GET, "route", FILTER_DEFAULT) ?? "/"); |
||
| 65 | $this->separator = ($separator ?? ":"); |
||
| 66 | $this->httpMethod = $_SERVER['REQUEST_METHOD']; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function __debugInfo() |
||
| 73 | { |
||
| 74 | return $this->routes; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param null|string $group |
||
| 79 | * @return Dispatch |
||
| 80 | */ |
||
| 81 | public function group(?string $group): Dispatch |
||
| 82 | { |
||
| 83 | $this->group = ($group ? str_replace("/", "", $group) : null); |
||
| 84 | return $this; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param null|string $namespace |
||
| 89 | * @return Dispatch |
||
| 90 | */ |
||
| 91 | public function namespace(?string $namespace): Dispatch |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return null|array |
||
| 99 | */ |
||
| 100 | public function data(): ?array |
||
| 101 | { |
||
| 102 | return $this->data; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @return null|int |
||
| 107 | */ |
||
| 108 | public function error(): ?int |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $name |
||
| 115 | * @return string|null |
||
| 116 | */ |
||
| 117 | public function route(string $name): ?string |
||
| 118 | { |
||
| 119 | foreach ($this->routes as $http_verb) { |
||
| 120 | foreach ($http_verb as $route_item) { |
||
| 121 | if (!empty($route_item["name"]) && $route_item["name"] == $name) { |
||
| 122 | $route = $route_item["route"]; |
||
| 123 | return "{$this->projectUrl}{$route}"; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return null; |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $route |
||
| 133 | */ |
||
| 134 | public function redirect(string $route): void |
||
| 135 | { |
||
| 136 | foreach ($this->routes as $http_verb) { |
||
| 137 | foreach ($http_verb as $route_item) { |
||
| 138 | if ($route_item["name"] == $route) { |
||
| 139 | $route = $route_item["route"]; |
||
| 140 | header("Location: {$this->projectUrl}{$route}"); |
||
| 141 | exit; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | $route = (substr($route, 0, 1) == "/" ? $route : "/{$route}"); |
||
| 147 | header("Location: {$this->projectUrl}{$route}"); |
||
| 148 | exit; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function dispatch(): bool |
||
| 155 | { |
||
| 156 | if (empty($this->routes) || empty($this->routes[$this->httpMethod])) { |
||
| 157 | $this->error = self::NOT_IMPLEMENTED; |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | |||
| 161 | $this->route = null; |
||
| 162 | foreach ($this->routes[$this->httpMethod] as $key => $route) { |
||
| 163 | if (preg_match("~^" . $key . "$~", $this->patch, $found)) { |
||
| 164 | $this->route = $route; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | |||
| 168 | return $this->execute(); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | private function execute() |
||
| 175 | { |
||
| 176 | if ($this->route) { |
||
| 177 | if (is_callable($this->route['handler'])) { |
||
| 178 | call_user_func($this->route['handler'], ($this->route['data'] ?? [])); |
||
| 179 | return true; |
||
| 180 | } |
||
| 181 | |||
| 182 | $controller = $this->route['handler']; |
||
| 183 | $method = $this->route['action']; |
||
| 184 | |||
| 185 | if (class_exists($controller)) { |
||
| 186 | $newController = new $controller($this); |
||
| 187 | if (method_exists($controller, $method)) { |
||
| 188 | $newController->$method(($this->route['data'] ?? [])); |
||
| 189 | return true; |
||
| 190 | } |
||
| 191 | |||
| 192 | $this->error = self::METHOD_NOT_ALLOWED; |
||
| 193 | return false; |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->error = self::BAD_REQUEST; |
||
| 197 | return false; |
||
| 198 | } |
||
| 199 | |||
| 200 | $this->error = self::NOT_FOUND; |
||
| 201 | return false; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $method |
||
| 206 | * @param string $route |
||
| 207 | * @param string|callable $handler |
||
| 208 | * @param null|string |
||
| 209 | * @return Dispatch |
||
| 210 | */ |
||
| 211 | protected function addRoute(string $method, string $route, $handler, string $name = null): Dispatch |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * httpMethod form spoofing |
||
| 247 | */ |
||
| 248 | protected function formSpoofing(): void |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $handler |
||
| 281 | * @param $namespace |
||
| 282 | * @return string|callable |
||
| 283 | */ |
||
| 284 | private function handler($handler, $namespace) |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param $handler |
||
| 291 | * @return null|string |
||
| 292 | */ |
||
| 293 | private function action($handler): ?string |
||
| 296 | } |
||
| 297 | } |