| Conditions | 6 |
| Paths | 5 |
| Total Lines | 64 |
| Code Lines | 40 |
| 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 |
||
| 146 | public function pwResetNewPw(PasswordResetManager $passwordReset, Request $request, EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher, ?string $user = null, ?string $token = null) |
||
| 147 | { |
||
| 148 | if (! $this->allow_email_pw_reset) { |
||
| 149 | throw new AccessDeniedHttpException('The password reset via email is disabled!'); |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { |
||
| 153 | throw new AccessDeniedHttpException('You are already logged in, so you can not reset your password!'); |
||
| 154 | } |
||
| 155 | |||
| 156 | $data = [ |
||
| 157 | 'username' => $user, |
||
| 158 | 'token' => $token, |
||
| 159 | ]; |
||
| 160 | $builder = $this->createFormBuilder($data); |
||
| 161 | $builder->add('username', TextType::class, [ |
||
| 162 | 'label' => $this->translator->trans('pw_reset.username'), |
||
| 163 | ]); |
||
| 164 | $builder->add('token', TextType::class, [ |
||
| 165 | 'label' => $this->translator->trans('pw_reset.token'), |
||
| 166 | ]); |
||
| 167 | $builder->add('new_password', RepeatedType::class, [ |
||
| 168 | 'type' => PasswordType::class, |
||
| 169 | 'first_options' => [ |
||
| 170 | 'label' => 'user.settings.pw_new.label', |
||
| 171 | ], |
||
| 172 | 'second_options' => [ |
||
| 173 | 'label' => 'user.settings.pw_confirm.label', |
||
| 174 | ], |
||
| 175 | 'invalid_message' => 'password_must_match', |
||
| 176 | 'constraints' => [new Length([ |
||
| 177 | 'min' => 6, |
||
| 178 | 'max' => 128, |
||
| 179 | ])], |
||
| 180 | ]); |
||
| 181 | |||
| 182 | $builder->add('submit', SubmitType::class, [ |
||
| 183 | 'label' => 'pw_reset.submit', |
||
| 184 | ]); |
||
| 185 | |||
| 186 | $form = $builder->getForm(); |
||
| 187 | $form->handleRequest($request); |
||
| 188 | |||
| 189 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 190 | $data = $form->getData(); |
||
| 191 | //Try to set the new password |
||
| 192 | $success = $passwordReset->setNewPassword($data['username'], $data['token'], $data['new_password']); |
||
| 193 | if (! $success) { |
||
| 194 | $this->addFlash('error', 'pw_reset.new_pw.error'); |
||
| 195 | } else { |
||
| 196 | $this->addFlash('success', 'pw_reset.new_pw.success'); |
||
| 197 | |||
| 198 | $repo = $em->getRepository(User::class); |
||
| 199 | $u = $repo->findOneBy(['name' => $data['username']]); |
||
| 200 | $event = new SecurityEvent($u); |
||
|
|
|||
| 201 | /** @var EventDispatcher $eventDispatcher */ |
||
| 202 | $eventDispatcher->dispatch($event, SecurityEvents::PASSWORD_RESET); |
||
| 203 | |||
| 204 | return $this->redirectToRoute('login'); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | return $this->render('security/pw_reset_new_pw.html.twig', [ |
||
| 209 | 'form' => $form->createView(), |
||
| 210 | ]); |
||
| 221 |