Total Complexity | 42 |
Total Lines | 419 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 0 | Features | 1 |
Complex classes like server 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 server, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class server |
||
29 | { |
||
30 | /** |
||
31 | * [$config] |
||
32 | * @var object |
||
33 | */ |
||
34 | protected $config; |
||
35 | |||
36 | /** |
||
37 | * [$options Variable store for the Responsible API options set] |
||
38 | * @var array |
||
39 | */ |
||
40 | private $options = null; |
||
41 | |||
42 | /** |
||
43 | * [$DB Database PDO connector] |
||
44 | * @var object |
||
45 | */ |
||
46 | protected $DB = null; |
||
47 | |||
48 | /** |
||
49 | * [$grant_access If grant type is set then allow system scope override] |
||
50 | * @var boolean |
||
51 | */ |
||
52 | protected $grantAccess = false; |
||
53 | |||
54 | /** |
||
55 | * [$RESPONSE] |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $RESPONSE = array(); |
||
59 | |||
60 | /** |
||
61 | * [$header Header class object] |
||
62 | * @var object|null |
||
63 | */ |
||
64 | protected $header = null; |
||
65 | |||
66 | /** |
||
67 | * [$endpoints Endpoints class object] |
||
68 | * @var object|null |
||
69 | */ |
||
70 | protected $endpoints = null; |
||
71 | |||
72 | /** |
||
73 | * [$keys Keys class object] |
||
74 | * @var object|null |
||
75 | */ |
||
76 | protected $keys = null; |
||
77 | |||
78 | /** |
||
79 | * [$auth Auth class object] |
||
80 | * @var object|null |
||
81 | */ |
||
82 | protected $auth = null; |
||
83 | |||
84 | /** |
||
85 | * [$limiter Limiter class object] |
||
86 | * @var object|null |
||
87 | */ |
||
88 | protected $limiter = null; |
||
89 | |||
90 | /** |
||
91 | * [$router Router class object] |
||
92 | * @var object|null |
||
93 | */ |
||
94 | protected $router = null; |
||
95 | |||
96 | /** |
||
97 | * [__construct] |
||
98 | * @param array $config |
||
99 | * environment variables |
||
100 | * @param boolean $db |
||
101 | */ |
||
102 | public function __construct(array $config = [], array $options = [], $db = false) |
||
103 | { |
||
104 | $this->setOptions($options); |
||
105 | |||
106 | if ($db && !$this->isMockTest()) { |
||
107 | if (empty($config)) { |
||
108 | $config = new configuration\config; |
||
109 | $config->responsibleDefault(); |
||
110 | $config = $config->getConfig(); |
||
111 | } |
||
112 | if (is_null($this->DB)) { |
||
113 | $this->DB = new connect\DB($config['DB_HOST'], $config['DB_NAME'], $config['DB_USER'], $config['DB_PASSWORD']); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | $this->header = new headers\header; |
||
118 | $this->header->setOptions($options); |
||
119 | |||
120 | $this->keys = new keys\key; |
||
121 | $this->endpoints = new endpoints\map; |
||
122 | $this->endpoints->setOptions($options); |
||
123 | |||
124 | $this->auth = new auth\authorise($options); |
||
125 | $this->auth->header = $this->header; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * [options Responsible API options] |
||
130 | * @param array $options |
||
131 | */ |
||
132 | public function setOptions($options) |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * [getOptions Get the stored Responsible API options] |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getOptions():array |
||
147 | { |
||
148 | return $this->options; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * [DB Get the database instance] |
||
153 | * @return object |
||
154 | */ |
||
155 | public function DB() |
||
156 | { |
||
157 | return $this->DB; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * [requestType] |
||
162 | * @var string $type |
||
163 | * @return self |
||
164 | */ |
||
165 | public function requestType($type) |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * [getRequestType] |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getRequestType() |
||
177 | { |
||
178 | return $this->header->getRequestType(); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * [setResponse Append the Responsible API response] |
||
183 | * @param string|array $key |
||
184 | * @param array|null $response |
||
185 | */ |
||
186 | public function setResponse($key, $response) |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * [getResponse Get the Responsible API output response] |
||
211 | * @return array |
||
212 | */ |
||
213 | private function getResponse() |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * [rate Set the API rate limit] |
||
220 | * @param integer $limit [The request limit] |
||
221 | * @param string|integer $rate [The request window] |
||
222 | * @return self |
||
223 | */ |
||
224 | public function rateLimit($limit = null, $rate = null) |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * [authenticate Parse the requests to Responsible API] |
||
232 | * |
||
233 | * 1. Authorise the requests JWT |
||
234 | * 2. Throttle the requests |
||
235 | * |
||
236 | * @return self |
||
237 | */ |
||
238 | public function authenticate() |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * [route Build the Responsible router] |
||
281 | * |
||
282 | * 1. Endpoints registry |
||
283 | * 2. Build router |
||
284 | * 3. Try run middleware |
||
285 | * |
||
286 | * @return self |
||
287 | */ |
||
288 | public function route($route) |
||
289 | { |
||
290 | /** |
||
291 | * Register endpoints |
||
292 | */ |
||
293 | $this->endpoints->baseApiRoot(dirname(__DIR__)); |
||
294 | $this->endpoints->register(); |
||
295 | |||
296 | /** |
||
297 | * Initialise the router |
||
298 | */ |
||
299 | $router = new route\router(); |
||
300 | $router->baseApiRoot(dirname(__DIR__)); |
||
301 | $this->router = $router->route($route); |
||
302 | $this->router->options = $this->getOptions(); |
||
303 | $this->router->auth = $this->auth->user(); |
||
304 | $this->router->limiter = $this->limiter->getThrottle(); |
||
305 | |||
306 | /** |
||
307 | * Endpoint tiers must be larger than 1 |
||
308 | */ |
||
309 | if ($router->getSize() < 2) { |
||
310 | (new exception\errorException)->error('NOT_FOUND'); |
||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Check if the requested endpoint is allowed |
||
315 | */ |
||
316 | if (!$this->router->endpoint = |
||
317 | $this->endpoints->isEndpoint($router->getApi(), $router->getPath()) |
||
318 | ) { |
||
319 | (new exception\errorException)->error('BAD_REQUEST'); |
||
320 | } |
||
321 | |||
322 | $this->router->endpoint->header = [ |
||
323 | 'method' => $this->header->getServerMethod(), |
||
324 | 'status' => $this->header->getHeaderStatus(), |
||
325 | 'body' => $this->header->getMethod(), |
||
326 | ]; |
||
327 | |||
328 | /** |
||
329 | * Check if theres a payload sent |
||
330 | */ |
||
331 | if(isset($_REQUEST['payload'])) { |
||
332 | $router->setRequestBody($_REQUEST['payload']); |
||
333 | } |
||
334 | // print_r($_REQUEST); |
||
335 | /*if(isset($_POST) && !empty($_POST)) { |
||
336 | $router->setPostBody($_POST); |
||
337 | }*/ |
||
338 | $this->router->payload = $router->getRequestBody(); |
||
339 | |||
340 | /** |
||
341 | * Check the access scope |
||
342 | */ |
||
343 | if( !isset($this->router->endpoint->model['scope']) ) { |
||
344 | $this->router->endpoint->model['scope'] = 'private'; |
||
345 | } |
||
346 | |||
347 | if( isset($this->header->getMethod()->data['scope']) && |
||
348 | ($this->header->getMethod()->data['scope'] == 'anonymous') |
||
349 | ) { |
||
350 | $this->router->endpoint->model['scope'] = 'anonymous'; |
||
351 | } |
||
352 | |||
353 | $router->setScope($this->router->endpoint->model['scope']); |
||
354 | |||
355 | if (!$this->auth->isGrantType()) { |
||
356 | if (!$router->systemAccess($this->auth->user())) { |
||
357 | $this->header->unauthorised(); |
||
358 | } |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Try run the requests |
||
363 | */ |
||
364 | if ($router->getScope() !== 'system') { |
||
365 | $response = $router->run(); |
||
366 | |||
367 | } else { |
||
368 | /*$response = [ |
||
369 | 'system' => $router->getApi(), |
||
370 | ];*/ |
||
371 | |||
372 | $response = $router->run(); |
||
373 | } |
||
374 | |||
375 | $this->setResponse('', $response); |
||
376 | |||
377 | return $this; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * [getRouter Get the details of the Responsible API router] |
||
382 | * @return array |
||
383 | */ |
||
384 | public function getRouter() |
||
385 | { |
||
386 | return $this->router; |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * [coredata Get the core data response] |
||
391 | * @return object |
||
392 | */ |
||
393 | public function coredata() |
||
394 | { |
||
395 | /** |
||
396 | * Set the core data response |
||
397 | * Used for debugging |
||
398 | */ |
||
399 | foreach ($this->router as $key => $value) { |
||
400 | $this->setResponse($key, $value); |
||
401 | } |
||
402 | return $this; |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * [response Finnal response output] |
||
407 | * @return array|object |
||
408 | */ |
||
409 | public function response($debug = '') |
||
410 | { |
||
411 | /** |
||
412 | * Output bebug functions |
||
413 | */ |
||
414 | if (!empty($debug)) { |
||
415 | if (method_exists($this, $debug)) { |
||
416 | call_user_func(array($this, $debug)); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Set the Responsible headers |
||
422 | */ |
||
423 | $this->header->requestType($this->getRequestType()); |
||
424 | $this->header->setHeaders(); |
||
425 | |||
426 | /** |
||
427 | * Output the response if any |
||
428 | */ |
||
429 | return (new request\application($this->getRequestType())) |
||
430 | ->data($this->getResponse()); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * isMockTest |
||
435 | * Check if there's a mook server request |
||
436 | * @return boolean |
||
437 | */ |
||
438 | public function isMockTest():bool |
||
447 | } |
||
448 | } |
||
449 |