Total Complexity | 48 |
Total Lines | 310 |
Duplicated Lines | 0 % |
Changes | 8 | ||
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 | * @param $data |
||
116 | * @return string|null |
||
117 | */ |
||
118 | public function route(string $name, array $data = null): ?string |
||
119 | { |
||
120 | foreach ($this->routes as $http_verb) { |
||
121 | foreach ($http_verb as $route_item) { |
||
122 | $this->treat($name, $route_item, $data); |
||
|
|||
123 | } |
||
124 | } |
||
125 | return null; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param string $name |
||
130 | * @param array $route_item |
||
131 | * @param array $data |
||
132 | * @return string|null |
||
133 | */ |
||
134 | private function treat(string $name, array $route_item, array $data): ?string |
||
135 | { |
||
136 | if (!empty($route_item["name"]) && $route_item["name"] == $name) { |
||
137 | $route = $route_item["route"]; |
||
138 | if ($data) { |
||
139 | $arguments = []; |
||
140 | foreach ($data as $key => $value) { |
||
141 | if (!strstr($route, "{{$key}}")) { |
||
142 | $params[$key] = $value; |
||
143 | } |
||
144 | $arguments["{{$key}}"] = $value; |
||
145 | } |
||
146 | $params = (!empty($params) ? "?" . http_build_query($params) : null); |
||
147 | $route = str_replace(array_keys($arguments), array_values($arguments), $route) . "{$params}"; |
||
148 | } |
||
149 | return "{$this->projectUrl}{$route}"; |
||
150 | } |
||
151 | |||
152 | return null; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @param string $route |
||
157 | * @param array $data |
||
158 | */ |
||
159 | public function redirect(string $route, array $data = null): void |
||
160 | { |
||
161 | if ($name = $this->route($route, $data)) { |
||
162 | header("Location: {$name}"); |
||
163 | exit; |
||
164 | } |
||
165 | |||
166 | if (filter_var($route, FILTER_VALIDATE_URL)) { |
||
167 | header("Location: {$route}"); |
||
168 | exit; |
||
169 | } |
||
170 | |||
171 | $route = (substr($route, 0, 1) == "/" ? $route : "/{$route}"); |
||
172 | header("Location: {$this->projectUrl}{$route}"); |
||
173 | exit; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function dispatch(): bool |
||
180 | { |
||
181 | if (empty($this->routes) || empty($this->routes[$this->httpMethod])) { |
||
182 | $this->error = self::NOT_IMPLEMENTED; |
||
183 | return false; |
||
184 | } |
||
185 | |||
186 | $this->route = null; |
||
187 | foreach ($this->routes[$this->httpMethod] as $key => $route) { |
||
188 | if (preg_match("~^" . $key . "$~", $this->patch, $found)) { |
||
189 | $this->route = $route; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | return $this->execute(); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return bool |
||
198 | */ |
||
199 | private function execute() |
||
200 | { |
||
201 | if ($this->route) { |
||
202 | if (is_callable($this->route['handler'])) { |
||
203 | call_user_func($this->route['handler'], ($this->route['data'] ?? [])); |
||
204 | return true; |
||
205 | } |
||
206 | |||
207 | $controller = $this->route['handler']; |
||
208 | $method = $this->route['action']; |
||
209 | |||
210 | if (class_exists($controller)) { |
||
211 | $newController = new $controller($this); |
||
212 | if (method_exists($controller, $method)) { |
||
213 | $newController->$method(($this->route['data'] ?? [])); |
||
214 | return true; |
||
215 | } |
||
216 | |||
217 | $this->error = self::METHOD_NOT_ALLOWED; |
||
218 | return false; |
||
219 | } |
||
220 | |||
221 | $this->error = self::BAD_REQUEST; |
||
222 | return false; |
||
223 | } |
||
224 | |||
225 | $this->error = self::NOT_FOUND; |
||
226 | return false; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * @param string $method |
||
231 | * @param string $route |
||
232 | * @param string|callable $handler |
||
233 | * @param null|string |
||
234 | * @return Dispatch |
||
235 | */ |
||
236 | protected function addRoute(string $method, string $route, $handler, string $name = null): Dispatch |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * httpMethod form spoofing |
||
272 | */ |
||
273 | protected function formSpoofing(): void |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param $handler |
||
306 | * @param $namespace |
||
307 | * @return string|callable |
||
308 | */ |
||
309 | private function handler($handler, $namespace) |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @param $handler |
||
316 | * @return null|string |
||
317 | */ |
||
318 | private function action($handler): ?string |
||
321 | } |
||
322 | } |