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