Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 35 | class SecurityController extends Controller |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @Route("/entrar", name="login", methods={"GET"}) |
||
| 39 | */ |
||
| 40 | public function loginAction() |
||
| 41 | { |
||
| 42 | $authenticationUtils = $this->get('security.authentication_utils'); |
||
| 43 | |||
| 44 | // obtener el error de entrada, si existe alguno |
||
| 45 | $error = $authenticationUtils->getLastAuthenticationError(); |
||
| 46 | |||
| 47 | // último nombre de usuario introducido |
||
| 48 | $lastUsername = $authenticationUtils->getLastUsername(); |
||
| 49 | |||
| 50 | return $this->render( |
||
| 51 | 'security/login.html.twig', |
||
| 52 | array( |
||
| 53 | 'last_username' => $lastUsername, |
||
| 54 | 'login_error' => $error, |
||
| 55 | ) |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @Route("/comprobar", name="login_check", methods={"POST"}) |
||
| 61 | * @Route("/salir", name="logout", methods={"GET"}) |
||
| 62 | */ |
||
| 63 | public function logInOutCheckAction() |
||
| 64 | { |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @Route("/restablecer", name="login_password_reset", methods={"GET", "POST"}) |
||
| 69 | */ |
||
| 70 | public function passwordResetRequestAction(Request $request) |
||
| 71 | { |
||
| 72 | |||
| 73 | $data = [ |
||
| 74 | 'email' => '' |
||
| 75 | ]; |
||
| 76 | |||
| 77 | $form = $this->createForm('AppBundle\Form\Type\PasswordResetType', $data); |
||
| 78 | |||
| 79 | $form->handleRequest($request); |
||
| 80 | |||
| 81 | $data = $form->getData(); |
||
| 82 | $email = $data['email']; |
||
| 83 | $error = ''; |
||
| 84 | |||
| 85 | // ¿se ha enviado una dirección? |
||
| 86 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 87 | $error = $this->passwordResetRequest($email); |
||
| 88 | |||
| 89 | if (!is_string($error)) { |
||
| 90 | return $error; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | return $this->render( |
||
| 95 | 'security/login_password_reset.html.twig', [ |
||
| 96 | 'last_username' => $this->get('session')->get('_security.last_username', ''), |
||
| 97 | 'form' => $form->createView(), |
||
| 98 | 'error' => $error |
||
| 99 | ] |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @Route("/restablecer/correo/{userId}/{token}", name="email_reset_do", methods={"GET", "POST"}) |
||
| 105 | */ |
||
| 106 | public function emailResetAction(Request $request, $userId, $token) |
||
| 107 | { |
||
| 108 | /** |
||
| 109 | * @var User |
||
| 110 | */ |
||
| 111 | $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneBy([ |
||
| 112 | 'id' => $userId, |
||
| 113 | 'token' => $token |
||
| 114 | ]); |
||
| 115 | |||
| 116 | View Code Duplication | if (null === $user || $user->getTokenType() === 'password' || $user->getTokenExpiration() < new \DateTime()) { |
|
|
|
|||
| 117 | $this->addFlash('error', $this->get('translator')->trans('form.change_email.notvalid', [], 'security')); |
||
| 118 | return $this->redirectToRoute('login'); |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($request->getMethod() === 'POST') { |
||
| 122 | $user |
||
| 123 | ->setEmailAddress($user->getTokenType()) |
||
| 124 | ->setToken(null) |
||
| 125 | ->setTokenExpiration(null) |
||
| 126 | ->setTokenType(null); |
||
| 127 | |||
| 128 | try { |
||
| 129 | $this->getDoctrine()->getManager()->flush(); |
||
| 130 | |||
| 131 | // indicar que los cambios se han realizado con éxito y volver a la página de inicio |
||
| 132 | $this->addFlash('success', $this->get('translator')->trans('form.change_email.message', [], 'security')); |
||
| 133 | } catch (\Exception $e) { |
||
| 134 | // indicar que no se ha podido cambiar |
||
| 135 | $this->addFlash('error', $this->get('translator')->trans('form.change_email.error', [], 'security')); |
||
| 136 | } |
||
| 137 | return new RedirectResponse( |
||
| 138 | $this->generateUrl('frontpage') |
||
| 139 | ); |
||
| 140 | } |
||
| 141 | |||
| 142 | return $this->render( |
||
| 143 | 'security/login_email_change.html.twig', [ |
||
| 144 | 'user' => $user |
||
| 145 | ] |
||
| 146 | ); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @Route("/restablecer/{userId}/{token}", name="login_password_reset_do", methods={"GET", "POST"}) |
||
| 151 | */ |
||
| 152 | public function passwordResetAction(Request $request, $userId, $token) |
||
| 153 | { |
||
| 154 | /** |
||
| 155 | * @var User |
||
| 156 | */ |
||
| 157 | $user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneBy([ |
||
| 158 | 'id' => $userId, |
||
| 159 | 'token' => $token, |
||
| 160 | 'tokenType' => 'password' |
||
| 161 | ]); |
||
| 162 | |||
| 163 | View Code Duplication | if (null === $user || ($user->getTokenExpiration() < new \DateTime())) { |
|
| 164 | $this->addFlash('error', $this->get('translator')->trans('form.reset.notvalid', [], 'security')); |
||
| 165 | return $this->redirectToRoute('login'); |
||
| 166 | } |
||
| 167 | |||
| 168 | $data = [ |
||
| 169 | 'password' => '', |
||
| 170 | 'repeat' => '' |
||
| 171 | ]; |
||
| 172 | |||
| 173 | $form = $this->createForm('AppBundle\Form\Type\NewPasswordType', $data); |
||
| 174 | |||
| 175 | $form->handleRequest($request); |
||
| 176 | |||
| 177 | $error = ''; |
||
| 178 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 179 | |||
| 180 | //codificar la nueva contraseña y asignarla al usuario |
||
| 181 | $password = $this->get('security.password_encoder') |
||
| 182 | ->encodePassword($user, $form->get('newPassword')->get('first')->getData()); |
||
| 183 | |||
| 184 | $user |
||
| 185 | ->setPassword($password) |
||
| 186 | ->setToken(null) |
||
| 187 | ->setTokenExpiration(null) |
||
| 188 | ->setTokenType(null); |
||
| 189 | |||
| 190 | $this->getDoctrine()->getManager()->flush(); |
||
| 191 | |||
| 192 | // indicar que los cambios se han realizado con éxito y volver a la página de inicio |
||
| 193 | $message = $this->get('translator')->trans('form.reset.message', [], 'security'); |
||
| 194 | $this->addFlash('success', $message); |
||
| 195 | return new RedirectResponse( |
||
| 196 | $this->generateUrl('frontpage') |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | |||
| 200 | return $this->render( |
||
| 201 | 'security/login_password_new.html.twig', [ |
||
| 202 | 'user' => $user, |
||
| 203 | 'form' => $form->createView(), |
||
| 204 | 'error' => $error |
||
| 205 | ] |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @Route("/organizacion", name="login_organization", methods={"GET", "POST"}) |
||
| 211 | */ |
||
| 212 | public function organizationAction(Request $request) |
||
| 213 | { |
||
| 214 | // si no hay usuario activo, volver |
||
| 215 | if (null === $this->getUser()) { |
||
| 216 | return $this->redirectToRoute('login'); |
||
| 217 | } |
||
| 218 | |||
| 219 | /** @var Session $session */ |
||
| 220 | $session = $this->get('session'); |
||
| 221 | |||
| 222 | $data = ['organization' => $this->getUser()->getDefaultOrganization()]; |
||
| 223 | |||
| 224 | $count = $this->getDoctrine()->getManager()->getRepository('AppBundle:Organization')->countOrganizationsByUser($this->getUser(), new \DateTime()); |
||
| 225 | |||
| 226 | $form = $this->createFormBuilder($data) |
||
| 227 | ->add('organization', EntityType::class, [ |
||
| 228 | 'expanded' => $count < 5, |
||
| 229 | 'class' => Organization::class, |
||
| 230 | 'query_builder' => function(OrganizationRepository $er) { |
||
| 231 | return $er->getMembershipByUserQueryBuilder($this->getUser(), new \DateTime()); |
||
| 232 | }, |
||
| 233 | 'required' => true |
||
| 234 | ]) |
||
| 235 | ->getForm(); |
||
| 236 | |||
| 237 | $form->handleRequest($request); |
||
| 238 | |||
| 239 | // ¿se ha seleccionado una organización? |
||
| 240 | if ($form->isSubmitted() && $form->isValid() && $form->get('organization')->getData()) { |
||
| 241 | |||
| 242 | $session->set('organization_id', $form->get('organization')->getData()->getId()); |
||
| 243 | $session->set('organization_selected', true); |
||
| 244 | $this->getUser()->setDefaultOrganization($form->get('organization')->getData()); |
||
| 245 | $this->getDoctrine()->getManager()->flush(); |
||
| 246 | |||
| 247 | $url = $session->get('_security.organization.target_path', $this->generateUrl('frontpage')); |
||
| 248 | $session->remove('_security.organization.target_path'); |
||
| 249 | return new RedirectResponse($url); |
||
| 250 | } |
||
| 251 | return $this->render('security/login_organization.html.twig', [ |
||
| 252 | 'form' => $form->createView(), |
||
| 253 | 'count' => $count |
||
| 254 | ] |
||
| 255 | ); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param $email |
||
| 260 | * @return string|RedirectResponse |
||
| 261 | */ |
||
| 262 | private function passwordResetRequest($email) |
||
| 323 | |||
| 324 | } |
||
| 325 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.