| Conditions | 28 |
| Paths | 41 |
| Total Lines | 141 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 109 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 110 | { |
||
| 111 | if (!$this->canHandleRequest($request)) { |
||
| 112 | return $handler->handle($request); |
||
| 113 | } |
||
| 114 | |||
| 115 | $controllerName = $request->getQueryParams()['install']['controller'] ?? 'layout'; |
||
| 116 | $actionName = $request->getParsedBody()['install']['action'] ?? $request->getQueryParams()['install']['action'] ?? 'init'; |
||
| 117 | |||
| 118 | if ($actionName === 'showEnableInstallToolFile' && EnableFileService::isInstallToolEnableFilePermanent()) { |
||
| 119 | $actionName = 'showLogin'; |
||
| 120 | } |
||
| 121 | |||
| 122 | $action = $actionName . 'Action'; |
||
| 123 | |||
| 124 | // not session related actions |
||
| 125 | if ($actionName === 'init') { |
||
| 126 | $controller = $this->container->get(LayoutController::class); |
||
| 127 | return $controller->initAction($request); |
||
| 128 | } |
||
| 129 | if ($actionName === 'checkEnableInstallToolFile') { |
||
| 130 | return new JsonResponse([ |
||
| 131 | 'success' => $this->checkEnableInstallToolFile(), |
||
| 132 | ]); |
||
| 133 | } |
||
| 134 | if ($actionName === 'showEnableInstallToolFile') { |
||
| 135 | $controller = $this->container->get(LoginController::class); |
||
| 136 | return $controller->showEnableInstallToolFileAction($request); |
||
| 137 | } |
||
| 138 | if ($actionName === 'showLogin') { |
||
| 139 | if (!$this->checkEnableInstallToolFile()) { |
||
| 140 | throw new \RuntimeException('Not authorized', 1505564888); |
||
| 141 | } |
||
| 142 | $controller = $this->container->get(LoginController::class); |
||
| 143 | return $controller->showLoginAction($request); |
||
| 144 | } |
||
| 145 | |||
| 146 | // session related actions |
||
| 147 | $session = new SessionService(); |
||
| 148 | if ($actionName === 'preAccessCheck') { |
||
| 149 | $response = new JsonResponse([ |
||
| 150 | 'installToolLocked' => !$this->checkEnableInstallToolFile(), |
||
| 151 | 'isAuthorized' => $session->isAuthorized() |
||
| 152 | ]); |
||
| 153 | } elseif ($actionName === 'checkLogin') { |
||
| 154 | if (!$this->checkEnableInstallToolFile() && !$session->isAuthorizedBackendUserSession()) { |
||
| 155 | throw new \RuntimeException('Not authorized', 1505563556); |
||
| 156 | } |
||
| 157 | if ($session->isAuthorized()) { |
||
| 158 | $session->refreshSession(); |
||
| 159 | $response = new JsonResponse([ |
||
| 160 | 'success' => true, |
||
| 161 | ]); |
||
| 162 | } else { |
||
| 163 | // Session expired, log out user, start new session |
||
| 164 | $session->resetSession(); |
||
| 165 | $session->startSession(); |
||
| 166 | $response = new JsonResponse([ |
||
| 167 | 'success' => false, |
||
| 168 | ]); |
||
| 169 | } |
||
| 170 | } elseif ($actionName === 'login') { |
||
| 171 | $session->initializeSession(); |
||
| 172 | if (!$this->checkEnableInstallToolFile()) { |
||
| 173 | throw new \RuntimeException('Not authorized', 1505567462); |
||
| 174 | } |
||
| 175 | $this->checkSessionToken($request, $session); |
||
| 176 | $this->checkSessionLifetime($session); |
||
| 177 | $password = $request->getParsedBody()['install']['password'] ?? null; |
||
| 178 | $authService = new AuthenticationService($session); |
||
| 179 | if ($authService->loginWithPassword($password, $request)) { |
||
| 180 | $response = new JsonResponse([ |
||
| 181 | 'success' => true, |
||
| 182 | ]); |
||
| 183 | } else { |
||
| 184 | if ($password === null || empty($password)) { |
||
| 185 | $messageQueue = (new FlashMessageQueue('install'))->enqueue( |
||
| 186 | new FlashMessage('Please enter the install tool password', '', FlashMessage::ERROR) |
||
| 187 | ); |
||
| 188 | } else { |
||
| 189 | $hashInstance = $this->passwordHashFactory->getDefaultHashInstance('BE'); |
||
| 190 | $hashedPassword = $hashInstance->getHashedPassword($password); |
||
| 191 | $messageQueue = (new FlashMessageQueue('install'))->enqueue( |
||
| 192 | new FlashMessage( |
||
| 193 | 'Given password does not match the install tool login password. Calculated hash: ' . $hashedPassword, |
||
| 194 | '', |
||
| 195 | FlashMessage::ERROR |
||
| 196 | ) |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | $response = new JsonResponse([ |
||
| 200 | 'success' => false, |
||
| 201 | 'status' => $messageQueue, |
||
| 202 | ]); |
||
| 203 | } |
||
| 204 | } elseif ($actionName === 'logout') { |
||
| 205 | if (EnableFileService::installToolEnableFileExists() && !EnableFileService::isInstallToolEnableFilePermanent()) { |
||
| 206 | EnableFileService::removeInstallToolEnableFile(); |
||
| 207 | } |
||
| 208 | $formProtection = FormProtectionFactory::get( |
||
| 209 | InstallToolFormProtection::class |
||
| 210 | ); |
||
| 211 | $formProtection->clean(); |
||
| 212 | $session->destroySession(); |
||
| 213 | $response = new JsonResponse([ |
||
| 214 | 'success' => true, |
||
| 215 | ]); |
||
| 216 | } else { |
||
| 217 | $enforceReferrerResponse = $this->enforceReferrer($request); |
||
| 218 | if ($enforceReferrerResponse instanceof ResponseInterface) { |
||
|
|
|||
| 219 | return $enforceReferrerResponse; |
||
| 220 | } |
||
| 221 | $session->initializeSession(); |
||
| 222 | if ( |
||
| 223 | !$this->checkSessionToken($request, $session) |
||
| 224 | || !$this->checkSessionLifetime($session) |
||
| 225 | || !$session->isAuthorized() |
||
| 226 | ) { |
||
| 227 | return new HtmlResponse('', 403); |
||
| 228 | } |
||
| 229 | $session->refreshSession(); |
||
| 230 | if (!array_key_exists($controllerName, $this->controllers)) { |
||
| 231 | throw new \RuntimeException( |
||
| 232 | 'Unknown controller ' . $controllerName, |
||
| 233 | 1505215756 |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | $this->recreatePackageStatesFileIfMissing(); |
||
| 237 | $className = $this->controllers[$controllerName]; |
||
| 238 | /** @var AbstractController $controller */ |
||
| 239 | $controller = $this->container->get($className); |
||
| 240 | if (!method_exists($controller, $action)) { |
||
| 241 | throw new \RuntimeException( |
||
| 242 | 'Unknown action method ' . $action . ' in controller ' . $controllerName, |
||
| 243 | 1505216027 |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | $response = $controller->$action($request); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $response; |
||
| 250 | } |
||
| 383 |