Conditions | 8 |
Paths | 8 |
Total Lines | 145 |
Code Lines | 91 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 4 | 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 |
||
49 | public function settingsAction(Request $request, Application $app) |
||
50 | { |
||
51 | $userOld = clone $app['user']; |
||
52 | $userOldArray = $userOld->toArray(false); |
||
53 | |||
54 | $form = $app['form.factory']->create( |
||
55 | new SettingsType(), |
||
56 | $app['user'] |
||
57 | ); |
||
58 | $newEmailCode = $request->query->get('new_email_code'); |
||
59 | |||
60 | if ($newEmailCode) { |
||
61 | $userByNewEmailCode = $app['orm.em'] |
||
62 | ->getRepository('Application\Entity\UserEntity') |
||
63 | ->findOneByNewEmailCode($newEmailCode) |
||
64 | ; |
||
65 | |||
66 | if ( |
||
67 | $userByNewEmailCode && |
||
68 | $userByNewEmailCode === $app['user'] |
||
69 | ) { |
||
70 | $app['user'] |
||
71 | ->setNewEmailCode(null) |
||
72 | ->setEmail($app['user']->getNewEmail()) |
||
73 | ->setNewEmail(null) |
||
74 | ; |
||
75 | $app['orm.em']->persist($app['user']); |
||
76 | $app['orm.em']->flush(); |
||
77 | |||
78 | $app['application.mailer'] |
||
79 | ->swiftMessageInitializeAndSend(array( |
||
80 | 'subject' => $app['name'].' - '.$app['translator']->trans('Email change confirmation'), |
||
81 | 'to' => array($app['user']->getEmail()), |
||
82 | 'body' => 'emails/users/new-email-confirmation.html.twig', |
||
83 | 'templateData' => array( |
||
84 | 'user' => $app['user'], |
||
85 | ), |
||
86 | )) |
||
87 | ; |
||
88 | |||
89 | $app['flashbag']->add( |
||
90 | 'success', |
||
91 | $app['translator']->trans( |
||
92 | 'You have successfully changed to your new email address!' |
||
93 | ) |
||
94 | ); |
||
95 | } else { |
||
96 | $app['flashbag']->add( |
||
97 | 'warning', |
||
98 | $app['translator']->trans( |
||
99 | 'The new email code is invalid. Please request your new password again!' |
||
100 | ) |
||
101 | ); |
||
102 | } |
||
103 | |||
104 | return $app->redirect( |
||
105 | $app['url_generator']->generate('members-area.my.settings') |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | if ($request->getMethod() == 'POST') { |
||
110 | $form->handleRequest($request); |
||
111 | |||
112 | if ($form->isValid()) { |
||
113 | $userEntity = $form->getData(); |
||
114 | |||
115 | if ($userEntity->getProfile()->getRemoveImage()) { |
||
116 | $userEntity->getProfile()->setImageUrl(null); |
||
117 | } |
||
118 | |||
119 | /*** Image ***/ |
||
120 | $userEntity |
||
121 | ->getProfile() |
||
122 | ->setImageUploadPath($app['baseUrl'].'/assets/uploads/') |
||
123 | ->setImageUploadDir(WEB_DIR.'/assets/uploads/') |
||
124 | ->imageUpload() |
||
125 | ; |
||
126 | $app['orm.em']->persist($userEntity); |
||
127 | |||
128 | if ($userOld->getEmail() !== $userEntity->getEmail()) { |
||
129 | $userEntity |
||
130 | ->setNewEmailCode(md5(uniqid(null, true))) |
||
131 | ->setNewEmail($userEntity->getEmail()) |
||
132 | ->setEmail($userOld->getEmail()) |
||
133 | ; |
||
134 | |||
135 | $app['application.mailer'] |
||
136 | ->swiftMessageInitializeAndSend(array( |
||
137 | 'subject' => $app['name'].' - '.$app['translator']->trans('Email change'), |
||
138 | 'to' => array($userEntity->getNewEmail()), |
||
139 | 'body' => 'emails/users/new-email.html.twig', |
||
140 | 'templateData' => array( |
||
141 | 'user' => $userEntity, |
||
142 | ), |
||
143 | )) |
||
144 | ; |
||
145 | |||
146 | $app['flashbag']->add( |
||
147 | 'success', |
||
148 | $app['translator']->trans( |
||
149 | 'Please confirm your new password, by clicking the confirmation link we just sent you to the new email address!' |
||
150 | ) |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | $userActionEntity = new UserActionEntity(); |
||
155 | $userActionEntity |
||
156 | ->setUser($userEntity) |
||
157 | ->setKey('user.settings.change') |
||
158 | ->setMessage('User has changed his settings!') |
||
159 | ->setData(array( |
||
160 | 'old' => $userOldArray, |
||
161 | 'new' => $userEntity->toArray(false), |
||
162 | )) |
||
163 | ->setIp($app['request']->getClientIp()) |
||
164 | ->setUserAgent($app['request']->headers->get('User-Agent')) |
||
165 | ; |
||
166 | $app['orm.em']->persist($userActionEntity); |
||
167 | |||
168 | $app['orm.em']->flush(); |
||
169 | |||
170 | $app['flashbag']->add( |
||
171 | 'success', |
||
172 | $app['translator']->trans( |
||
173 | 'Your settings were successfully saved!' |
||
174 | ) |
||
175 | ); |
||
176 | |||
177 | return $app->redirect( |
||
178 | $app['url_generator']->generate('members-area.my.settings') |
||
179 | ); |
||
180 | } else { |
||
181 | $app['orm.em']->refresh($app['user']); |
||
182 | } |
||
183 | } |
||
184 | |||
185 | return new Response( |
||
186 | $app['twig']->render( |
||
187 | 'contents/members-area/my/settings.html.twig', |
||
188 | array( |
||
189 | 'form' => $form->createView(), |
||
190 | ) |
||
191 | ) |
||
192 | ); |
||
193 | } |
||
194 | |||
298 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.