Total Complexity | 75 |
Total Lines | 486 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like REST 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 REST, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class REST |
||
10 | { |
||
11 | /** |
||
12 | * [private description] |
||
13 | * @var [type] |
||
|
|||
14 | */ |
||
15 | private $ci; |
||
16 | |||
17 | /** |
||
18 | * [private description] |
||
19 | * @var [type] |
||
20 | */ |
||
21 | private $api_key_limit_column; |
||
22 | |||
23 | /** |
||
24 | * [private description] |
||
25 | * @var [type] |
||
26 | */ |
||
27 | private $api_key_column; |
||
28 | |||
29 | /** |
||
30 | * [private description] |
||
31 | * @var [type] |
||
32 | */ |
||
33 | private $per_hour; |
||
34 | |||
35 | /** |
||
36 | * [private description] |
||
37 | * @var [type] |
||
38 | */ |
||
39 | private $ip_per_hour; |
||
40 | |||
41 | /** |
||
42 | * [private description] |
||
43 | * @var [type] |
||
44 | */ |
||
45 | private $show_header; |
||
46 | |||
47 | /** |
||
48 | * [private description] |
||
49 | * @var [type] |
||
50 | */ |
||
51 | private $whitelist; |
||
52 | |||
53 | /** |
||
54 | * [private description] |
||
55 | * @var [type] |
||
56 | */ |
||
57 | private $checked_rate_limit = false; |
||
58 | |||
59 | /** |
||
60 | * [private description] |
||
61 | * @var [type] |
||
62 | */ |
||
63 | private $header_prefix; |
||
64 | |||
65 | /** |
||
66 | * [private description] |
||
67 | * @var [type] |
||
68 | */ |
||
69 | private $limit_api; |
||
70 | |||
71 | /** |
||
72 | * [public description] |
||
73 | * @var [type] |
||
74 | */ |
||
75 | public $userId; |
||
76 | |||
77 | /** |
||
78 | * [public description] |
||
79 | * @var [type] |
||
80 | */ |
||
81 | public $apiKey; |
||
82 | |||
83 | /** |
||
84 | * [public description] |
||
85 | * @var [type] |
||
86 | */ |
||
87 | public $apiKeyHeader; |
||
88 | |||
89 | /** |
||
90 | * [public description] |
||
91 | * @var [type] |
||
92 | */ |
||
93 | public $token; |
||
94 | |||
95 | /** |
||
96 | * [public description] |
||
97 | * @var [type] |
||
98 | */ |
||
99 | public $allowedIps; |
||
100 | |||
101 | /** |
||
102 | * [public description] |
||
103 | * @var [type] |
||
104 | */ |
||
105 | public $config; |
||
106 | |||
107 | /** |
||
108 | * [public description] |
||
109 | * @var [type] |
||
110 | */ |
||
111 | public $authPreempted = false; |
||
112 | |||
113 | /** |
||
114 | * [PACKAGE description] |
||
115 | * @var string |
||
116 | */ |
||
117 | const PACKAGE = "francis94c/ci-rest"; |
||
118 | |||
119 | /** |
||
120 | * [RATE_LIMIT description] |
||
121 | * @var string |
||
122 | */ |
||
123 | const RATE_LIMIT = "RateLimit"; |
||
124 | |||
125 | /** |
||
126 | * [AUTH_GRAVITY description] |
||
127 | * @var integer |
||
128 | */ |
||
129 | const AUTH_GRAVITY = 0b100; |
||
130 | const AUTH_PASSIVE = 0b010; |
||
131 | const AUTH_FINAL = 0b001; |
||
132 | |||
133 | /** |
||
134 | * [__construct This is the part of the code that takes care of all |
||
135 | * authentiations. allowing you to focus on building wonderful things at REST. |
||
136 | * pun intended ;-)] |
||
137 | * @param array|null $params Initialization parameters from the Slint system. |
||
138 | * There's no use for this arg yet. |
||
139 | */ |
||
140 | function __construct(?array $params=null) |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * [authenticate description] |
||
209 | * @date 2020-01-30 |
||
210 | */ |
||
211 | private function authenticate():void |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * [process_auth description] |
||
239 | * @date 2020-04-07 |
||
240 | * @param string $auth [description] |
||
241 | * @param int $flags [description] |
||
242 | */ |
||
243 | private function process_auth(string &$auth, int $flags):void |
||
253 | } |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * [auth_proceed description] |
||
258 | * @date 2020-04-07 |
||
259 | * @param bool $success [description] |
||
260 | * @param int $flags [description] |
||
261 | * @return bool [description] |
||
262 | */ |
||
263 | private function auth_proceed(bool $success, int $flags):bool |
||
264 | { |
||
265 | if ($flags & self::AUTH_GRAVITY) return $success; |
||
266 | if ($success) { |
||
267 | if ($flags & self::AUTH_FINAL) { |
||
268 | $this->authPreempted = true; |
||
269 | return true; |
||
270 | } |
||
271 | } else { |
||
272 | return $flags & self::AUTH_PASSIVE ? true : false; |
||
273 | } |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * [ip_auth description] |
||
278 | * @date 2020-04-07 |
||
279 | * @param int $flags [description] |
||
280 | */ |
||
281 | private function ip_auth(int $flags):void |
||
285 | } |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * [bearer_auth description] |
||
290 | * @date 2020-04-07 |
||
291 | * @param string $auth [description] |
||
292 | * @param int $flags [description] |
||
293 | */ |
||
294 | private function bearer_auth(string $auth, int $flags):void |
||
314 | } |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * [basic_auth description] |
||
319 | * @date 2020-04-07 |
||
320 | * @param int $flags [description] |
||
321 | */ |
||
322 | private function basic_auth(int $flags):void |
||
328 | } |
||
329 | /** |
||
330 | * [api_key_auth description] |
||
331 | */ |
||
332 | private function api_key_auth(int $flags=self::AUTH_GRAVITY):void |
||
333 | { |
||
334 | if (uri_string() == '') return; |
||
335 | $shouldProceed = $this->auth_proceed(false, $flags); |
||
336 | |||
337 | if (!$this->ci->input->get_request_header($this->apiKeyHeader, true) && !$shouldProceed) { |
||
338 | // if (!isset($_SERVER['HTTP_' . str_replace("-", "_", $this->apiKeyHeader)])) { |
||
339 | $this->handle_response(RESTResponse::BAD_REQUEST, RESTAuth::API_KEY); // Exits. |
||
340 | } |
||
341 | |||
342 | $apiKey = $this->rest_model->getAPIKeyData( |
||
343 | $this->ci->input->get_request_header($this->apiKeyHeader, true) |
||
344 | ); |
||
345 | |||
346 | if ($apiKey == null && !$shouldProceed) { |
||
347 | $this->handle_response(RESTResponse::UN_AUTHORIZED, RESTAuth::API_KEY); // Exits. |
||
348 | } |
||
349 | |||
350 | $this->apiKey = $apiKey; |
||
351 | |||
352 | if (!$this->auth_proceed(true, $flags)) return; |
||
353 | |||
354 | // ==== API KEY Auth Passed ==== // |
||
355 | |||
356 | if ($this->limit_api && $this->api_key_limit_column != null && $apiKey->{$this->api_key_limit_column} == 1) { |
||
357 | $this->limitAPIKey($apiKey->{$this->api_key_column}); |
||
358 | } |
||
359 | |||
360 | $this->checked_rate_limit = true; // Ignore Limit By IP. |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * [limitAPIKey description] |
||
365 | * @date 2020-04-08 |
||
366 | * @param string $apiKey [description] |
||
367 | */ |
||
368 | public function limitAPIKey(string $apiKey):void |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * [api_rest_limit_by_ip_address description] |
||
406 | * TODO: Implement. |
||
407 | */ |
||
408 | private function api_rest_limit_by_ip_address():void |
||
437 | } |
||
438 | } |
||
439 | } |
||
440 | /** |
||
441 | * [custom_auth description] |
||
442 | * @param string $auth [description] |
||
443 | */ |
||
444 | private function custom_auth(string &$auth):void |
||
457 | } |
||
458 | } |
||
459 | /** |
||
460 | * [get_authorization_header description] |
||
461 | * @return [type] [description] |
||
462 | */ |
||
463 | private function get_authorization_header():?string |
||
464 | { |
||
465 | if (isset($_SERVER['Authorization'])) { |
||
466 | return trim($_SERVER["Authorization"]); |
||
467 | } else if (isset($_SERVER['HTTP_AUTHORIZATION'])) { //Nginx or fast CGI |
||
468 | return trim($_SERVER["HTTP_AUTHORIZATION"]); |
||
469 | } elseif (function_exists('apache_request_headers')) { |
||
470 | $requestHeaders = apache_request_headers(); |
||
471 | |||
472 | // Avoid Surprises. |
||
473 | $requestHeaders = array_combine(array_map('ucwords', array_keys($requestHeaders)), array_values($requestHeaders)); |
||
474 | |||
475 | if (isset($requestHeaders['Authorization'])) { |
||
476 | return trim($requestHeaders['Authorization']); |
||
477 | } |
||
478 | } |
||
479 | return null; |
||
480 | } |
||
481 | |||
482 | /** |
||
483 | * [handle_response description] |
||
484 | * @param int $code [description] |
||
485 | */ |
||
486 | private function handle_response(int $code, $auth=null, ?string $errorReason=null):void |
||
495 | } |
||
496 | } |
||
498 |