| Conditions | 9 |
| Paths | 21 |
| Total Lines | 102 |
| Code Lines | 65 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | 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 |
||
| 79 | public function registerAction(Request $request, Application $app) |
||
| 80 | { |
||
| 81 | if ($app['security.authorization_checker']->isGranted('ROLE_USER')) { |
||
| 82 | return $app->redirect( |
||
| 83 | $app['url_generator']->generate('members-area') |
||
| 84 | ); |
||
| 85 | } |
||
| 86 | |||
| 87 | $code = $request->query->has('code') |
||
| 88 | ? $request->query->get('code') |
||
| 89 | : false |
||
| 90 | ; |
||
| 91 | $action = $code |
||
| 92 | ? 'confirm' |
||
| 93 | : 'register' |
||
| 94 | ; |
||
| 95 | $alert = false; |
||
| 96 | $alertMessage = ''; |
||
| 97 | |||
| 98 | $form = $app['form.factory']->create( |
||
| 99 | new RegisterType(), |
||
| 100 | new UserEntity() |
||
| 101 | ); |
||
| 102 | |||
| 103 | if ($action == 'confirm') { |
||
| 104 | $userEntity = $app['orm.em'] |
||
| 105 | ->getRepository('Application\Entity\UserEntity') |
||
| 106 | ->findOneByActivationCode($code) |
||
| 107 | ; |
||
| 108 | |||
| 109 | if ($userEntity) { |
||
| 110 | $userEntity |
||
| 111 | ->setActivationCode(null) |
||
| 112 | ->enable() |
||
| 113 | ; |
||
| 114 | |||
| 115 | $app['orm.em']->persist($userEntity); |
||
| 116 | $app['orm.em']->flush(); |
||
| 117 | |||
| 118 | $app['application.mailer'] |
||
| 119 | ->swiftMessageInitializeAndSend(array( |
||
| 120 | 'subject' => $app['name'].' - '.$app['translator']->trans('Welcome'), |
||
| 121 | 'to' => array($userEntity->getEmail()), |
||
| 122 | 'body' => 'emails/users/register-welcome.html.twig', |
||
| 123 | 'templateData' => array( |
||
| 124 | 'user' => $userEntity, |
||
| 125 | ), |
||
| 126 | )) |
||
| 127 | ; |
||
| 128 | |||
| 129 | $alert = 'success'; |
||
| 130 | $alertMessage = 'Your account has been activated!'; |
||
| 131 | } else { |
||
| 132 | $alert = 'danger'; |
||
| 133 | $alertMessage = 'This activation code was not found!'; |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | if ( |
||
| 137 | $request->getMethod() == 'POST' && |
||
| 138 | $app['user_system_options']['registrations_enabled'] |
||
| 139 | ) { |
||
| 140 | $form->handleRequest($request); |
||
| 141 | |||
| 142 | if ($form->isValid()) { |
||
| 143 | $userEntity = $form->getData(); |
||
| 144 | |||
| 145 | $userEntity->setPlainPassword( |
||
| 146 | $userEntity->getPlainPassword(), |
||
| 147 | $app['security.encoder_factory'] |
||
| 148 | ); |
||
| 149 | |||
| 150 | $app['application.mailer'] |
||
| 151 | ->swiftMessageInitializeAndSend(array( |
||
| 152 | 'subject' => $app['name'].' - '.$app['translator']->trans('Registration'), |
||
| 153 | 'to' => array($userEntity->getEmail()), |
||
| 154 | 'body' => 'emails/users/register.html.twig', |
||
| 155 | 'templateData' => array( |
||
| 156 | 'user' => $userEntity, |
||
| 157 | ), |
||
| 158 | )) |
||
| 159 | ; |
||
| 160 | |||
| 161 | $app['orm.em']->persist($userEntity); |
||
| 162 | $app['orm.em']->flush(); |
||
| 163 | |||
| 164 | $alert = 'success'; |
||
| 165 | $alertMessage = 'You have successfully registered. We have sent you an confirmation email. Please click the link inside to activate your account.'; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | return new Response( |
||
| 171 | $app['twig']->render( |
||
| 172 | 'contents/members-area/register.html.twig', |
||
| 173 | array( |
||
| 174 | 'form' => $form->createView(), |
||
| 175 | 'alert' => $alert, |
||
| 176 | 'alertMessage' => $alertMessage, |
||
| 177 | ) |
||
| 178 | ) |
||
| 179 | ); |
||
| 180 | } |
||
| 181 | |||
| 361 |