| Conditions | 17 |
| Paths | 23 |
| Total Lines | 96 |
| Code Lines | 70 |
| 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 |
||
| 85 | #[Route('/change-password', name: 'chamilo_core_account_change_password', methods: ['GET', 'POST'])] |
||
| 86 | public function changePassword(Request $request, UserRepository $userRepository, CsrfTokenManagerInterface $csrfTokenManager): Response |
||
| 87 | { |
||
| 88 | /* @var User $user */ |
||
| 89 | $user = $this->getUser(); |
||
| 90 | |||
| 91 | if (!\is_object($user) || !$user instanceof UserInterface) { |
||
| 92 | throw $this->createAccessDeniedException('This user does not have access to this section'); |
||
| 93 | } |
||
| 94 | |||
| 95 | $form = $this->createForm(ChangePasswordType::class, [ |
||
| 96 | 'enable2FA' => $user->getMfaEnabled(), |
||
| 97 | ]); |
||
| 98 | $form->handleRequest($request); |
||
| 99 | |||
| 100 | $qrCodeBase64 = null; |
||
| 101 | if ($user->getMfaEnabled() && $user->getMfaService() === 'TOTP' && $user->getMfaSecret()) { |
||
| 102 | $decryptedSecret = $this->decryptTOTPSecret($user->getMfaSecret(), $_ENV['APP_SECRET']); |
||
| 103 | $totp = TOTP::create($decryptedSecret); |
||
| 104 | $totp->setLabel($user->getEmail()); |
||
| 105 | |||
| 106 | $qrCodeResult = Builder::create() |
||
| 107 | ->writer(new PngWriter()) |
||
| 108 | ->data($totp->getProvisioningUri()) |
||
| 109 | ->encoding(new Encoding('UTF-8')) |
||
| 110 | ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||
| 111 | ->size(300) |
||
| 112 | ->margin(10) |
||
| 113 | ->build(); |
||
| 114 | |||
| 115 | $qrCodeBase64 = base64_encode($qrCodeResult->getString()); |
||
| 116 | } |
||
| 117 | |||
| 118 | if ($form->isSubmitted() && $form->isValid()) { |
||
| 119 | $submittedToken = $request->request->get('_token'); |
||
| 120 | |||
| 121 | if (!$csrfTokenManager->isTokenValid(new CsrfToken('change_password', $submittedToken))) { |
||
| 122 | $form->addError(new FormError($this->translator->trans('CSRF token is invalid. Please try again.'))); |
||
| 123 | } else { |
||
| 124 | $currentPassword = $form->get('currentPassword')->getData(); |
||
| 125 | $newPassword = $form->get('newPassword')->getData(); |
||
| 126 | $confirmPassword = $form->get('confirmPassword')->getData(); |
||
| 127 | $enable2FA = $form->get('enable2FA')->getData(); |
||
| 128 | |||
| 129 | if ($enable2FA && !$user->getMfaSecret()) { |
||
| 130 | $totp = TOTP::create(); |
||
| 131 | $totp->setLabel($user->getEmail()); |
||
| 132 | $encryptedSecret = $this->encryptTOTPSecret($totp->getSecret(), $_ENV['APP_SECRET']); |
||
| 133 | $user->setMfaSecret($encryptedSecret); |
||
| 134 | $user->setMfaEnabled(true); |
||
| 135 | $user->setMfaService('TOTP'); |
||
| 136 | $userRepository->updateUser($user); |
||
| 137 | |||
| 138 | $qrCodeResult = Builder::create() |
||
| 139 | ->writer(new PngWriter()) |
||
| 140 | ->data($totp->getProvisioningUri()) |
||
| 141 | ->encoding(new Encoding('UTF-8')) |
||
| 142 | ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) |
||
| 143 | ->size(300) |
||
| 144 | ->margin(10) |
||
| 145 | ->build(); |
||
| 146 | |||
| 147 | $qrCodeBase64 = base64_encode($qrCodeResult->getString()); |
||
| 148 | |||
| 149 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
| 150 | 'form' => $form->createView(), |
||
| 151 | 'qrCode' => $qrCodeBase64, |
||
| 152 | 'user' => $user |
||
| 153 | ]); |
||
| 154 | } elseif (!$enable2FA) { |
||
| 155 | $user->setMfaEnabled(false); |
||
| 156 | $user->setMfaSecret(null); |
||
| 157 | $userRepository->updateUser($user); |
||
| 158 | $this->addFlash('success', '2FA disabled successfully.'); |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($newPassword || $confirmPassword || $currentPassword) { |
||
| 162 | if (!$userRepository->isPasswordValid($user, $currentPassword)) { |
||
| 163 | $form->get('currentPassword')->addError(new FormError($this->translator->trans('Current password is incorrect.'))); |
||
| 164 | } elseif ($newPassword !== $confirmPassword) { |
||
| 165 | $form->get('confirmPassword')->addError(new FormError($this->translator->trans('Passwords do not match.'))); |
||
| 166 | } else { |
||
| 167 | $user->setPlainPassword($newPassword); |
||
| 168 | $userRepository->updateUser($user); |
||
| 169 | $this->addFlash('success', 'Password updated successfully.'); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | return $this->redirectToRoute('chamilo_core_account_home'); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | return $this->render('@ChamiloCore/Account/change_password.html.twig', [ |
||
| 178 | 'form' => $form->createView(), |
||
| 179 | 'qrCode' => $qrCodeBase64, |
||
| 180 | 'user' => $user |
||
| 181 | ]); |
||
| 234 |
This check looks for private methods that have been defined, but are not used inside the class.