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