| Conditions | 34 |
| Paths | 4829 |
| Total Lines | 137 |
| Code Lines | 93 |
| 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 |
||
| 52 | public function loginAction( |
||
| 53 | Request $request, |
||
| 54 | CurrentUserApiInterface $currentUserApi, |
||
| 55 | UserRepositoryInterface $userRepository, |
||
| 56 | AuthenticationMethodCollector $authenticationMethodCollector, |
||
| 57 | AccessHelper $accessHelper, |
||
| 58 | EventDispatcherInterface $eventDispatcher, |
||
| 59 | HookDispatcherInterface $hookDispatcher |
||
| 60 | ): Response { |
||
| 61 | if ($currentUserApi->isLoggedIn()) { |
||
| 62 | return $this->redirectToRoute('zikulausersmodule_account_menu'); |
||
| 63 | } |
||
| 64 | $returnUrl = $request->query->get('returnUrl', ''); |
||
| 65 | |||
| 66 | $session = $request->hasSession() ? $request->getSession() : null; |
||
| 67 | |||
| 68 | $selectedMethod = null !== $session ? $session->get('authenticationMethod') : ''; |
||
| 69 | $selectedMethod = $request->query->get('authenticationMethod', $selectedMethod); |
||
| 70 | if (empty($selectedMethod) && count($authenticationMethodCollector->getActiveKeys()) > 1) { |
||
| 71 | // there are multiple authentication methods available and none selected yet, so let the user choose one |
||
| 72 | return $this->render('@ZikulaUsersModule/Access/authenticationMethodSelector.html.twig', [ |
||
| 73 | 'collector' => $authenticationMethodCollector, |
||
| 74 | 'path' => 'zikulausersmodule_access_login' |
||
| 75 | ]); |
||
| 76 | } |
||
| 77 | if (empty($selectedMethod) && 1 === count($authenticationMethodCollector->getActiveKeys())) { |
||
| 78 | // there is only one authentication method available, so use this |
||
| 79 | $selectedMethod = $authenticationMethodCollector->getActiveKeys()[0]; |
||
| 80 | } |
||
| 81 | if (null !== $session) { |
||
| 82 | // save method to session for reEntrant needs |
||
| 83 | $session->set('authenticationMethod', $selectedMethod); |
||
| 84 | if (!empty($returnUrl)) { |
||
| 85 | // save returnUrl to session for reEntrant needs |
||
| 86 | $session->set('returnUrl', $returnUrl); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | $authenticationMethod = $authenticationMethodCollector->get($selectedMethod); |
||
| 91 | $rememberMe = false; |
||
| 92 | |||
| 93 | $loginHeader = $this->renderView('@ZikulaUsersModule/Access/loginHeader.html.twig'); |
||
| 94 | $loginFooter = $this->renderView('@ZikulaUsersModule/Access/loginFooter.html.twig'); |
||
| 95 | |||
| 96 | $form = null; |
||
| 97 | if ($authenticationMethod instanceof NonReEntrantAuthenticationMethodInterface) { |
||
| 98 | $form = $this->createForm($authenticationMethod->getLoginFormClassName()); |
||
| 99 | if (!$form->has('rememberme')) { |
||
| 100 | throw new InvalidAuthenticationMethodLoginFormException(); |
||
| 101 | } |
||
| 102 | $loginFormEvent = new LoginFormPostCreatedEvent($form); |
||
| 103 | $eventDispatcher->dispatch($loginFormEvent); |
||
| 104 | $form->handleRequest($request); |
||
| 105 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 106 | $data = $form->getData(); |
||
| 107 | $rememberMe = $data['rememberme']; |
||
| 108 | $uid = $authenticationMethod->authenticate($data); |
||
| 109 | } else { |
||
| 110 | return $this->render($authenticationMethod->getLoginTemplateName(), [ |
||
| 111 | 'loginHeader' => $loginHeader, |
||
| 112 | 'loginFooter' => $loginFooter, |
||
| 113 | 'form' => $form->createView(), |
||
| 114 | 'additionalTemplates' => isset($loginFormEvent) ? $loginFormEvent->getTemplates() : [] |
||
| 115 | ]); |
||
| 116 | } |
||
| 117 | } elseif ($authenticationMethod instanceof ReEntrantAuthenticationMethodInterface) { |
||
| 118 | // provide temp value for uid until form gives real value. |
||
| 119 | $uid = 'POST' === $request->getMethod() ? Constant::USER_ID_ANONYMOUS : $authenticationMethod->authenticate(); |
||
| 120 | $hasListeners = $eventDispatcher->hasListeners(LoginFormPostCreatedEvent::class); |
||
| 121 | $hookBindings = $hookDispatcher->getBindingsFor('subscriber.users.ui_hooks.login_screen'); |
||
| 122 | if ($hasListeners || count($hookBindings) > 0) { |
||
| 123 | $form = $this->createForm(DefaultLoginType::class, ['uid' => $uid]); |
||
| 124 | $loginFormEvent = new LoginFormPostCreatedEvent($form); |
||
| 125 | $eventDispatcher->dispatch($loginFormEvent); |
||
| 126 | if ($form->count() > 3) { // count > 3 means that the LoginFormPostCreatedEvent event added some form children |
||
| 127 | $form->handleRequest($request); |
||
| 128 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 129 | $uid = $form->get('uid')->getData(); |
||
| 130 | $rememberMe = $form->get('rememberme')->getData(); |
||
| 131 | } else { |
||
| 132 | return $this->render('@ZikulaUsersModule/Access/defaultLogin.html.twig', [ |
||
| 133 | 'loginHeader' => $loginHeader, |
||
| 134 | 'loginFooter' => $loginFooter, |
||
| 135 | 'form' => $form->createView(), |
||
| 136 | 'additionalTemplates' => isset($loginFormEvent) ? $loginFormEvent->getTemplates() : [] |
||
| 137 | ]); |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } else { |
||
| 142 | throw new LogicException($this->trans('Invalid authentication method.')); |
||
| 143 | } |
||
| 144 | $user = null; |
||
| 145 | if (isset($uid)) { |
||
| 146 | /** @var UserEntity $user */ |
||
| 147 | $user = $userRepository->find($uid); |
||
| 148 | if (isset($user)) { |
||
| 149 | $hook = new ValidationHook(); |
||
| 150 | $hookDispatcher->dispatch(LoginUiHooksSubscriber::LOGIN_VALIDATE, $hook); |
||
| 151 | $validators = $hook->getValidators(); |
||
| 152 | if (!$validators->hasErrors() && $accessHelper->loginAllowed($user)) { |
||
| 153 | if (isset($form)) { |
||
| 154 | $formDataEvent = new LoginFormPostValidatedEvent($form, $user); |
||
| 155 | $eventDispatcher->dispatch($formDataEvent); |
||
| 156 | } |
||
| 157 | $hookDispatcher->dispatch(LoginUiHooksSubscriber::LOGIN_PROCESS, new ProcessHook($user)); |
||
| 158 | $userPreSuccessLoginEvent = new UserPreLoginSuccessEvent($user, $selectedMethod); |
||
| 159 | $eventDispatcher->dispatch($userPreSuccessLoginEvent); |
||
| 160 | if (!$userPreSuccessLoginEvent->isPropagationStopped()) { |
||
| 161 | $returnUrlFromSession = null !== $session ? $session->get('returnUrl', $returnUrl) : $returnUrl; |
||
| 162 | $returnUrlFromSession = urldecode($returnUrlFromSession); |
||
| 163 | $accessHelper->login($user, $rememberMe); |
||
| 164 | $userPostSuccessLoginEvent = new UserPostLoginSuccessEvent($user, $selectedMethod); |
||
| 165 | $userPostSuccessLoginEvent->setRedirectUrl($returnUrlFromSession); |
||
| 166 | $eventDispatcher->dispatch($userPostSuccessLoginEvent); |
||
| 167 | $returnUrl = $userPostSuccessLoginEvent->getRedirectUrl(); |
||
| 168 | } else { |
||
| 169 | if ($userPreSuccessLoginEvent->hasFlashes()) { |
||
| 170 | $this->addFlash('danger', $userPreSuccessLoginEvent->getFlashesAsString()); |
||
| 171 | } |
||
| 172 | $returnUrl = $userPreSuccessLoginEvent->getRedirectUrl(); |
||
| 173 | } |
||
| 174 | |||
| 175 | return !empty($returnUrl) ? $this->redirect($returnUrl) : $this->redirectToRoute('home'); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | // login failed |
||
| 180 | $this->addFlash('error', 'Login failed.'); |
||
| 181 | if (null !== $session) { |
||
| 182 | $session->remove('authenticationMethod'); |
||
| 183 | } |
||
| 184 | $userPostFailLoginEvent = new UserPostLoginFailureEvent($user, $authenticationMethod->getAlias()); |
||
| 185 | $userPostFailLoginEvent->setRedirectUrl($returnUrl); |
||
| 186 | $returnUrl = $userPostFailLoginEvent->getRedirectUrl(); |
||
| 187 | |||
| 188 | return !empty($returnUrl) ? $this->redirect($returnUrl) : $this->redirectToRoute('home'); |
||
| 189 | } |
||
| 226 |