| Total Complexity | 74 |
| Total Lines | 542 |
| Duplicated Lines | 0 % |
| Changes | 23 | ||
| 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 |
||
| 23 | class header extends server implements interfaces\optionsInterface |
||
| 24 | { |
||
| 25 | use \responsible\core\traits\optionsTrait; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Max age constant |
||
| 29 | */ |
||
| 30 | private const MAX_WINDOW = 3600; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * [$REQUEST_APPLICATION] |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | private $REQUEST_APPLICATION = array( |
||
| 37 | 'xml' => 'text/xml', |
||
| 38 | 'json' => 'application/json', |
||
| 39 | 'html' => 'text/html', |
||
| 40 | 'array' => 'text/plain', |
||
| 41 | 'object' => 'text/plain', |
||
| 42 | ); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * [$REQUEST_TYPE / Default is json] |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $REQUEST_TYPE = 'json'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * [$REQUEST_METHOD] |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $REQUEST_METHOD = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * [$headerAuth Header authorise class object] |
||
| 58 | * @var object |
||
| 59 | */ |
||
| 60 | protected $headerAuth; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Options can set additional CORS headers |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | private $additionalCORSHeaders = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * [__construct] |
||
| 70 | */ |
||
| 71 | public function __construct() |
||
| 72 | { |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * [requestType] |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function requestType($type = 'json') |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * [getRequestType] |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getRequestType() |
||
| 89 | { |
||
| 90 | return $this->REQUEST_TYPE; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * [requestMethod Set and return the request method] |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | public function requestMethod() |
||
| 98 | { |
||
| 99 | $verbs = new headerVerbs(); |
||
| 100 | |||
| 101 | switch (strtolower($_SERVER['REQUEST_METHOD'])) { |
||
| 102 | case 'get': |
||
| 103 | $this->REQUEST_METHOD = $verbs->get(); |
||
| 104 | break; |
||
| 105 | |||
| 106 | case 'post': |
||
| 107 | $this->REQUEST_METHOD = $verbs->post(); |
||
| 108 | break; |
||
| 109 | |||
| 110 | case 'options': |
||
| 111 | $isOriginRequest = ($_SERVER['HTTP_ORIGIN']) ?? false; |
||
| 112 | $this->REQUEST_METHOD = $verbs->post(); |
||
| 113 | echo json_encode(['success' => true]); |
||
| 114 | $this->setHeaders($isOriginRequest); |
||
| 115 | exit; |
||
|
|
|||
| 116 | break; |
||
| 117 | |||
| 118 | case 'put': |
||
| 119 | $this->REQUEST_METHOD = $verbs->put(); |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 'patch': |
||
| 123 | $this->REQUEST_METHOD = $verbs->patch(); |
||
| 124 | break; |
||
| 125 | |||
| 126 | case 'delete': |
||
| 127 | $this->REQUEST_METHOD = $verbs->delete(); |
||
| 128 | break; |
||
| 129 | |||
| 130 | default: |
||
| 131 | $this->REQUEST_METHOD = []; |
||
| 132 | break; |
||
| 133 | } |
||
| 134 | return $this->REQUEST_METHOD; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * [getMethod Get the request method] |
||
| 139 | * @return object |
||
| 140 | */ |
||
| 141 | public function getMethod() |
||
| 142 | { |
||
| 143 | return (object) $this->REQUEST_METHOD; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * [getBody Get the post body] |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getBody(): array |
||
| 151 | { |
||
| 152 | if (isset($this->getMethod()->data) && !empty($this->getMethod()->data)) { |
||
| 153 | return $this->getMethod()->data; |
||
| 154 | } |
||
| 155 | return []; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * [setAllowedMethods Set the allowed methods for endpoints] |
||
| 160 | * @param array $methods [GET, POST, PUT, PATCH, DELETE, ect..] |
||
| 161 | */ |
||
| 162 | public function setAllowedMethods(array $methods) |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * [getMethod Get the request method] |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getServerMethod() |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * [getHeaders List all headers Server headers and Apache headers] |
||
| 190 | * @return array |
||
| 191 | */ |
||
| 192 | public function getHeaders() |
||
| 193 | { |
||
| 194 | $headers_list = $this->headersList(); |
||
| 195 | foreach ($headers_list as $index => $headValue) { |
||
| 196 | @list($key, $value) = explode(": ", $headValue); |
||
| 197 | |||
| 198 | if (!is_null($key) && !is_null($value)) { |
||
| 199 | $headers_list[$key] = $value; |
||
| 200 | unset($headers_list[$index]); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | if (!function_exists('apache_request_headers')) { |
||
| 205 | $apacheRequestHeaders = $this->apacheRequestHeaders(); |
||
| 206 | } else { |
||
| 207 | $apacheRequestHeaders = apache_request_headers(); |
||
| 208 | } |
||
| 209 | |||
| 210 | if (is_null($apacheRequestHeaders) || empty($apacheRequestHeaders)) { |
||
| 211 | return []; |
||
| 212 | } |
||
| 213 | |||
| 214 | $apache_headers = array_replace($headers_list, array_filter($apacheRequestHeaders)); |
||
| 215 | |||
| 216 | $headers = array(); |
||
| 217 | foreach ($_SERVER as $key => $value) { |
||
| 218 | if (substr($key, 0, 5) != 'HTTP_') { |
||
| 219 | continue; |
||
| 220 | } |
||
| 221 | $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5))))); |
||
| 222 | $headers[$header] = $value; |
||
| 223 | } |
||
| 224 | |||
| 225 | return array_merge($headers, $apache_headers); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * [headersList Get the default header list] |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | private function headersList() |
||
| 233 | { |
||
| 234 | /*$server = new server([], $this->getOptions()); |
||
| 235 | if ($isMockTest = $server->isMockTest()) { |
||
| 236 | return $this->apacheRequestHeaders(); |
||
| 237 | }*/ |
||
| 238 | |||
| 239 | return headers_list(); |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * [apacheRequestHeaders Native replacment fuction] |
||
| 244 | * https://www.php.net/manual/en/function.apache-request-headers.php#70810 |
||
| 245 | * @return array |
||
| 246 | */ |
||
| 247 | public function apacheRequestHeaders() |
||
| 248 | { |
||
| 249 | $arh = array(); |
||
| 250 | $rx_http = '/\AHTTP_/'; |
||
| 251 | |||
| 252 | foreach ($_SERVER as $key => $val) { |
||
| 253 | if (preg_match($rx_http, $key)) { |
||
| 254 | $arh_key = preg_replace($rx_http, '', $key); |
||
| 255 | $rx_matches = explode('_', $arh_key); |
||
| 256 | if (count($rx_matches) > 0 and strlen($arh_key) > 2) { |
||
| 257 | foreach ($rx_matches as $ak_key => $ak_val) { |
||
| 258 | $rx_matches[$ak_key] = ucfirst($ak_val); |
||
| 259 | } |
||
| 260 | |||
| 261 | $arh_key = implode('-', $rx_matches); |
||
| 262 | } |
||
| 263 | $arh[$arh_key] = $val; |
||
| 264 | } |
||
| 265 | } |
||
| 266 | return ($arh); |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * [setHeader Append aditional headers] |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public function setHeader($header, $headerValue = array(), $status = '', $delimiter = ';') |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * [setHeaders Default headers] |
||
| 283 | * @return void |
||
| 284 | */ |
||
| 285 | public function setHeaders($CORS = false) |
||
| 286 | { |
||
| 287 | $auth_headers = $this->getHeaders(); |
||
| 288 | if (!array_key_exists('Content-Type', $auth_headers)) { |
||
| 289 | $application = 'json'; |
||
| 290 | if (isset($this->REQUEST_APPLICATION[$this->getRequestType()])) { |
||
| 291 | $application = $this->REQUEST_APPLICATION[$this->getRequestType()]; |
||
| 292 | } |
||
| 293 | $this->setHeader('Content-Type', array( |
||
| 294 | $application, 'charset=UTF-8', |
||
| 295 | )); |
||
| 296 | } |
||
| 297 | |||
| 298 | $this->setHeader('Accept-Ranges', array( |
||
| 299 | 'bytes', |
||
| 300 | )); |
||
| 301 | |||
| 302 | if (is_null($this->getHeaderValue('Access-Control-Allow-Credentials'))) { |
||
| 303 | $this->setHeader('Access-Control-Allow-Credentials', array( |
||
| 304 | 'true', |
||
| 305 | )); |
||
| 306 | } |
||
| 307 | |||
| 308 | if (!array_key_exists('Access-Control-Allow-Methods', $auth_headers)) { |
||
| 309 | $this->setHeader('Access-Control-Allow-Methods', array( |
||
| 310 | 'GET,POST,DELETE', |
||
| 311 | )); |
||
| 312 | } |
||
| 313 | |||
| 314 | $this->setHeader('Access-Control-Expose-Headers', array( |
||
| 315 | 'Content-Range', |
||
| 316 | )); |
||
| 317 | |||
| 318 | $this->setHeader('Access-Control-Max-Age', array( |
||
| 319 | $this->getMaxWindow(), |
||
| 320 | )); |
||
| 321 | |||
| 322 | $this->setHeader('Expires', array( |
||
| 323 | 'Wed, 20 September 1978 00:00:00 GMT', |
||
| 324 | )); |
||
| 325 | |||
| 326 | $this->setHeader('Cache-Control', array( |
||
| 327 | 'no-store, no-cache, must-revalidate', |
||
| 328 | )); |
||
| 329 | |||
| 330 | $this->setHeader('Cache-Control', array( |
||
| 331 | 'post-check=0, pre-check=0', |
||
| 332 | )); |
||
| 333 | |||
| 334 | $this->setHeader('Pragma', array( |
||
| 335 | 'no-cache', |
||
| 336 | )); |
||
| 337 | |||
| 338 | $this->setHeader('X-Content-Type-Options', array( |
||
| 339 | 'nosniff', |
||
| 340 | )); |
||
| 341 | |||
| 342 | $this->setHeader('X-XSS-Protection', array( |
||
| 343 | '1', 'mode=block', |
||
| 344 | )); |
||
| 345 | |||
| 346 | if ($CORS) { |
||
| 347 | if ( |
||
| 348 | isset($this->getOptions()['aditionalCORSHeaders']) && |
||
| 349 | (is_array($this->getOptions()['aditionalCORSHeaders']) && |
||
| 350 | !empty($this->getOptions()['aditionalCORSHeaders'])) |
||
| 351 | ) { |
||
| 352 | $this->setAccessControllAllowedHeaders($this->getOptions()['aditionalCORSHeaders']); |
||
| 353 | } |
||
| 354 | |||
| 355 | if (!$this->hasHeader('Access-Control-Allow-Origin')) { |
||
| 356 | $origin = ($auth_headers['Origin']) ?? false; |
||
| 357 | $origin = ($origin) ? $auth_headers['Origin'] : '*'; |
||
| 358 | $this->setHeader('Access-Control-Allow-Origin', array( |
||
| 359 | $origin, |
||
| 360 | )); |
||
| 361 | } |
||
| 362 | |||
| 363 | $this->setHeader('Access-Control-Allow-Headers', array( |
||
| 364 | 'origin,x-requested-with,Authorization,cache-control,content-type,x-header-csrf,x-auth-token' |
||
| 365 | . $this->getAccessControllAllowedHeaders(), |
||
| 366 | )); |
||
| 367 | |||
| 368 | $this->setHeader('Access-Control-Allow-Methods', array( |
||
| 369 | 'GET,POST,OPTIONS', |
||
| 370 | )); |
||
| 371 | } |
||
| 372 | |||
| 373 | if ( |
||
| 374 | isset($this->getOptions()['addHeaders']) && |
||
| 375 | (is_array($this->getOptions()['addHeaders']) && !empty($this->getOptions()['addHeaders'])) |
||
| 376 | ) { |
||
| 377 | foreach ($this->getOptions()['addHeaders'] as $i => $customHeader) { |
||
| 378 | if (is_array($customHeader) && sizeof($customHeader) == 2) { |
||
| 379 | $this->setHeader($customHeader[0], array( |
||
| 380 | $customHeader[1], |
||
| 381 | )); |
||
| 382 | } |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * [headerAuth] |
||
| 389 | * @return object |
||
| 390 | */ |
||
| 391 | public function headerAuth() |
||
| 392 | { |
||
| 393 | if (is_null($this->headerAuth)) { |
||
| 394 | $this->headerAuth = new headerAuth(); |
||
| 395 | } |
||
| 396 | $this->headerAuth->setOptions($this->getOptions()); |
||
| 397 | return $this->headerAuth; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * [authorizationHeaders Scan for "Authorization" header] |
||
| 402 | * @return string|array [mixed: string / error] |
||
| 403 | */ |
||
| 404 | public function authorizationHeaders($skipError = false) |
||
| 405 | { |
||
| 406 | return $this->headerAuth()->authorizationHeaders($skipError); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * [hasBearerToken Check if bearer token is present] |
||
| 411 | * @return string|null |
||
| 412 | */ |
||
| 413 | public function hasBearerToken() |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * [unauthorised Set an unauthorised header] |
||
| 420 | */ |
||
| 421 | public function unauthorised() |
||
| 422 | { |
||
| 423 | $this->headerAuth()->setUnauthorised(); |
||
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * [getMaxWindow Get the max control age window] |
||
| 428 | * @return integer |
||
| 429 | */ |
||
| 430 | private function getMaxWindow() |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * [setHeaderStatus] |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | public function setHeaderStatus($status) |
||
| 452 | { |
||
| 453 | http_response_code($status); |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * [getHeaderStatus] |
||
| 458 | * @return integer |
||
| 459 | */ |
||
| 460 | public function getHeaderStatus() |
||
| 461 | { |
||
| 462 | return http_response_code(); |
||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * [setData Set request method data] |
||
| 467 | * @param array $data |
||
| 468 | * @return void |
||
| 469 | */ |
||
| 470 | public function setData($data = []) |
||
| 471 | { |
||
| 472 | $this->REQUEST_METHOD['data'] = $data; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Set additional CORS control headers |
||
| 477 | * |
||
| 478 | * @param array |
||
| 479 | */ |
||
| 480 | private function setAccessControllAllowedHeaders(array $allowedHeaders): array |
||
| 481 | { |
||
| 482 | $this->additionalCORSHeaders = $allowedHeaders; |
||
| 483 | return $this->additionalCORSHeaders; |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Get a list of additional CORS control headers set by the config |
||
| 488 | * |
||
| 489 | * @return string |
||
| 490 | */ |
||
| 491 | private function getAccessControllAllowedHeaders(): string |
||
| 492 | { |
||
| 493 | if (empty($this->additionalCORSHeaders)) { |
||
| 494 | return ''; |
||
| 495 | } |
||
| 496 | |||
| 497 | return ',' . implode(',', $this->additionalCORSHeaders); |
||
| 498 | } |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Check if a specific header is present |
||
| 502 | * |
||
| 503 | * @param string $propertyName |
||
| 504 | * @param string $delimiter |
||
| 505 | * @return bool |
||
| 506 | */ |
||
| 507 | public function hasHeader($propertyName, $delimiter = '_'): bool |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Try get the header value |
||
| 514 | * |
||
| 515 | * @param string $propertyName |
||
| 516 | * @return string|null |
||
| 517 | */ |
||
| 518 | public function getHeaderValue(string $propertyName, $delimiter = '_'): ?string |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Normalize the property name in 6 formats |
||
| 535 | * |
||
| 536 | * @param string $propertyName |
||
| 537 | * @param string $delimiter |
||
| 538 | * @return array |
||
| 539 | */ |
||
| 540 | private function normalize(string $propertyName, string $delimiter) |
||
| 565 | } |
||
| 566 | } |
||
| 567 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.