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