Total Complexity | 42 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 10 | ||
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; |
||
41 | |||
42 | /** |
||
43 | * [$DB Database PDO connector] |
||
44 | * @var object |
||
45 | */ |
||
46 | protected $DB = null; |
||
47 | |||
48 | /** |
||
49 | * [$router The responsible API router] |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $router; |
||
53 | |||
54 | /** |
||
55 | * [$grant_access If grant type is set then allow system scope override] |
||
56 | * @var boolean |
||
57 | */ |
||
58 | protected $grantAccess = false; |
||
59 | |||
60 | /** |
||
61 | * [$ALLOWED_METHODS] |
||
62 | * @var array |
||
63 | */ |
||
64 | private $ALLOWED_METHODS = array( |
||
|
|||
65 | 'GET', |
||
66 | 'POST', |
||
67 | 'PUT', |
||
68 | 'PATCH', |
||
69 | 'DELETE', |
||
70 | ); |
||
71 | |||
72 | /** |
||
73 | * [$RESPONSE] |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $RESPONSE = array(); |
||
77 | |||
78 | /** |
||
79 | * [__construct] |
||
80 | * @param array $config |
||
81 | * environment variables |
||
82 | * @param boolean $db |
||
83 | */ |
||
84 | public function __construct(array $config = [], array $options = [], $db = false) |
||
85 | { |
||
86 | $this->setOptions($options); |
||
87 | |||
88 | if ($db && !$this->isMockTest()) { |
||
89 | if (empty($config)) { |
||
90 | $config = new configuration\config; |
||
91 | $config->responsibleDefault(); |
||
92 | $config = $config->getConfig(); |
||
93 | } |
||
94 | if (is_null($this->DB)) { |
||
95 | $this->DB = new connect\DB($config['DB_HOST'], $config['DB_NAME'], $config['DB_USER'], $config['DB_PASSWORD']); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | $this->header = new headers\header; |
||
100 | $this->header->setOptions($options); |
||
101 | |||
102 | $this->keys = new keys\key; |
||
103 | $this->endpoints = new endpoints\map; |
||
104 | $this->endpoints->setOptions($options); |
||
105 | |||
106 | $this->auth = new auth\authorise($options); |
||
107 | $this->auth->header = $this->header; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * [options Responsible API options] |
||
112 | * @param array $options |
||
113 | */ |
||
114 | public function setOptions($options) |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * [getOptions Get the stored Responsible API options] |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getOptions():array |
||
129 | { |
||
130 | return $this->options; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * [DB Get the database instance] |
||
135 | * @return object |
||
136 | */ |
||
137 | public function DB() |
||
138 | { |
||
139 | return $this->DB; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * [requestType] |
||
144 | * @var string $type |
||
145 | * @return self |
||
146 | */ |
||
147 | public function requestType($type) |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * [getRequestType] |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getRequestType() |
||
159 | { |
||
160 | return $this->header->getRequestType(); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * [setResponse Append the Responsible API response] |
||
165 | * @param [string/array] $key [Array key] |
||
166 | * @param array|null $response [Array value] |
||
167 | */ |
||
168 | public function setResponse($key, $response) |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * [getResponse Get the Responsible API output response] |
||
193 | * @return array |
||
194 | */ |
||
195 | private function getResponse() |
||
196 | { |
||
197 | return $this->RESPONSE; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * [rate Set the API rate limit] |
||
202 | * @param integer $limit [The request limit] |
||
203 | * @param string|integer $rate [The request window] |
||
204 | * @return self |
||
205 | */ |
||
206 | public function rateLimit($limit = null, $rate = null) |
||
207 | { |
||
208 | $this->limiter = new throttle\limiter($limit, $rate); |
||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * [authenticate Parse the requests to Responsible API] |
||
214 | * |
||
215 | * 1. Authorise the requests JWT |
||
216 | * 2. Throttle the requests |
||
217 | * |
||
218 | * @return self |
||
219 | */ |
||
220 | public function authenticate() |
||
221 | { |
||
222 | $options = $this->getOptions(); |
||
223 | $route = (isset($options['route']) && !empty($options['route']) ) ? $options['route'] : ''; |
||
224 | |||
225 | $this->endpoints->baseApiRoot(dirname(__DIR__)); |
||
226 | $this->endpoints->register(); |
||
227 | |||
228 | $router = new route\router(); |
||
229 | $router->baseApiRoot(dirname(__DIR__)); |
||
230 | |||
231 | $this->router = $router->route($route); |
||
232 | $endpoint = $this->endpoints->isEndpoint($router->getApi(), $router->getPath()); |
||
233 | |||
234 | if(isset($endpoint->model['scope'])) { |
||
235 | $_REQUEST['scope'] = $endpoint->model['scope']; |
||
236 | $this->header->setData($_REQUEST); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Authenticate the JWT |
||
241 | */ |
||
242 | $this->auth->authorise(); |
||
243 | |||
244 | /** |
||
245 | * Call the rate limiter then throttle |
||
246 | */ |
||
247 | if (!isset($this->limiter)) { |
||
248 | $this->rateLimit(); |
||
249 | } |
||
250 | |||
251 | $this->limiter |
||
252 | ->options($this->getOptions()) |
||
253 | ->setAccount($this->auth->user()) |
||
254 | ->setupOptions() |
||
255 | ->throttleRequest() |
||
256 | ; |
||
257 | |||
258 | return $this; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * [route Build the Responsible router] |
||
263 | * |
||
264 | * 1. Endpoints registry |
||
265 | * 2. Build router |
||
266 | * 3. Try run middleware |
||
267 | * |
||
268 | * @return array |
||
269 | */ |
||
270 | public function route($route) |
||
271 | { |
||
272 | /** |
||
273 | * Register endpoints |
||
274 | */ |
||
275 | $this->endpoints->baseApiRoot(dirname(__DIR__)); |
||
276 | $this->endpoints->register(); |
||
277 | |||
278 | /** |
||
279 | * Initialise the router |
||
280 | */ |
||
281 | $router = new route\router(); |
||
282 | $router->baseApiRoot(dirname(__DIR__)); |
||
283 | $this->router = $router->route($route); |
||
284 | $this->router->options = $this->getOptions(); |
||
285 | $this->router->auth = $this->auth->user(); |
||
286 | $this->router->limiter = $this->limiter->getThrottle(); |
||
287 | |||
288 | /** |
||
289 | * Endpoint tiers must be larger than 1 |
||
290 | */ |
||
291 | if ($router->getSize() < 2) { |
||
292 | (new exception\errorException)->error('NOT_FOUND'); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Check if the requested endpoint is allowed |
||
297 | */ |
||
298 | if (!$this->router->endpoint = |
||
299 | $this->endpoints->isEndpoint($router->getApi(), $router->getPath()) |
||
300 | ) { |
||
301 | (new exception\errorException)->error('BAD_REQUEST'); |
||
302 | } |
||
303 | |||
304 | $this->router->endpoint->header = [ |
||
305 | 'method' => $this->header->getServerMethod(), |
||
306 | 'status' => $this->header->getHeaderStatus(), |
||
307 | 'body' => $this->header->getMethod(), |
||
308 | ]; |
||
309 | |||
310 | /** |
||
311 | * Check if theres a payload sent |
||
312 | */ |
||
313 | if(isset($_REQUEST['payload'])) { |
||
314 | $router->setRequestBody($_REQUEST['payload']); |
||
315 | } |
||
316 | // print_r($_REQUEST); |
||
317 | /*if(isset($_POST) && !empty($_POST)) { |
||
318 | $router->setPostBody($_POST); |
||
319 | }*/ |
||
320 | $this->router->payload = $router->getRequestBody(); |
||
321 | |||
322 | /** |
||
323 | * Check the access scope |
||
324 | */ |
||
325 | if( !isset($this->router->endpoint->model['scope']) ) { |
||
326 | $this->router->endpoint->model['scope'] = 'private'; |
||
327 | } |
||
328 | |||
329 | if( isset($this->header->getMethod()->data['scope']) && |
||
330 | ($this->header->getMethod()->data['scope'] == 'anonymous') |
||
331 | ) { |
||
332 | $this->router->endpoint->model['scope'] = 'anonymous'; |
||
333 | } |
||
334 | |||
335 | $router->setScope($this->router->endpoint->model['scope']); |
||
336 | |||
337 | if (!$this->auth->isGrantType()) { |
||
338 | if (!$router->systemAccess($this->auth->user())) { |
||
339 | $this->header->unauthorised(); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Try run the requests |
||
345 | */ |
||
346 | if ($router->getScope() !== 'system') { |
||
347 | $response = $router->run(); |
||
348 | |||
349 | } else { |
||
350 | $response = [ |
||
351 | 'system' => $router->getApi(), |
||
352 | ]; |
||
353 | |||
354 | $response = $router->run(); |
||
355 | } |
||
356 | |||
357 | $this->setResponse('', $response); |
||
358 | |||
359 | return $this; |
||
360 | } |
||
361 | |||
362 | /** |
||
363 | * [getRouter Get the details of the Responsible API router] |
||
364 | * @return array |
||
365 | */ |
||
366 | public function getRouter() |
||
367 | { |
||
368 | return $this->router; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * [coredata Get the core data response] |
||
373 | * @return object |
||
374 | */ |
||
375 | public function coredata() |
||
376 | { |
||
377 | /** |
||
378 | * Set the core data response |
||
379 | * Used for debugging |
||
380 | */ |
||
381 | foreach ($this->router as $key => $value) { |
||
382 | $this->setResponse($key, $value); |
||
383 | } |
||
384 | return $this; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * [response Finnal response output] |
||
389 | * @return array|object |
||
390 | */ |
||
391 | public function response($debug = '') |
||
392 | { |
||
393 | /** |
||
394 | * Output bebug functions |
||
395 | */ |
||
396 | if (!empty($debug)) { |
||
397 | if (method_exists($this, $debug)) { |
||
398 | call_user_func(array($this, $debug)); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Set the Responsible headers |
||
404 | */ |
||
405 | $this->header->requestType($this->getRequestType()); |
||
406 | $this->header->setHeaders(); |
||
407 | |||
408 | /** |
||
409 | * Output the response if any |
||
410 | */ |
||
411 | return (new request\application($this->getRequestType())) |
||
412 | ->data($this->getResponse()); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * isMockTest |
||
417 | * Check if there's a mook server request |
||
418 | * @return boolean |
||
419 | */ |
||
420 | public function isMockTest():bool |
||
429 | } |
||
430 | } |
||
431 |