| Conditions | 10 |
| Paths | 6 |
| Total Lines | 83 |
| 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 |
||
| 39 | public function passwordResetRequestAction(Request $request) |
||
| 40 | { |
||
| 41 | |||
| 42 | $data = [ |
||
| 43 | 'email' => '' |
||
| 44 | ]; |
||
| 45 | |||
| 46 | $form = $this->createForm('AppBundle\Form\Type\PasswordResetType', $data); |
||
| 47 | |||
| 48 | $form->handleRequest($request); |
||
| 49 | |||
| 50 | $data = $form->getData(); |
||
| 51 | $email = $data['email']; |
||
| 52 | $error = ''; |
||
| 53 | |||
| 54 | // ¿se ha enviado una dirección? |
||
| 55 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 56 | |||
| 57 | // comprobar que está asociada a un usuario |
||
| 58 | $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneBy(['email' => $email]); |
||
| 59 | |||
| 60 | if (null === $user) { |
||
| 61 | $error = $this->get('translator')->trans('form.reset.notfound', [], 'security'); |
||
| 62 | } else { |
||
| 63 | // almacenar como último correo electrónico el indicado |
||
| 64 | $this->get('session')->set('_security.last_username', $email); |
||
| 65 | |||
| 66 | // obtener tiempo de expiración del token |
||
| 67 | $expire = (int) $this->getParameter('password_reset.expire'); |
||
| 68 | |||
| 69 | if ($this->getParameter('external.enabled') && $user->getAllowExternalLogin() && $user->hasExternalLogin()) { |
||
| 70 | $this->addFlash('error', $this->get('translator')->trans('form.reset.external_login.error', [], 'security')); |
||
| 71 | } else { |
||
| 72 | // comprobar que no se ha generado un token hace poco |
||
| 73 | if ($user->getToken() && $user->getTokenValidity() > new \DateTime()) { |
||
| 74 | $error = $this->get('translator')->trans('form.reset.wait', ['%expiry%' => $expire], 'security'); |
||
| 75 | } else { |
||
| 76 | // generar un nuevo token |
||
| 77 | $token = bin2hex(random_bytes(16)); |
||
| 78 | $user->setToken($token); |
||
| 79 | |||
| 80 | // calcular fecha de expiración del token |
||
| 81 | $validity = new \DateTime(); |
||
| 82 | $validity->add(new \DateInterval('PT' . $expire . 'M')); |
||
| 83 | $user->setTokenValidity($validity); |
||
| 84 | |||
| 85 | // enviar correo |
||
| 86 | if (0 === $this->get('app.mailer')->sendEmail([$user], |
||
| 87 | ['id' => 'form.reset.email.subject', 'parameters' => []], |
||
| 88 | [ |
||
| 89 | 'id' => 'form.reset.email.body', |
||
| 90 | 'parameters' => [ |
||
| 91 | '%name%' => $user->getFirstName(), |
||
| 92 | '%link%' => $this->generateUrl('login_password_reset_do', |
||
| 93 | ['userId' => $user->getId(), 'token' => $token], |
||
| 94 | UrlGeneratorInterface::ABSOLUTE_URL), |
||
| 95 | '%expiry%' => $expire |
||
| 96 | ] |
||
| 97 | ], 'security') |
||
| 98 | ) { |
||
| 99 | $this->addFlash('error', $this->get('translator')->trans('form.reset.error', [], 'security')); |
||
| 100 | } else { |
||
| 101 | |||
| 102 | // guardar token |
||
| 103 | $this->get('doctrine')->getManager()->flush(); |
||
| 104 | |||
| 105 | $this->addFlash('success', |
||
| 106 | $this->get('translator')->trans('form.reset.sent', ['%email%' => $email], 'security')); |
||
| 107 | return $this->redirectToRoute('login'); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | return $this->render( |
||
| 115 | ':security:login_password_reset.html.twig', [ |
||
| 116 | 'last_username' => $this->get('session')->get('_security.last_username', ''), |
||
| 117 | 'form' => $form->createView(), |
||
| 118 | 'error' => $error |
||
| 119 | ] |
||
| 120 | ); |
||
| 121 | } |
||
| 122 | |||
| 184 |