| Conditions | 10 |
| Paths | 7 |
| Total Lines | 44 |
| Code Lines | 30 |
| 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 |
||
| 80 | #[Route('/change-password', name: 'chamilo_core_account_change_password', methods: ['GET', 'POST'])] |
||
| 81 | public function changePassword(Request $request, UserRepository $userRepository, CsrfTokenManagerInterface $csrfTokenManager): Response |
||
| 82 | { |
||
| 83 | $user = $this->getUser(); |
||
| 84 | |||
| 85 | if (!\is_object($user) || !$user instanceof UserInterface) { |
||
| 86 | throw $this->createAccessDeniedException('This user does not have access to this section'); |
||
| 87 | } |
||
| 88 | |||
| 89 | $form = $this->createForm(ChangePasswordType::class); |
||
| 90 | $form->handleRequest($request); |
||
| 91 | |||
| 92 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 93 | $submittedToken = $request->request->get('_token'); |
||
| 94 | |||
| 95 | if (!$csrfTokenManager->isTokenValid(new CsrfToken('change_password', $submittedToken))) { |
||
| 96 | $form->addError(new FormError($this->translator->trans('CSRF token is invalid. Please try again.'))); |
||
| 97 | } else { |
||
| 98 | $currentPassword = $form->get('currentPassword')->getData(); |
||
| 99 | $newPassword = $form->get('newPassword')->getData(); |
||
| 100 | $confirmPassword = $form->get('confirmPassword')->getData(); |
||
| 101 | |||
| 102 | if (!$userRepository->isPasswordValid($user, $currentPassword)) { |
||
| 103 | $form->get('currentPassword')->addError(new FormError($this->translator->trans('Current password is incorrect.'))); |
||
| 104 | } elseif ($newPassword !== $confirmPassword) { |
||
| 105 | $form->get('confirmPassword')->addError(new FormError($this->translator->trans('Passwords do not match.'))); |
||
| 106 | } else { |
||
| 107 | $errors = $this->validatePassword($newPassword); |
||
| 108 | if (count($errors) > 0) { |
||
| 109 | foreach ($errors as $error) { |
||
| 110 | $form->get('newPassword')->addError(new FormError($error)); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | $user->setPlainPassword($newPassword); |
||
| 114 | $userRepository->updateUser($user); |
||
| 115 | $this->addFlash('success', $this->translator->trans('Password changed successfully.')); |
||
| 116 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
| 123 | 'form' => $form->createView(), |
||
| 124 | ]); |
||
| 154 |