| Conditions | 11 |
| Paths | 33 |
| Total Lines | 120 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 7 | ||
| Bugs | 3 | 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 |
||
| 191 | public function resetPasswordAction(Request $request, Application $app) |
||
| 192 | { |
||
| 193 | if ($app['security.authorization_checker']->isGranted('ROLE_USER')) { |
||
| 194 | return $app->redirect( |
||
| 195 | $app['url_generator']->generate('members-area') |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | $data = array(); |
||
| 200 | |||
| 201 | $code = $request->query->has('code') |
||
| 202 | ? $request->query->get('code') |
||
| 203 | : false |
||
| 204 | ; |
||
| 205 | $action = $code |
||
| 206 | ? 'reset' |
||
| 207 | : 'request' |
||
| 208 | ; |
||
| 209 | $alert = false; |
||
| 210 | $alertMessage = ''; |
||
| 211 | |||
| 212 | $form = $app['form.factory']->create( |
||
| 213 | new ResetPasswordType($action), |
||
| 214 | new UserEntity() |
||
| 215 | ); |
||
| 216 | |||
| 217 | if ($action == 'reset') { |
||
| 218 | $userEntity = $app['orm.em'] |
||
| 219 | ->getRepository('Application\Entity\UserEntity') |
||
| 220 | ->findOneByResetPasswordCode($code) |
||
| 221 | ; |
||
| 222 | |||
| 223 | if ($userEntity) { |
||
| 224 | if ($request->getMethod() == 'POST') { |
||
| 225 | $form->handleRequest($request); |
||
| 226 | |||
| 227 | if ($form->isValid()) { |
||
| 228 | $temporaryUserEntity = $form->getData(); |
||
| 229 | |||
| 230 | $userEntity |
||
| 231 | ->setResetPasswordCode(null) |
||
| 232 | ->setPlainPassword( |
||
| 233 | $temporaryUserEntity->getPlainPassword(), |
||
| 234 | $app['security.encoder_factory'] |
||
| 235 | ) |
||
| 236 | ; |
||
| 237 | |||
| 238 | $app['orm.em']->persist($userEntity); |
||
| 239 | $app['orm.em']->flush(); |
||
| 240 | |||
| 241 | $app['application.mailer'] |
||
| 242 | ->swiftMessageInitializeAndSend(array( |
||
| 243 | 'subject' => $app['name'].' - '.$app['translator']->trans('Reset Password Confirmation'), |
||
| 244 | 'to' => array( |
||
| 245 | $userEntity->getEmail() => $userEntity->getProfile()->getFullName(), |
||
| 246 | ), |
||
| 247 | 'body' => 'emails/users/reset-password-confirmation.html.twig', |
||
| 248 | 'templateData' => array( |
||
| 249 | 'user' => $userEntity, |
||
| 250 | ), |
||
| 251 | )) |
||
| 252 | ; |
||
| 253 | |||
| 254 | $alert = 'success'; |
||
| 255 | $alertMessage = 'You password has been reset successfully.'; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } else { |
||
| 259 | $alert = 'danger'; |
||
| 260 | $alertMessage = 'This reset code was not found.'; |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | if ($request->getMethod() == 'POST') { |
||
| 264 | $form->handleRequest($request); |
||
| 265 | |||
| 266 | if ($form->isValid()) { |
||
| 267 | $temporaryUserEntity = $form->getData(); |
||
| 268 | |||
| 269 | $userEntity = $app['orm.em'] |
||
| 270 | ->getRepository('Application\Entity\UserEntity') |
||
| 271 | ->findOneByEmail( |
||
| 272 | $temporaryUserEntity->getEmail() |
||
| 273 | ); |
||
| 274 | |||
| 275 | if ($userEntity) { |
||
| 276 | $app['application.mailer'] |
||
| 277 | ->swiftMessageInitializeAndSend(array( |
||
| 278 | 'subject' => $app['name'].' - '.$app['translator']->trans('Reset password'), |
||
| 279 | 'to' => array($userEntity->getEmail()), |
||
| 280 | 'body' => 'emails/users/reset-password.html.twig', |
||
| 281 | 'type' => 'user.reset_password', |
||
| 282 | 'templateData' => array( |
||
| 283 | 'user' => $userEntity, |
||
| 284 | ), |
||
| 285 | )) |
||
| 286 | ; |
||
| 287 | |||
| 288 | $alert = 'success'; |
||
| 289 | $alertMessage = 'We have sent you an email. The link inside the email will lead you to a reset page.'; |
||
| 290 | } else { |
||
| 291 | $alert = 'danger'; |
||
| 292 | $alertMessage = 'This email was not found in our database.'; |
||
| 293 | } |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | $data['code'] = $code; |
||
| 299 | $data['action'] = $action; |
||
| 300 | $data['form'] = $form->createView(); |
||
| 301 | $data['alert'] = $alert; |
||
| 302 | $data['alertMessage'] = $alertMessage; |
||
| 303 | |||
| 304 | return new Response( |
||
| 305 | $app['twig']->render( |
||
| 306 | 'contents/members-area/reset-password.html.twig', |
||
| 307 | $data |
||
| 308 | ) |
||
| 309 | ); |
||
| 310 | } |
||
| 311 | } |
||
| 312 |