GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( e8545c...16e366 )
by Borut
03:19
created

MembersAreaController::resetPasswordAction()   D

Complexity

Conditions 14
Paths 49

Size

Total Lines 172
Code Lines 114

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 2 Features 0
Metric Value
c 8
b 2
f 0
dl 0
loc 172
rs 4.9516
cc 14
eloc 114
nc 49
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Application\Controller;
4
5
use Application\Entity\UserEntity;
6
use Application\Entity\UserActionEntity;
7
use Application\Form\Type\User\RegisterType;
8
use Application\Form\Type\User\ResetPasswordType;
9
use Silex\Application;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * @author Borut Balažek <[email protected]>
15
 */
16
class MembersAreaController
17
{
18
    /**
19
     * @param Application $app
20
     *
21
     * @return Response
22
     */
23
    public function indexAction(Application $app)
24
    {
25
        return new Response(
26
            $app['twig']->render(
27
                'contents/members-area/index.html.twig'
28
            )
29
        );
30
    }
31
32
    /**
33
     * @param Application $app
34
     *
35
     * @return Response
36
     */
37
    public function loginAction(Application $app)
38
    {
39
        if ($app['security.authorization_checker']->isGranted('ROLE_USER')) {
40
            return $app->redirect(
41
                $app['url_generator']->generate('members-area')
42
            );
43
        }
44
45
        $data = array(
46
            'lastUsername' => $app['session']->get('_security.last_username'),
47
            'lastError' => $app['security.last_error']($app['request']),
48
            'csrfToken' => $app['form.csrf_provider']->getToken('authenticate'),
49
        );
50
51
        return new Response(
52
            $app['twig']->render(
53
                'contents/members-area/login.html.twig',
54
                $data
55
            )
56
        );
57
    }
58
59
    /**
60
     * @param Application $app
61
     *
62
     * @return Response
63
     */
64
    public function logoutAction(Application $app)
65
    {
66
        return new Response(
67
            $app['twig']->render(
68
                'contents/members-area/logout.html.twig'
69
            )
70
        );
71
    }
72
73
    /**
74
     * @param Request     $request
75
     * @param Application $app
76
     *
77
     * @return Response
78
     */
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
182
    /**
183
     * @param Request     $request
184
     * @param Application $app
185
     *
186
     * @return Response
187
     */
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