| Conditions | 14 |
| Paths | 49 |
| Total Lines | 172 |
| Code Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| Bugs | 2 | Features | 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 |
||
| 188 | public function resetPasswordAction(Request $request, Application $app) |
||
| 189 | { |
||
| 190 | if ($app['security.authorization_checker']->isGranted('ROLE_USER')) { |
||
| 191 | return $app->redirect( |
||
| 192 | $app['url_generator']->generate('members-area') |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | $code = $request->query->has('code') |
||
| 197 | ? $request->query->get('code') |
||
| 198 | : false |
||
| 199 | ; |
||
| 200 | $action = $code |
||
| 201 | ? 'reset' |
||
| 202 | : 'request' |
||
| 203 | ; |
||
| 204 | $alert = false; |
||
| 205 | $alertMessage = ''; |
||
| 206 | |||
| 207 | $currentDateTime = new \DateTime(); |
||
| 208 | $form = $app['form.factory']->create( |
||
| 209 | new ResetPasswordType($action), |
||
| 210 | new UserEntity() |
||
| 211 | ); |
||
| 212 | |||
| 213 | if ($action == 'reset') { |
||
| 214 | $userEntity = $app['orm.em'] |
||
| 215 | ->getRepository('Application\Entity\UserEntity') |
||
| 216 | ->findOneByResetPasswordCode($code) |
||
| 217 | ; |
||
| 218 | |||
| 219 | if ($userEntity) { |
||
| 220 | $isResetPasswordCodeExpired = $currentDateTime > $userEntity->getTimeResetPasswordCodeExpires(); |
||
| 221 | |||
| 222 | if ($isResetPasswordCodeExpired) { |
||
| 223 | $alert = 'danger'; |
||
| 224 | $alertMessage = 'This code has expired. Please try to reset your password again.'; |
||
| 225 | } else { |
||
| 226 | if ($request->getMethod() == 'POST') { |
||
| 227 | $form->handleRequest($request); |
||
| 228 | |||
| 229 | if ($form->isValid()) { |
||
| 230 | $temporaryUserEntity = $form->getData(); |
||
| 231 | |||
| 232 | $userEntity |
||
| 233 | ->setResetPasswordCode(null) |
||
| 234 | ->setTimeResetPasswordCodeExpires(null) |
||
| 235 | ->setPlainPassword( |
||
| 236 | $temporaryUserEntity->getPlainPassword(), |
||
| 237 | $app['security.encoder_factory'] |
||
| 238 | ) |
||
| 239 | ; |
||
| 240 | $app['orm.em']->persist($userEntity); |
||
| 241 | |||
| 242 | $userActionEntity = new UserActionEntity(); |
||
| 243 | $userActionEntity |
||
| 244 | ->setUser($userEntity) |
||
| 245 | ->setKey('user.password.reset') |
||
| 246 | ->setMessage('User has reset his password!') |
||
| 247 | ->setIp($app['request']->getClientIp()) |
||
| 248 | ->setUserAgent($app['request']->headers->get('User-Agent')) |
||
| 249 | ; |
||
| 250 | $app['orm.em']->persist($userActionEntity); |
||
| 251 | |||
| 252 | $app['orm.em']->flush(); |
||
| 253 | |||
| 254 | $app['application.mailer'] |
||
| 255 | ->swiftMessageInitializeAndSend(array( |
||
| 256 | 'subject' => $app['name'].' - '.$app['translator']->trans('Reset Password Confirmation'), |
||
| 257 | 'to' => array( |
||
| 258 | $userEntity->getEmail() => $userEntity->getProfile()->getFullName(), |
||
| 259 | ), |
||
| 260 | 'body' => 'emails/users/reset-password-confirmation.html.twig', |
||
| 261 | 'templateData' => array( |
||
| 262 | 'user' => $userEntity, |
||
| 263 | ), |
||
| 264 | )) |
||
| 265 | ; |
||
| 266 | |||
| 267 | $alert = 'success'; |
||
| 268 | $alertMessage = 'Your password has been reset successfully.'; |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } else { |
||
| 273 | $alert = 'danger'; |
||
| 274 | $alertMessage = 'This reset code was not found.'; |
||
| 275 | } |
||
| 276 | } else { |
||
| 277 | if ($request->getMethod() == 'POST') { |
||
| 278 | $form->handleRequest($request); |
||
| 279 | |||
| 280 | if ($form->isValid()) { |
||
| 281 | $temporaryUserEntity = $form->getData(); |
||
| 282 | |||
| 283 | $userEntity = $app['orm.em'] |
||
| 284 | ->getRepository('Application\Entity\UserEntity') |
||
| 285 | ->findOneByEmail( |
||
| 286 | $temporaryUserEntity->getEmail() |
||
| 287 | ) |
||
| 288 | ; |
||
| 289 | |||
| 290 | if ($userEntity) { |
||
| 291 | $isPasswordCodeAlreadySent = $currentDateTime < $userEntity->getTimeResetPasswordCodeExpires(); |
||
| 292 | |||
| 293 | if ($isPasswordCodeAlreadySent) { |
||
| 294 | $alert = 'info'; |
||
| 295 | $alertMessage = 'A reset password email was already sent to you. Please check your email address for further instructions.'; |
||
| 296 | } else { |
||
| 297 | $userEntity |
||
| 298 | ->setResetPasswordCode(md5(uniqid(null, true))) |
||
| 299 | ->setTimeResetPasswordCodeExpires( |
||
| 300 | new \Datetime( |
||
| 301 | 'now +'.$app['user_system_options']['reset_password_expiry_time'] |
||
| 302 | ) |
||
| 303 | ) |
||
| 304 | ; |
||
| 305 | $app['orm.em']->persist($userEntity); |
||
| 306 | |||
| 307 | $userActionEntity = new UserActionEntity(); |
||
| 308 | $userActionEntity |
||
| 309 | ->setUser($userEntity) |
||
| 310 | ->setKey('user.password.request') |
||
| 311 | ->setMessage('User has requested a password reset!') |
||
| 312 | ->setIp($app['request']->getClientIp()) |
||
| 313 | ->setUserAgent($app['request']->headers->get('User-Agent')) |
||
| 314 | ; |
||
| 315 | $app['orm.em']->persist($userActionEntity); |
||
| 316 | |||
| 317 | // In the REALLY unlikely case that the reset password code wouldn't be unique |
||
| 318 | try { |
||
| 319 | $app['orm.em']->flush(); |
||
| 320 | |||
| 321 | $app['application.mailer'] |
||
| 322 | ->swiftMessageInitializeAndSend(array( |
||
| 323 | 'subject' => $app['name'].' - '.$app['translator']->trans('Reset password'), |
||
| 324 | 'to' => array($userEntity->getEmail()), |
||
| 325 | 'body' => 'emails/users/reset-password.html.twig', |
||
| 326 | 'templateData' => array( |
||
| 327 | 'user' => $userEntity, |
||
| 328 | ), |
||
| 329 | )) |
||
| 330 | ; |
||
| 331 | |||
| 332 | $alert = 'success'; |
||
| 333 | $alertMessage = 'We have sent you an email. The link inside the email will lead you to a reset page.'; |
||
| 334 | } catch (\Exception $e) { |
||
| 335 | $alert = 'danger'; |
||
| 336 | $alertMessage = 'Whops. Something went wrong. Please try again.'; |
||
| 337 | } |
||
| 338 | } |
||
| 339 | } else { |
||
| 340 | $alert = 'danger'; |
||
| 341 | $alertMessage = 'This email was not found in our database.'; |
||
| 342 | } |
||
| 343 | } |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | return new Response( |
||
| 348 | $app['twig']->render( |
||
| 349 | 'contents/members-area/reset-password.html.twig', |
||
| 350 | array( |
||
| 351 | 'code' => $code, |
||
| 352 | 'action' => $action, |
||
| 353 | 'form' => $form->createView(), |
||
| 354 | 'alert' => $alert, |
||
| 355 | 'alertMessage' => $alertMessage, |
||
| 356 | ) |
||
| 357 | ) |
||
| 358 | ); |
||
| 359 | } |
||
| 360 | } |
||
| 361 |