| Total Complexity | 46 |
| Total Lines | 381 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Request 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 Request, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class Request |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Application Object |
||
| 11 | * |
||
| 12 | * @var \System\Application |
||
| 13 | */ |
||
| 14 | private $app; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Url |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | private $url; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Base Url |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $baseUrl; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Uploaded files container |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $files = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Host |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private $host; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | * |
||
| 47 | * @param \System\Application $app |
||
| 48 | */ |
||
| 49 | public function __construct(Application $app) |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Prepare url |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | public function prepareUrl() |
||
| 60 | { |
||
| 61 | $script = dirname($this->server('SCRIPT_NAME')); |
||
| 62 | |||
| 63 | $requestUri = $this->server('REQUEST_URI'); |
||
| 64 | |||
| 65 | if (strpos($requestUri, '?')) { |
||
| 66 | |||
| 67 | list($requestUri, $queryString) = explode('?', $requestUri); |
||
| 68 | } |
||
| 69 | |||
| 70 | $this->url = $this->cleanUrl($script, $requestUri); |
||
| 71 | |||
| 72 | $REQUEST_PROTOCOL = $this->isSecure() ? 'https' : 'http'; |
||
| 73 | |||
| 74 | $this->host = $REQUEST_PROTOCOL . '://' . $this->server('HTTP_HOST'); |
||
| 75 | |||
| 76 | $this->baseUrl = $this->host . $requestUri; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Clean url |
||
| 81 | * |
||
| 82 | * @param string $script |
||
| 83 | * @param string $default |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | private function cleanUrl($script, $requestUri) |
||
| 87 | { |
||
| 88 | if (!in_array($script, ['/', '\\'])) { |
||
| 89 | |||
| 90 | $url = preg_replace('#^' . $script . '#', '', $requestUri); |
||
| 91 | |||
| 92 | } else { |
||
| 93 | |||
| 94 | $url = $requestUri; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($url !== '/') { |
||
| 98 | |||
| 99 | $url = rtrim($url, '/'); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $url; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Check if the website is secure |
||
| 107 | * |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | private function isSecure() |
||
| 111 | { |
||
| 112 | if ($this->checkHttp() || $this->checkHttpXforwardedProto() || $this->checkHttpXforwardedSsl()) { |
||
| 113 | |||
| 114 | return true; |
||
| 115 | } |
||
| 116 | |||
| 117 | return false; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Check if HTTPS is 'on' |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | private function checkHttp() |
||
| 126 | { |
||
| 127 | if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { |
||
| 128 | |||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Check if HTTP_X_FORWARDED_PROTO is not empty or 'https' |
||
| 137 | * |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | private function checkHttpXforwardedProto() |
||
| 141 | { |
||
| 142 | if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') { |
||
| 143 | |||
| 144 | return true; |
||
| 145 | } |
||
| 146 | |||
| 147 | return false; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Check if HTTP_X_FORWARDED_SSL is 'on' |
||
| 152 | * |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | private function checkHttpXforwardedSsl() |
||
| 156 | { |
||
| 157 | if (!empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') { |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | return false; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get value from $_GET by the given key |
||
| 167 | * |
||
| 168 | * @param string $key |
||
| 169 | * @return mixed |
||
| 170 | */ |
||
| 171 | public function get($key) |
||
| 172 | { |
||
| 173 | $value = array_get($_GET, $key); |
||
| 174 | |||
| 175 | if (is_array($value)) { |
||
| 176 | |||
| 177 | $value = array_filter($value); |
||
| 178 | |||
| 179 | } else { |
||
| 180 | |||
| 181 | $value = trim($value); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $value; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Get value from $_POST by the given key |
||
| 189 | * |
||
| 190 | * @param string $key |
||
| 191 | * @return mixed |
||
| 192 | */ |
||
| 193 | public function post($key) |
||
| 194 | { |
||
| 195 | $value = array_get($_POST, $key); |
||
| 196 | |||
| 197 | if (is_array($value)) { |
||
| 198 | |||
| 199 | $value = array_filter($value); |
||
| 200 | |||
| 201 | } else { |
||
| 202 | |||
| 203 | $value = trim($value); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $value; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set value To $_POST For the given key |
||
| 211 | * |
||
| 212 | * @param string $key |
||
| 213 | * @param mixed $valuet |
||
| 214 | * @return mixed |
||
| 215 | */ |
||
| 216 | public function setPost($key, $value) |
||
| 217 | { |
||
| 218 | $_POST[$key] = $value; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get $_POST |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function posts() |
||
| 227 | { |
||
| 228 | return $_POST; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get $_GET |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function gets() |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get $_FILES |
||
| 243 | * |
||
| 244 | * @return array |
||
| 245 | */ |
||
| 246 | public function files() |
||
| 247 | { |
||
| 248 | return $_FILES; |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get the uploaded file object for the given input |
||
| 253 | * |
||
| 254 | * @param string $input |
||
| 255 | * @return System\Http\UploadeFile\ |
||
|
|
|||
| 256 | */ |
||
| 257 | public function file($input) |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get value from $_SERVER by the given key |
||
| 273 | * |
||
| 274 | * @param string $key |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | public function server($key) |
||
| 278 | { |
||
| 279 | return array_get($_SERVER, $key); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get current request method |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function method() |
||
| 288 | { |
||
| 289 | return $this->server('REQUEST_METHOD'); |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get the referer link |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function referer() |
||
| 298 | { |
||
| 299 | return $this->server('HTTP_REFERER'); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get full url of the script |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function baseUrl() |
||
| 308 | { |
||
| 309 | return $this->baseUrl; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Get only relative url (clean url) |
||
| 314 | * |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public function url() |
||
| 318 | { |
||
| 319 | return $this->url; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get only host |
||
| 324 | * |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function host() |
||
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Check if the request to the admin panel |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function isRequestToAdminManagement() |
||
| 338 | { |
||
| 339 | $url = $this->url; |
||
| 340 | |||
| 341 | $url = ltrim($url, '/'); |
||
| 342 | |||
| 343 | $url = explode('/', $url)[0]; |
||
| 344 | |||
| 345 | return $url == 'admin'; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function isMatchingRequestMethod($methods = ['GET']) |
||
| 369 | } |
||
| 370 | |||
| 371 | |||
| 372 | public function canRequestContinue($middlewares) |
||
| 373 | { |
||
| 374 | if (empty($middlewares)) { |
||
| 375 | |||
| 376 | return true; |
||
| 377 | } |
||
| 378 | |||
| 379 | foreach ($middlewares as $middleware) { |
||
| 388 | } |
||
| 389 | } |
||
| 390 |