| Conditions | 24 |
| Paths | 7688 |
| Total Lines | 92 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 285 | private function httpHandler() |
||
| 286 | { |
||
| 287 | // Instantiate Http Router Service |
||
| 288 | $this->services->load(Reactor\Http\Router::class); |
||
| 289 | |||
| 290 | if (profiler() !== false) { |
||
| 291 | profiler()->watch('Parse Router Request'); |
||
| 292 | } |
||
| 293 | router()->parseRequest(); |
||
| 294 | |||
| 295 | // Instantiate Http Middleware Service |
||
| 296 | $this->services->load(Reactor\Http\Middleware::class); |
||
| 297 | |||
| 298 | if (profiler() !== false) { |
||
| 299 | profiler()->watch('Running Middleware Service: Pre Controller'); |
||
| 300 | } |
||
| 301 | middleware()->run(); |
||
| 302 | |||
| 303 | if (false !== ($controller = $this->services->get('controller'))) { |
||
| 304 | if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) { |
||
| 305 | // Autoload Model |
||
| 306 | $modelClassName = str_replace('Controllers', 'Models', $controller->getName()); |
||
| 307 | |||
| 308 | if (class_exists($modelClassName)) { |
||
| 309 | $this->models->register($modelClassName, 'controller'); |
||
| 310 | } |
||
| 311 | |||
| 312 | // Initialize Controller |
||
| 313 | if (profiler() !== false) { |
||
| 314 | profiler()->watch('Calling Hooks Service: Pre Controller'); |
||
| 315 | } |
||
| 316 | |||
| 317 | if (profiler() !== false) { |
||
| 318 | profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
||
| 319 | } |
||
| 320 | $requestController = $controller->getInstance(); |
||
| 321 | |||
| 322 | if (method_exists($requestController, '__reconstruct')) { |
||
| 323 | $requestController->__reconstruct(); |
||
| 324 | } elseif (method_exists($requestController, 'initialize')) { |
||
| 325 | $requestController->initialize(); |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->services->add($requestController, 'controller'); |
||
| 329 | |||
| 330 | if (profiler() !== false) { |
||
| 331 | profiler()->watch('Calling Middleware Service: Post Controller'); |
||
| 332 | } |
||
| 333 | |||
| 334 | $requestMethod = $controller->getRequestMethod(); |
||
| 335 | $requestMethodArgs = $controller->getRequestMethodArgs(); |
||
| 336 | |||
| 337 | // Call the requested controller method |
||
| 338 | if (profiler() !== false) { |
||
| 339 | profiler()->watch('Execute Requested Controller Method'); |
||
| 340 | } |
||
| 341 | ob_start(); |
||
| 342 | $requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
||
| 343 | |||
| 344 | if (empty($requestControllerOutput)) { |
||
| 345 | $requestControllerOutput = ob_get_contents(); |
||
| 346 | ob_end_clean(); |
||
| 347 | } |
||
| 348 | |||
| 349 | if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
||
| 350 | // Send default error 204 - No Content |
||
| 351 | output()->sendError(204); |
||
| 352 | } elseif (is_bool($requestControllerOutput)) { |
||
| 353 | if ($requestControllerOutput === true) { |
||
| 354 | output()->sendError(200); |
||
| 355 | } elseif ($requestControllerOutput === false) { |
||
| 356 | output()->sendError(204); |
||
| 357 | } |
||
| 358 | } elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
||
| 359 | $requestController->sendPayload($requestControllerOutput); |
||
| 360 | } elseif (is_numeric($requestControllerOutput)) { |
||
| 361 | output()->sendError($requestControllerOutput); |
||
| 362 | } elseif (is_string($requestControllerOutput)) { |
||
| 363 | if (is_json($requestControllerOutput)) { |
||
| 364 | output()->setContentType('application/json'); |
||
| 365 | output()->send($requestControllerOutput); |
||
| 366 | } elseif (is_serialized($requestControllerOutput)) { |
||
| 367 | output()->send($requestControllerOutput); |
||
| 368 | } else { |
||
| 369 | output()->send($requestControllerOutput); |
||
| 370 | } |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | |||
| 375 | // Show Error (404) Page Not Found |
||
| 376 | output()->sendError(404); |
||
| 377 | } |
||
| 379 |