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.

UsersController::removeAction()   D
last analyzed

Complexity

Conditions 14
Paths 216

Size

Total Lines 86
Code Lines 51

Duplication

Lines 86
Ratio 100 %

Importance

Changes 0
Metric Value
dl 86
loc 86
rs 4.348
c 0
b 0
f 0
cc 14
eloc 51
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 Application\Form\Type\UserType;
6
use Application\Entity\UserEntity;
7
use Silex\Application;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
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
    public function listAction(Request $request, Application $app)
23
    {
24
        if (
25
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
26
            !$app['security']->isGranted('ROLE_ADMIN')
27
        ) {
28
            $app->abort(403);
29
        }
30
31
        $limitPerPage = $request->query->get('limit_per_page', 20);
32
        $currentPage = $request->query->get('page');
33
34
        $userResults = $app['orm.em']
35
            ->createQueryBuilder()
36
            ->select('u')
37
            ->from('Application\Entity\UserEntity', 'u')
38
            ->leftJoin('u.profile', 'p')
39
        ;
40
41
        $pagination = $app['application.paginator']->paginate(
42
            $userResults,
43
            $currentPage,
44
            $limitPerPage,
45
            array(
46
                'route' => 'members-area.users',
47
                'defaultSortFieldName' => 'u.email',
48
                'defaultSortDirection' => 'asc',
49
                'searchFields' => array(
50
                    'u.username',
51
                    'u.email',
52
                    'u.roles',
53
                    'p.firstName',
54
                    'p.lastName',
55
                ),
56
            )
57
        );
58
59
        return new Response(
60
            $app['twig']->render(
61
                'contents/members-area/users/list.html.twig',
62
                array(
63
                    'pagination' => $pagination,
64
                )
65
            )
66
        );
67
    }
68
69
    /**
70
     * @param Request     $request
71
     * @param Application $app
72
     *
73
     * @return Response
74
     */
75
    public function newAction(Request $request, Application $app)
76
    {
77
        if (
78
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
79
            !$app['security']->isGranted('ROLE_ADMIN')
80
        ) {
81
            $app->abort(403);
82
        }
83
84
        $form = $app['form.factory']->create(
85
            new UserType($app),
86
            new UserEntity()
87
        );
88
89 View Code Duplication
        if ($request->getMethod() == 'POST') {
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...
90
            $form->handleRequest($request);
91
92
            if ($form->isValid()) {
93
                $userEntity = $form->getData();
94
95
                /*** Image ***/
96
                $userEntity
97
                    ->getProfile()
98
                    ->setImageUploadPath($app['baseUrl'].'/assets/uploads/')
99
                    ->setImageUploadDir(WEB_DIR.'/assets/uploads/')
100
                    ->imageUpload()
101
                ;
102
103
                /*** Password ***/
104
                $userEntity->setPlainPassword(
105
                    $userEntity->getPlainPassword(), // This getPassword() is here just the plain password. That's why we need to convert it
106
                    $app['security.encoder_factory']
107
                );
108
109
                $app['orm.em']->persist($userEntity);
110
                $app['orm.em']->flush();
111
112
                $app['flashbag']->add(
113
                    'success',
114
                    $app['translator']->trans(
115
                        'A new user was successfully created!'
116
                    )
117
                );
118
119
                return $app->redirect(
120
                    $app['url_generator']->generate(
121
                        'members-area.users.edit',
122
                        array(
123
                            'id' => $userEntity->getId(),
124
                        )
125
                    )
126
                );
127
            }
128
        }
129
130
        return new Response(
131
            $app['twig']->render(
132
                'contents/members-area/users/new.html.twig',
133
                array(
134
                    'form' => $form->createView(),
135
                )
136
            )
137
        );
138
    }
139
140
    /**
141
     * @param $id
142
     * @param Application $app
143
     *
144
     * @return Response
145
     */
146
    public function detailAction($id, Application $app)
147
    {
148
        if (
149
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
150
            !$app['security']->isGranted('ROLE_ADMIN')
151
        ) {
152
            $app->abort(403);
153
        }
154
155
        $user = $app['orm.em']->find('Application\Entity\UserEntity', $id);
156
157
        if (!$user) {
158
            $app->abort(404);
159
        }
160
161
        return new Response(
162
            $app['twig']->render(
163
                'contents/members-area/users/detail.html.twig',
164
                array(
165
                    'user' => $user,
166
                )
167
            )
168
        );
169
    }
170
171
    public function editAction($id, Request $request, Application $app)
172
    {
173
        if (
174
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
175
            !$app['security']->isGranted('ROLE_ADMIN')
176
        ) {
177
            $app->abort(403);
178
        }
179
180
        $user = $app['orm.em']->find(
181
            'Application\Entity\UserEntity',
182
            $id
183
        );
184
185
        if (!$user) {
186
            $app->abort(404);
187
        }
188
189
        $form = $app['form.factory']->create(
190
            new UserType($app),
191
            $user
192
        );
193
194
        if ($request->getMethod() == 'POST') {
195
            $form->handleRequest($request);
196
197
            if ($form->isValid()) {
198
                $userEntity = $form->getData();
199
200
                if (
201
                    $userEntity->isLocked() &&
202
                    $userEntity->hasRole('ROLE_SUPER_ADMIN')
203
                ) {
204
                    $app['flashbag']->add(
205
                        'danger',
206
                        $app['translator']->trans(
207
                            'A super admin user can not be locked!'
208
                        )
209
                    );
210
211
                    return $app->redirect(
212
                        $app['url_generator']->generate(
213
                            'members-area.users.edit',
214
                            array(
215
                                'id' => $userEntity->getId(),
216
                            )
217
                        )
218
                    );
219
                }
220
221
                if ($userEntity->getProfile()->getRemoveImage()) {
222
                    $userEntity->getProfile()->setImageUrl(null);
223
                }
224
225
                /*** Image ***/
226
                $userEntity
227
                    ->getProfile()
228
                    ->setImageUploadPath($app['baseUrl'].'/assets/uploads/')
229
                    ->setImageUploadDir(WEB_DIR.'/assets/uploads/')
230
                    ->imageUpload()
231
                ;
232
233
                /*** Password ***/
234
                if ($userEntity->getPlainPassword()) {
235
                    $userEntity->setPlainPassword(
236
                        $userEntity->getPlainPassword(),
237
                        $app['security.encoder_factory']
238
                    );
239
                }
240
241
                $app['orm.em']->persist($userEntity);
242
                $app['orm.em']->flush();
243
244
                $app['flashbag']->add(
245
                    'success',
246
                    $app['translator']->trans(
247
                        'The user was successfully edited!'
248
                    )
249
                );
250
251
                return $app->redirect(
252
                    $app['url_generator']->generate(
253
                        'members-area.users.edit',
254
                        array(
255
                            'id' => $userEntity->getId(),
256
                        )
257
                    )
258
                );
259
            }
260
        }
261
262
        return new Response(
263
            $app['twig']->render(
264
                'contents/members-area/users/edit.html.twig',
265
                array(
266
                    'form' => $form->createView(),
267
                    'user' => $user,
268
                )
269
            )
270
        );
271
    }
272
273 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...
274
    {
275
        if (
276
            !$app['security']->isGranted('ROLE_USERS_EDITOR') &&
277
            !$app['security']->isGranted('ROLE_ADMIN')
278
        ) {
279
            $app->abort(403);
280
        }
281
282
        $users = array();
283
        $ids = $request->query->get('ids', false);
284
        $idsExploded = explode(',', $ids);
285
        foreach ($idsExploded as $singleId) {
286
            $singleEntity = $app['orm.em']->find(
287
                'Application\Entity\UserEntity',
288
                $singleId
289
            );
290
291
            if ($singleEntity) {
292
                $users[] = $singleEntity;
293
            }
294
        }
295
296
        $user = $app['orm.em']->find('Application\Entity\UserEntity', $id);
297
298
        if (
299
            (
300
                !$user &&
301
                $ids === false
302
            ) ||
303
            (
304
                empty($users) &&
305
                $ids !== false
306
            )
307
        ) {
308
            $app->abort(404);
309
        }
310
311
        $confirmAction = $app['request']->query->has('action') && $app['request']->query->get('action') == 'confirm';
312
313
        if ($confirmAction) {
314
            try {
315
                if (!empty($users)) {
316
                    foreach ($users as $user) {
317
                        $app['orm.em']->remove($user);
318
                    }
319
                } else {
320
                    $app['orm.em']->remove($user);
321
                }
322
323
                $app['orm.em']->flush();
324
325
                $app['flashbag']->add(
326
                    'success',
327
                    $app['translator']->trans(
328
                        'The user "%user%" was successfully removed!',
329
                        array(
330
                            '%user%' => $user,
331
                        )
332
                    )
333
                );
334
            } catch (\Exception $e) {
335
                $app['flashbag']->add(
336
                    'danger',
337
                    $app['translator']->trans(
338
                        $e->getMessage()
339
                    )
340
                );
341
            }
342
343
            return $app->redirect(
344
                $app['url_generator']->generate('members-area.users')
345
            );
346
        }
347
348
        return new Response(
349
            $app['twig']->render(
350
                'contents/members-area/users/remove.html.twig',
351
                array(
352
                    'user' => $user,
353
                    'users' => $users,
354
                    'ids' => $ids,
355
                )
356
            )
357
        );
358
    }
359
}
360