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.

PostsController::editAction()   C
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 68
Code Lines 38

Duplication

Lines 37
Ratio 54.41 %

Importance

Changes 0
Metric Value
dl 37
loc 68
rs 6.9654
c 0
b 0
f 0
cc 7
eloc 38
nc 16
nop 3

How to fix   Long Method   

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\PostType;
6
use Application\Entity\PostEntity;
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 PostsController
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_POSTS_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
        $postResults = $app['orm.em']
35
            ->createQueryBuilder()
36
            ->select('p')
37
            ->from('Application\Entity\PostEntity', 'p')
38
            ->leftJoin('p.user', 'u')
39
        ;
40
41
        $pagination = $app['application.paginator']->paginate(
42
            $postResults,
43
            $currentPage,
44
            $limitPerPage,
45
            array(
46
                'route' => 'members-area.posts',
47
                'defaultSortFieldName' => 'p.timeCreated',
48
                'defaultSortDirection' => 'desc',
49
                'searchFields' => array(
50
                    'p.title',
51
                    'p.content',
52
                    'u.username',
53
                    'u.email',
54
                ),
55
            )
56
        );
57
58
        return new Response(
59
            $app['twig']->render(
60
                'contents/members-area/posts/list.html.twig',
61
                array(
62
                    'pagination' => $pagination,
63
                )
64
            )
65
        );
66
    }
67
68
    /**
69
     * @param Request     $request
70
     * @param Application $app
71
     *
72
     * @return Response
73
     */
74
    public function newAction(Request $request, Application $app)
75
    {
76
        if (
77
            !$app['security']->isGranted('ROLE_POSTS_EDITOR') &&
78
            !$app['security']->isGranted('ROLE_ADMIN')
79
        ) {
80
            $app->abort(403);
81
        }
82
83
        $form = $app['form.factory']->create(
84
            new PostType(),
85
            new PostEntity()
86
        );
87
88
        if ($request->getMethod() == 'POST') {
89
            $form->handleRequest($request);
90
91
            if ($form->isValid()) {
92
                $postEntity = $form->getData();
93
94
                /*** Image ***/
95
                $postEntity
96
                    ->setImageUploadPath($app['baseUrl'].'/assets/uploads/')
97
                    ->setImageUploadDir(WEB_DIR.'/assets/uploads/')
98
                    ->imageUpload()
99
                ;
100
101
                $app['orm.em']->persist($postEntity);
102
                $app['orm.em']->flush();
103
104
                $app['flashbag']->add(
105
                    'success',
106
                    $app['translator']->trans(
107
                        'A new post was successfully created!'
108
                    )
109
                );
110
111
                return $app->redirect(
112
                    $app['url_generator']->generate(
113
                        'members-area.posts.edit',
114
                        array(
115
                            'id' => $postEntity->getId(),
116
                        )
117
                    )
118
                );
119
            }
120
        }
121
122
        return new Response(
123
            $app['twig']->render(
124
                'contents/members-area/posts/new.html.twig',
125
                array(
126
                    'form' => $form->createView(),
127
                )
128
            )
129
        );
130
    }
131
132
    /**
133
     * @param $id
134
     * @param Request     $request
135
     * @param Application $app
136
     *
137
     * @return Response
138
     */
139
    public function editAction($id, Request $request, Application $app)
140
    {
141
        if (
142
            !$app['security']->isGranted('ROLE_POSTS_EDITOR') &&
143
            !$app['security']->isGranted('ROLE_ADMIN')
144
        ) {
145
            $app->abort(403);
146
        }
147
148
        $post = $app['orm.em']->find('Application\Entity\PostEntity', $id);
149
150
        if (!$post) {
151
            $app->abort(404);
152
        }
153
154
        $form = $app['form.factory']->create(
155
            new PostType(),
156
            $post
157
        );
158
159 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...
160
            $form->handleRequest($request);
161
162
            if ($form->isValid()) {
163
                $postEntity = $form->getData();
164
165
                if ($postEntity->getRemoveImage()) {
166
                    $postEntity->setImageUrl(null);
167
                }
168
169
                /*** Image ***/
170
                $postEntity
171
                    ->setImageUploadPath($app['baseUrl'].'/assets/uploads/')
172
                    ->setImageUploadDir(WEB_DIR.'/assets/uploads/')
173
                    ->imageUpload()
174
                ;
175
176
                $app['orm.em']->persist($postEntity);
177
                $app['orm.em']->flush();
178
179
                $app['flashbag']->add(
180
                    'success',
181
                    $app['translator']->trans(
182
                        'The post was successfully edited!'
183
                    )
184
                );
185
186
                return $app->redirect(
187
                    $app['url_generator']->generate(
188
                        'members-area.posts.edit',
189
                        array(
190
                            'id' => $postEntity->getId(),
191
                        )
192
                    )
193
                );
194
            }
195
        }
196
197
        return new Response(
198
            $app['twig']->render(
199
                'contents/members-area/posts/edit.html.twig',
200
                array(
201
                    'form' => $form->createView(),
202
                    'post' => $post,
203
                )
204
            )
205
        );
206
    }
207
208
    /**
209
     * @param $id
210
     * @param Request     $request
211
     * @param Application $app
212
     *
213
     * @return Response
214
     */
215 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...
216
    {
217
        if (
218
            !$app['security']->isGranted('ROLE_POSTS_EDITOR') &&
219
            !$app['security']->isGranted('ROLE_ADMIN')
220
        ) {
221
            $app->abort(403);
222
        }
223
224
        $posts = array();
225
        $ids = $request->query->get('ids', false);
226
        $idsExploded = explode(',', $ids);
227
        foreach ($idsExploded as $singleId) {
228
            $singleEntity = $app['orm.em']->find(
229
                'Application\Entity\PostEntity',
230
                $singleId
231
            );
232
233
            if ($singleEntity) {
234
                $posts[] = $singleEntity;
235
            }
236
        }
237
238
        $post = $app['orm.em']->find('Application\Entity\PostEntity', $id);
239
240
        if (
241
            (
242
                !$post &&
243
                $ids === false
244
            ) ||
245
            (
246
                empty($posts) &&
247
                $ids !== false
248
            )
249
        ) {
250
            $app->abort(404);
251
        }
252
253
        $confirmAction = $app['request']->query->has('action') &&
254
            $app['request']->query->get('action') == 'confirm'
255
        ;
256
257
        if ($confirmAction) {
258
            try {
259
                if (!empty($posts)) {
260
                    foreach ($posts as $post) {
261
                        $app['orm.em']->remove($post);
262
                    }
263
                } else {
264
                    $app['orm.em']->remove($post);
265
                }
266
267
                $app['orm.em']->flush();
268
269
                $app['flashbag']->add(
270
                    'success',
271
                    $app['translator']->trans(
272
                        'The post "%post%" was successfully removed!',
273
                        array(
274
                            '%post%' => $post,
275
                        )
276
                    )
277
                );
278
            } catch (\Exception $e) {
279
                $app['flashbag']->add(
280
                    'danger',
281
                    $app['translator']->trans(
282
                        $e->getMessage()
283
                    )
284
                );
285
            }
286
287
            return $app->redirect(
288
                $app['url_generator']->generate(
289
                    'members-area.posts'
290
                )
291
            );
292
        }
293
294
        return new Response(
295
            $app['twig']->render(
296
                'contents/members-area/posts/remove.html.twig',
297
                array(
298
                    'post' => $post,
299
                    'posts' => $posts,
300
                    'ids' => $ids,
301
                )
302
            )
303
        );
304
    }
305
}
306