Total Complexity | 56 |
Total Lines | 409 |
Duplicated Lines | 0 % |
Changes | 16 | ||
Bugs | 1 | Features | 0 |
Complex classes like header 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 header, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class header extends server implements interfaces\optionsInterface |
||
25 | { |
||
26 | use \responsible\core\traits\optionsTrait; |
||
27 | |||
28 | /** |
||
29 | * Max age constant |
||
30 | */ |
||
31 | const MAX_WINDOW = 3600; |
||
32 | |||
33 | /** |
||
34 | * [$REQUEST_APPLICATION] |
||
35 | * @var array |
||
36 | */ |
||
37 | private $REQUEST_APPLICATION = array( |
||
38 | 'xml' => 'text/xml', |
||
39 | 'json' => 'application/json', |
||
40 | 'html' => 'text/html', |
||
41 | 'array' => 'text/plain', |
||
42 | 'object' => 'text/plain', |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * [$REQUEST_TYPE / Default is json] |
||
47 | * @var string |
||
48 | */ |
||
49 | private $REQUEST_TYPE; |
||
50 | |||
51 | /** |
||
52 | * [$REQUEST_METHOD] |
||
53 | * @var array |
||
54 | */ |
||
55 | private $REQUEST_METHOD = []; |
||
56 | |||
57 | /** |
||
58 | * [$headerAuth Header authorise class object] |
||
59 | * @var object |
||
60 | */ |
||
61 | protected $headerAuth; |
||
62 | |||
63 | /** |
||
64 | * [__construct] |
||
65 | */ |
||
66 | public function __construct() |
||
67 | {} |
||
68 | |||
69 | /** |
||
70 | * [requestType] |
||
71 | * @return void |
||
72 | */ |
||
73 | public function requestType($type = 'json') |
||
74 | { |
||
75 | $this->REQUEST_TYPE = $type; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * [getRequestType] |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getRequestType() |
||
83 | { |
||
84 | return $this->REQUEST_TYPE; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * [requestMethod Set and return the request method] |
||
89 | * @return object |
||
90 | */ |
||
91 | public function requestMethod() |
||
92 | { |
||
93 | switch (strtolower($_SERVER['REQUEST_METHOD'])) { |
||
94 | |||
95 | case 'get': |
||
96 | $this->REQUEST_METHOD = ['method' => 'get', 'data' => $_GET]; |
||
97 | break; |
||
98 | |||
99 | case 'post': |
||
100 | $_POST_DATA = $_POST; |
||
101 | $jsonData = json_decode(file_get_contents("php://input")); |
||
102 | |||
103 | if (is_object($jsonData) || is_array($jsonData)) { |
||
104 | $_POST_DATA = json_decode(file_get_contents("php://input"), true); |
||
105 | } |
||
106 | $_POST = array_merge($_REQUEST, $_POST); |
||
107 | $_REQUEST = array_merge($_POST, $_POST_DATA); |
||
108 | |||
109 | $this->REQUEST_METHOD = ['method' => 'post', 'data' => $_REQUEST]; |
||
110 | break; |
||
111 | |||
112 | case 'options': |
||
113 | $this->REQUEST_METHOD = ['method' => 'options', 'data' => $_POST]; |
||
114 | echo json_encode(['success'=>true]); |
||
115 | $this->setHeaders(); |
||
116 | exit; |
||
|
|||
117 | break; |
||
118 | |||
119 | case 'put': |
||
120 | parse_str(file_get_contents("php://input"), $_PUT); |
||
121 | |||
122 | foreach ($_PUT as $key => $value) { |
||
123 | unset($_PUT[$key]); |
||
124 | $_PUT[str_replace('amp;', '', $key)] = $value; |
||
125 | } |
||
126 | |||
127 | $_REQUEST = array_merge($_REQUEST, $_PUT); |
||
128 | |||
129 | $this->REQUEST_METHOD = ['method' => 'put', 'data' => $_REQUEST]; |
||
130 | break; |
||
131 | |||
132 | case 'patch': |
||
133 | # [TODO] |
||
134 | $this->REQUEST_METHOD = ['method' => 'patch', 'data' => []]; |
||
135 | break; |
||
136 | |||
137 | case 'delete': |
||
138 | # [TODO] |
||
139 | $this->REQUEST_METHOD = ['method' => 'delete', 'data' => []]; |
||
140 | break; |
||
141 | |||
142 | default: |
||
143 | $this->REQUEST_METHOD = []; |
||
144 | break; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * [getMethod Get the request method] |
||
150 | * @return object |
||
151 | */ |
||
152 | public function getMethod() |
||
153 | { |
||
154 | return (object) $this->REQUEST_METHOD; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * [setAllowedMethods Set the allowed methods for endpoints] |
||
159 | * @param array $methods [GET, POST, PUT, PATCH, DELETE, ect..] |
||
160 | */ |
||
161 | public function setAllowedMethods(array $methods) |
||
162 | { |
||
163 | $this->setHeader('Access-Control-Allow-Methods', array( |
||
164 | implode(',', $methods), |
||
165 | )); |
||
166 | |||
167 | $requestMethod = $this->getServerMethod(); |
||
168 | if (!in_array($requestMethod, $methods)) { |
||
169 | (new exception\errorException)->error('METHOD_NOT_ALLOWED'); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * [getMethod Get the request method] |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getServerMethod() |
||
178 | { |
||
179 | if (!isset($_SERVER['REQUEST_METHOD'])) { |
||
180 | return ''; |
||
181 | } |
||
182 | return $_SERVER['REQUEST_METHOD']; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * [getHeaders List all headers Server headers and Apache headers] |
||
187 | * @return array |
||
188 | */ |
||
189 | public function getHeaders() |
||
190 | { |
||
191 | $headers_list = headers_list(); |
||
192 | foreach ($headers_list as $index => $headValue) { |
||
193 | @list($key, $value) = explode(": ", $headValue); |
||
194 | |||
195 | if (!is_null($key) && !is_null($value) ) { |
||
196 | $headers_list[$key] = $value; |
||
197 | unset($headers_list[$index]); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | if (!function_exists('apache_request_headers')) { |
||
202 | $apacheRequestHeaders = $this->apacheRequestHeaders(); |
||
203 | } else { |
||
204 | $apacheRequestHeaders = apache_request_headers(); |
||
205 | } |
||
206 | |||
207 | if( is_null($apacheRequestHeaders) || empty($apacheRequestHeaders) ) { |
||
208 | return []; |
||
209 | } |
||
210 | |||
211 | $apache_headers = array_merge($headers_list, $apacheRequestHeaders); |
||
212 | |||
213 | $headers = array(); |
||
214 | |||
215 | foreach ($_SERVER as $key => $value) { |
||
216 | if (substr($key, 0, 5) != 'HTTP_') { |
||
217 | continue; |
||
218 | } |
||
219 | $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5))))); |
||
220 | $headers[$header] = $value; |
||
221 | } |
||
222 | |||
223 | return array_merge($headers, $apache_headers); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * [setHeader Append aditional headers] |
||
228 | * @return void |
||
229 | */ |
||
230 | public function setHeader($header, $headerValue = array(), $status = '', $delimiter = ';') |
||
231 | { |
||
232 | $header = trim(str_replace(':', '', $header)) . ': '; |
||
233 | $headerValue = implode($delimiter . ' ', $headerValue); |
||
234 | |||
235 | header($header . $status . $headerValue); |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * [setHeaders Default headers] |
||
240 | * @return void |
||
241 | */ |
||
242 | public function setHeaders() |
||
243 | { |
||
244 | $application = 'json'; |
||
245 | if (isset($this->REQUEST_APPLICATION[$this->getRequestType()])) { |
||
246 | $application = $this->REQUEST_APPLICATION[$this->getRequestType()]; |
||
247 | } |
||
248 | |||
249 | $this->setHeader('Content-Type', array( |
||
250 | $application, 'charset=UTF-8', |
||
251 | )); |
||
252 | |||
253 | $this->setHeader('Accept-Ranges', array( |
||
254 | 'bytes', |
||
255 | )); |
||
256 | |||
257 | $this->setHeader('Access-Control-Allow-Credentials', array( |
||
258 | true, |
||
259 | )); |
||
260 | |||
261 | $this->setHeader('Access-Control-Allow-Origin', array( |
||
262 | '*', |
||
263 | )); |
||
264 | |||
265 | if( !array_key_exists('Access-Control-Allow-Methods', $this->getHeaders()) ) { |
||
266 | $this->setHeader('Access-Control-Allow-Methods', array( |
||
267 | 'GET,POST,OPTIONS', |
||
268 | )); |
||
269 | } |
||
270 | |||
271 | $this->setHeader('Access-Control-Expose-Headers', array( |
||
272 | 'Content-Range', |
||
273 | )); |
||
274 | |||
275 | $this->setHeader('Access-Control-Allow-Headers', array( |
||
276 | 'origin,x-requested-with,Authorization,cache-control', |
||
277 | )); |
||
278 | |||
279 | $this->setHeader('Access-Control-Max-Age', array( |
||
280 | $this->getMaxWindow(), |
||
281 | )); |
||
282 | |||
283 | $this->setHeader('Expires', array( |
||
284 | 'Wed, 20 September 1978 00:00:00 GMT', |
||
285 | )); |
||
286 | |||
287 | $this->setHeader('Cache-Control', array( |
||
288 | 'no-store, no-cache, must-revalidate', |
||
289 | )); |
||
290 | |||
291 | $this->setHeader('Cache-Control', array( |
||
292 | 'post-check=0, pre-check=0', |
||
293 | )); |
||
294 | |||
295 | $this->setHeader('Pragma', array( |
||
296 | 'no-cache', |
||
297 | )); |
||
298 | |||
299 | $this->setHeader('X-Content-Type-Options', array( |
||
300 | 'nosniff', |
||
301 | )); |
||
302 | |||
303 | $this->setHeader('X-XSS-Protection', array( |
||
304 | '1', 'mode=block', |
||
305 | )); |
||
306 | |||
307 | if (isset($this->getOptions()['addHeaders']) && |
||
308 | (is_array($this->getOptions()['addHeaders']) && !empty($this->getOptions()['addHeaders'])) |
||
309 | ) { |
||
310 | foreach ($this->getOptions()['addHeaders'] as $i => $customHeader) { |
||
311 | if (is_array($customHeader) && sizeof($customHeader) == 2) { |
||
312 | $this->setHeader($customHeader[0], array( |
||
313 | $customHeader[1], |
||
314 | )); |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * [apacheRequestHeaders Native replacment fuction] |
||
322 | * https://www.php.net/manual/en/function.apache-request-headers.php#70810 |
||
323 | * @return array |
||
324 | */ |
||
325 | public function apacheRequestHeaders() |
||
326 | { |
||
327 | $arh = array(); |
||
328 | $rx_http = '/\AHTTP_/'; |
||
329 | |||
330 | foreach ($_SERVER as $key => $val) { |
||
331 | if (preg_match($rx_http, $key)) { |
||
332 | $arh_key = preg_replace($rx_http, '', $key); |
||
333 | $rx_matches = explode('_', $arh_key); |
||
334 | if (count($rx_matches) > 0 and strlen($arh_key) > 2) { |
||
335 | foreach ($rx_matches as $ak_key => $ak_val) { |
||
336 | $rx_matches[$ak_key] = ucfirst($ak_val); |
||
337 | } |
||
338 | |||
339 | $arh_key = implode('-', $rx_matches); |
||
340 | } |
||
341 | $arh[$arh_key] = $val; |
||
342 | } |
||
343 | } |
||
344 | return ($arh); |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * [headerAuth] |
||
349 | * @return object |
||
350 | */ |
||
351 | public function headerAuth() |
||
352 | { |
||
353 | if (is_null($this->headerAuth)) { |
||
354 | $this->headerAuth = new headerAuth; |
||
355 | } |
||
356 | $this->headerAuth->setOptions($this->getOptions()); |
||
357 | return $this->headerAuth; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * [authorizationHeaders Scan for "Authorization" header] |
||
362 | * @return string|array [mixed: string / error] |
||
363 | */ |
||
364 | public function authorizationHeaders($skipError = false) |
||
365 | { |
||
366 | return $this->headerAuth()->authorizationHeaders($skipError = false); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * [hasBearerToken Check if bearer token is present] |
||
371 | * @return string|null |
||
372 | */ |
||
373 | public function hasBearerToken() |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * [unauthorised Set an unauthorised header] |
||
380 | * @return array [exit exception message] |
||
381 | */ |
||
382 | public function unauthorised() |
||
383 | { |
||
384 | $this->headerAuth()->setUnauthorised(); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * [getMaxWindow Get the max control age window] |
||
389 | * @return integer |
||
390 | */ |
||
391 | private function getMaxWindow() |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * [setHeaderStatus] |
||
409 | * @param void |
||
410 | */ |
||
411 | public function setHeaderStatus($status) |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * [getHeaderStatus] |
||
418 | * @return integer |
||
419 | */ |
||
420 | public function getHeaderStatus() |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * [setData Set request method data] |
||
427 | * @param array $data |
||
428 | * @return void |
||
429 | */ |
||
430 | public function setData($data = []) |
||
433 | } |
||
434 | } |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.