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

PostsController   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 293
Duplicated Lines 68.94 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 8
Bugs 1 Features 2
Metric Value
wmc 28
c 8
b 1
f 2
lcom 0
cbo 6
dl 202
loc 293
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B newAction() 33 59 5
B editAction() 33 66 6
D removeAction() 89 89 14
A listAction() 47 47 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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