| Conditions | 17 |
| Paths | 77 |
| Total Lines | 72 |
| Code Lines | 45 |
| 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 |
||
| 57 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 58 | { |
||
| 59 | if (!$this->canHandleRequest($request)) { |
||
| 60 | return $handler->handle($request); |
||
| 61 | } |
||
| 62 | |||
| 63 | // Lazy load InstallerController, to instantiate the class and the dependencies only if we handle an install request. |
||
| 64 | $controller = $this->container->get(InstallerController::class); |
||
| 65 | $actionName = $request->getParsedBody()['install']['action'] ?? $request->getQueryParams()['install']['action'] ?? 'init'; |
||
| 66 | $action = $actionName . 'Action'; |
||
| 67 | |||
| 68 | if ($actionName === 'init' || $actionName === 'mainLayout') { |
||
| 69 | $response = $controller->$action(); |
||
| 70 | } elseif ($actionName === 'checkInstallerAvailable') { |
||
| 71 | $response = new JsonResponse([ |
||
| 72 | 'success' => $this->isInstallerAvailable(), |
||
| 73 | ]); |
||
| 74 | } elseif ($actionName === 'showInstallerNotAvailable') { |
||
| 75 | $response = $controller->showInstallerNotAvailableAction(); |
||
| 76 | } elseif ($actionName === 'checkEnvironmentAndFolders' |
||
| 77 | || $actionName === 'showEnvironmentAndFolders' |
||
| 78 | || $actionName === 'executeEnvironmentAndFolders' |
||
| 79 | ) { |
||
| 80 | $this->throwIfInstallerIsNotAvailable(); |
||
| 81 | $response = $controller->$action($request); |
||
| 82 | } else { |
||
| 83 | $this->throwIfInstallerIsNotAvailable(); |
||
| 84 | // With main folder layout available, sessions can be handled |
||
| 85 | $session = new SessionService(); |
||
| 86 | if (!$session->hasSession()) { |
||
| 87 | $session->startSession(); |
||
| 88 | } |
||
| 89 | if ($session->isExpired()) { |
||
| 90 | $session->refreshSession(); |
||
| 91 | } |
||
| 92 | $postValues = $request->getParsedBody()['install']; |
||
| 93 | $sessionTokenOk = false; |
||
| 94 | if (empty($postValues)) { |
||
| 95 | // No post data is there, no token check necessary |
||
| 96 | $sessionTokenOk = true; |
||
| 97 | } |
||
| 98 | if (isset($postValues['token'])) { |
||
| 99 | // A token must be given as soon as there is POST data |
||
| 100 | $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class); |
||
| 101 | if ($actionName === '') { |
||
| 102 | throw new \RuntimeException('No POST action given for token check', 1505647681); |
||
| 103 | } |
||
| 104 | $sessionTokenOk = $formProtection->validateToken($postValues['token'], 'installTool', $actionName); |
||
| 105 | } |
||
| 106 | if (!$sessionTokenOk) { |
||
| 107 | $session->resetSession(); |
||
| 108 | $session->startSession(); |
||
| 109 | throw new \RuntimeException('Invalid session token', 1505647737); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!method_exists($controller, $action)) { |
||
| 113 | // Sanitize action method, preventing injecting whatever method name |
||
| 114 | throw new \RuntimeException( |
||
| 115 | 'Unknown action method ' . $action . ' in controller InstallerController', |
||
| 116 | 1505687700 |
||
| 117 | ); |
||
| 118 | } |
||
| 119 | |||
| 120 | $response = $controller->$action($request); |
||
| 121 | |||
| 122 | if ($actionName === 'executeDefaultConfiguration') { |
||
| 123 | // Executing last step cleans session |
||
| 124 | $session->destroySession(); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | return $response; |
||
| 129 | } |
||
| 168 |