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 ( 1255d5...e0639c )
by Borut
06:26
created

MembersAreaController::resetPasswordAction()   C

Complexity

Conditions 11
Paths 33

Size

Total Lines 120
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

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