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 ( 42ab49...6b8a34 )
by Borut
09:28
created

UsersController::removeAction()   D

Complexity

Conditions 14
Paths 216

Size

Total Lines 82
Code Lines 48

Duplication

Lines 82
Ratio 100 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 82
loc 82
rs 4.3506
cc 14
eloc 48
nc 216
nop 3

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\MembersArea;
4
5
use Silex\Application;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Application\Form\Type\UserType;
9
use Application\Entity\UserEntity;
10
11
/**
12
 * @author Borut Balažek <[email protected]>
13
 */
14
class UsersController
15
{
16
    /**
17
     * @param Request     $request
18
     * @param Application $app
19
     *
20
     * @return Response
21
     */
22 View Code Duplication
    public function listAction(Request $request, Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
23
    {
24
        $data = array();
25
26
        if (
27
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
28
            !$app['security']->isGranted('ROLE_ADMIN')
29
        ) {
30
            $app->abort(403);
31
        }
32
33
        $limitPerPage = $request->query->get('limit_per_page', 20);
34
        $currentPage = $request->query->get('page');
35
36
        $userResults = $app['orm.em']
37
            ->createQueryBuilder()
38
            ->select('u')
39
            ->from('Application\Entity\UserEntity', 'u')
40
            ->leftJoin('u.profile', 'p')
41
        ;
42
43
        $pagination = $app['paginator']->paginate(
44
            $userResults,
45
            $currentPage,
46
            $limitPerPage,
47
            array(
48
                'route' => 'members-area.users',
49
                'defaultSortFieldName' => 'u.email',
50
                'defaultSortDirection' => 'asc',
51
                'searchFields' => array(
52
                    'u.username',
53
                    'u.email',
54
                    'u.roles',
55
                    'p.firstName',
56
                    'p.lastName',
57
                ),
58
            )
59
        );
60
61
        $data['pagination'] = $pagination;
62
63
        return new Response(
64
            $app['twig']->render(
65
                'contents/members-area/users/list.html.twig',
66
                $data
67
            )
68
        );
69
    }
70
71
    /**
72
     * @param Request     $request
73
     * @param Application $app
74
     *
75
     * @return Response
76
     */
77
    public function newAction(Request $request, Application $app)
78
    {
79
        $data = array();
80
81
        if (
82
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
83
            !$app['security']->isGranted('ROLE_ADMIN')
84
        ) {
85
            $app->abort(403);
86
        }
87
88
        $form = $app['form.factory']->create(
89
            new UserType($app),
90
            new UserEntity()
91
        );
92
93
        if ($request->getMethod() == 'POST') {
94
            $form->handleRequest($request);
95
96 View Code Duplication
            if ($form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
97
                $userEntity = $form->getData();
98
99
                /*** Image ***/
100
                $userEntity
101
                    ->getProfile()
102
                    ->setImageUploadPath($app['baseUrl'].'/assets/uploads/')
103
                    ->setImageUploadDir(WEB_DIR.'/assets/uploads/')
104
                    ->imageUpload()
105
                ;
106
107
                /*** Password ***/
108
                $userEntity->setPlainPassword(
109
                    $userEntity->getPlainPassword(), // This getPassword() is here just the plain password. That's why we need to convert it
110
                    $app['security.encoder_factory']
111
                );
112
113
                $app['orm.em']->persist($userEntity);
114
                $app['orm.em']->flush();
115
116
                $app['flashbag']->add(
117
                    'success',
118
                    $app['translator']->trans(
119
                        'members-area.users.new.successText'
120
                    )
121
                );
122
123
                return $app->redirect(
124
                    $app['url_generator']->generate(
125
                        'members-area.users.edit',
126
                        array(
127
                            'id' => $userEntity->getId(),
128
                        )
129
                    )
130
                );
131
            }
132
        }
133
134
        $data['form'] = $form->createView();
135
136
        return new Response(
137
            $app['twig']->render(
138
                'contents/members-area/users/new.html.twig',
139
                $data
140
            )
141
        );
142
    }
143
144
    /**
145
     * @param $id
146
     * @param Application $app
147
     *
148
     * @return Response
149
     */
150
    public function detailAction($id, Application $app)
151
    {
152
        $data = array();
153
154
        if (
155
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
156
            !$app['security']->isGranted('ROLE_ADMIN')
157
        ) {
158
            $app->abort(403);
159
        }
160
161
        $user = $app['orm.em']->find('Application\Entity\UserEntity', $id);
162
163
        if (!$user) {
164
            $app->abort(404);
165
        }
166
167
        $data['user'] = $user;
168
169
        return new Response(
170
            $app['twig']->render(
171
                'contents/members-area/users/detail.html.twig',
172
                $data
173
            )
174
        );
175
    }
176
177
    public function editAction($id, Request $request, Application $app)
178
    {
179
        $data = array();
180
181
        if (
182
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
183
            !$app['security']->isGranted('ROLE_ADMIN')
184
        ) {
185
            $app->abort(403);
186
        }
187
188
        $user = $app['orm.em']->find(
189
            'Application\Entity\UserEntity',
190
            $id
191
        );
192
193
        if (!$user) {
194
            $app->abort(404);
195
        }
196
197
        $form = $app['form.factory']->create(
198
            new UserType($app),
199
            $user
200
        );
201
202
        if ($request->getMethod() == 'POST') {
203
            $form->handleRequest($request);
204
205
            if ($form->isValid()) {
206
                $userEntity = $form->getData();
207
208
                if (
209
                    $userEntity->isLocked() &&
210
                    $userEntity->hasRole('ROLE_SUPER_ADMIN')
211
                ) {
212
                    $app['flashbag']->add(
213
                        'danger',
214
                        $app['translator']->trans(
215
                            'A super admin user can not be locked!'
216
                        )
217
                    );
218
219
                    return $app->redirect(
220
                        $app['url_generator']->generate(
221
                            'members-area.users.edit',
222
                            array(
223
                                'id' => $userEntity->getId(),
224
                            )
225
                        )
226
                    );
227
                }
228
229
                /*** Image ***/
230
                $userEntity
231
                    ->getProfile()
232
                    ->setImageUploadPath($app['baseUrl'].'/assets/uploads/')
233
                    ->setImageUploadDir(WEB_DIR.'/assets/uploads/')
234
                    ->imageUpload()
235
                ;
236
237
                /*** Password ***/
238
                if ($userEntity->getPlainPassword()) {
239
                    $userEntity->setPlainPassword(
240
                        $userEntity->getPlainPassword(), // This getPassword() is here just the plain password. That's why we need to convert it
241
                        $app['security.encoder_factory']
242
                    );
243
                }
244
245
                $app['orm.em']->persist($userEntity);
246
                $app['orm.em']->flush();
247
248
                $app['flashbag']->add(
249
                    'success',
250
                    $app['translator']->trans(
251
                        'members-area.users.edit.successText'
252
                    )
253
                );
254
255
                return $app->redirect(
256
                    $app['url_generator']->generate(
257
                        'members-area.users.edit',
258
                        array(
259
                            'id' => $userEntity->getId(),
260
                        )
261
                    )
262
                );
263
            }
264
        }
265
266
        $data['form'] = $form->createView();
267
268
        $data['user'] = $user;
269
270
        return new Response(
271
            $app['twig']->render(
272
                'contents/members-area/users/edit.html.twig',
273
                $data
274
            )
275
        );
276
    }
277
278 View Code Duplication
    public function removeAction($id, Request $request, Application $app)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
279
    {
280
        $data = array();
281
282
        if (
283
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
284
            !$app['security']->isGranted('ROLE_ADMIN')
285
        ) {
286
            $app->abort(403);
287
        }
288
289
        $users = array();
290
        $ids = $request->query->get('ids', false);
291
        $idsExploded = explode(',', $ids);
292
        foreach ($idsExploded as $singleId) {
293
            $singleEntity = $app['orm.em']->find(
294
                'Application\Entity\UserEntity',
295
                $singleId
296
            );
297
298
            if ($singleEntity) {
299
                $users[] = $singleEntity;
300
            }
301
        }
302
303
        $user = $app['orm.em']->find('Application\Entity\UserEntity', $id);
304
305
        if (
306
            (
307
                !$user &&
308
                $ids === false
309
            ) ||
310
            (
311
                empty($users) &&
312
                $ids !== false
313
            )
314
        ) {
315
            $app->abort(404);
316
        }
317
318
        $confirmAction = $app['request']->query->has('action') && $app['request']->query->get('action') == 'confirm';
319
320
        if ($confirmAction) {
321
            try {
322
                if (!empty($users)) {
323
                    foreach ($users as $user) {
324
                        $app['orm.em']->remove($user);
325
                    }
326
                } else {
327
                    $app['orm.em']->remove($user);
328
                }
329
330
                $app['orm.em']->flush();
331
332
                $app['flashbag']->add(
333
                    'success',
334
                    $app['translator']->trans(
335
                        'members-area.users.remove.successText'
336
                    )
337
                );
338
            } catch (\Exception $e) {
339
                $app['flashbag']->add(
340
                    'danger',
341
                    $app['translator']->trans(
342
                        $e->getMessage()
343
                    )
344
                );
345
            }
346
347
            return $app->redirect(
348
                $app['url_generator']->generate('members-area.users')
349
            );
350
        }
351
352
        $data['user'] = $user;
353
        $data['users'] = $users;
354
        $data['ids'] = $ids;
355
356
        return new Response(
357
            $app['twig']->render('contents/members-area/users/remove.html.twig', $data)
358
        );
359
    }
360
}
361