CommentController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 194
Duplicated Lines 20.62 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 9
dl 40
loc 194
ccs 60
cts 66
cp 0.9091
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldsAction() 0 4 1
B cgetAction() 5 37 4
A getAction() 0 9 2
A putAction() 15 15 2
A deleteAction() 7 7 1
A cdeleteAction() 13 13 2
B postTriggerAction() 0 26 3
A getFieldDescriptors() 0 5 1

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
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Bundle\CommentBundle\Controller;
13
14
use FOS\RestBundle\Controller\Annotations\Get;
15
use FOS\RestBundle\Controller\Annotations\Post;
16
use FOS\RestBundle\Routing\ClassResourceInterface;
17
use Sulu\Bundle\CommentBundle\Entity\CommentInterface;
18
use Sulu\Bundle\CommentBundle\Entity\CommentRepositoryInterface;
19
use Sulu\Component\Rest\Exception\EntityNotFoundException;
20
use Sulu\Component\Rest\Exception\RestException;
21
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface;
22
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
23
use Sulu\Component\Rest\RequestParametersTrait;
24
use Sulu\Component\Rest\RestController;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\HttpFoundation\Response;
27
28
/**
29
 * Provides an api for comments.
30
 */
31
class CommentController extends RestController implements ClassResourceInterface
32
{
33
    use RequestParametersTrait;
34
35
    /**
36
     * Returns list of field-descriptors.
37
     *
38
     * @Get("/comments/fields")
39
     *
40
     * @return Response
41
     */
42
    public function getFieldsAction()
43
    {
44
        return $this->handleView($this->view($this->getFieldDescriptors()));
0 ignored issues
show
Documentation introduced by
$this->view($this->getFieldDescriptors()) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
    }
46
47
    /**
48
     * Returns list of comments.
49
     *
50
     * @param Request $request
51
     *
52
     * @return Response
53
     */
54 3
    public function cgetAction(Request $request)
55
    {
56 3
        $restHelper = $this->get('sulu_core.doctrine_rest_helper');
57 3
        $factory = $this->get('sulu_core.doctrine_list_builder_factory');
58 3
        $listBuilder = $factory->create($this->getParameter('sulu.model.comment.class'));
59
60 3
        $fieldDescriptors = $this->getFieldDescriptors();
61 3
        $restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
62
63 3
        if ($request->query->get('threadType')) {
64 1
            $listBuilder->in(
65 1
                $fieldDescriptors['threadType'],
66 1
                array_filter(explode(',', $request->query->get('threadType')))
67
            );
68
69 1
            $request->query->remove('threadType');
70
        }
71
72 3 View Code Duplication
        foreach ($request->query->all() as $filterKey => $filterValue) {
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...
73 1
            if (isset($fieldDescriptors[$filterKey])) {
74 1
                $listBuilder->where($fieldDescriptors[$filterKey], $filterValue);
75
            }
76
        }
77
78 3
        $results = $listBuilder->execute();
79 3
        $list = new ListRepresentation(
80
            $results,
81 3
            'comments',
82 3
            'get_comments',
83 3
            $request->query->all(),
84 3
            $listBuilder->getCurrentPage(),
85 3
            $listBuilder->getLimit(),
86 3
            $listBuilder->count()
87
        );
88
89 3
        return $this->handleView($this->view($list, 200));
0 ignored issues
show
Documentation introduced by
$this->view($list, 200) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
    }
91
92
    /**
93
     * Returns single comment.
94
     *
95
     * @param int $id
96
     *
97
     * @return Response
98
     *
99
     * @throws EntityNotFoundException
100
     */
101 1
    public function getAction($id)
102
    {
103 1
        $comment = $this->get('sulu.repository.comment')->findCommentById($id);
104 1
        if (!$comment) {
105
            throw new EntityNotFoundException(CommentInterface::class, $id);
106
        }
107
108 1
        return $this->handleView($this->view($comment));
0 ignored issues
show
Documentation introduced by
$this->view($comment) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
    }
110
111
    /**
112
     * Update comment.
113
     *
114
     * @param int $id
115
     * @param Request $request
116
     *
117
     * @return Response
118
     *
119
     * @throws EntityNotFoundException
120
     */
121 1 View Code Duplication
    public function putAction($id, Request $request)
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...
122
    {
123
        /** @var CommentInterface $comment */
124 1
        $comment = $this->get('sulu.repository.comment')->findCommentById($id);
125 1
        if (!$comment) {
126
            throw new EntityNotFoundException(CommentInterface::class, $id);
127
        }
128
129 1
        $comment->setMessage($request->request->get('message'));
130
131 1
        $this->get('sulu_comment.manager')->update($comment);
132 1
        $this->get('doctrine.orm.entity_manager')->flush();
133
134 1
        return $this->handleView($this->view($comment));
0 ignored issues
show
Documentation introduced by
$this->view($comment) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
135
    }
136
137
    /**
138
     * Delete comment identified by id.
139
     *
140
     * @param int $id
141
     *
142
     * @return Response
143
     */
144 1 View Code Duplication
    public function deleteAction($id)
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...
145
    {
146 1
        $this->get('sulu_comment.manager')->delete([$id]);
147 1
        $this->get('doctrine.orm.entity_manager')->flush();
148
149 1
        return $this->handleView($this->view(null, 204));
0 ignored issues
show
Documentation introduced by
$this->view(null, 204) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
    }
151
152
    /**
153
     * Delete multiple comments identified by ids parameter.
154
     *
155
     * @param Request $request
156
     *
157
     * @return Response
158
     */
159 1 View Code Duplication
    public function cdeleteAction(Request $request)
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...
160
    {
161 1
        $ids = array_filter(explode(',', $request->get('ids', '')));
162
163 1
        if (0 === count($ids)) {
164
            return $this->handleView($this->view(null, 204));
0 ignored issues
show
Documentation introduced by
$this->view(null, 204) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
165
        }
166
167 1
        $this->get('sulu_comment.manager')->delete($ids);
168 1
        $this->get('doctrine.orm.entity_manager')->flush();
169
170 1
        return $this->handleView($this->view(null, 204));
0 ignored issues
show
Documentation introduced by
$this->view(null, 204) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
171
    }
172
173
    /**
174
     * trigger a action for given comment specified over action get-parameter
175
     * - publish: Publish a comment
176
     * - unpublish: Unpublish a comment.
177
     *
178
     * @Post("/comments/{id}")
179
     *
180
     * @param int $id
181
     * @param Request $request
182
     *
183
     * @return Response
184
     *
185
     * @throws RestException
186
     */
187 2
    public function postTriggerAction($id, Request $request)
188
    {
189 2
        $action = $this->getRequestParameter($request, 'action', true);
190
191
        /** @var CommentRepositoryInterface $commentRepository */
192 2
        $commentRepository = $this->get('sulu.repository.comment');
193 2
        $commentManager = $this->get('sulu_comment.manager');
194 2
        $comment = $commentRepository->findCommentById($id);
195
196
        switch ($action) {
197 2
            case 'unpublish':
198 1
                $commentManager->unpublish($comment);
199 1
200 1
                break;
201 1
            case 'publish':
202 1
                $commentManager->publish($comment);
203
204
                break;
205
            default:
206
                throw new RestException('Unrecognized action: ' . $action);
207 2
        }
208
209 2
        $this->get('doctrine.orm.entity_manager')->flush();
210
211
        return $this->handleView($this->view($comment));
0 ignored issues
show
Documentation introduced by
$this->view($comment) is of type this<Sulu\Bundle\Comment...ller\CommentController>, but the function expects a object<FOS\RestBundle\View\View>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
212
    }
213
214
    /**
215
     * Returns array of field-descriptors.
216
     *
217 3
     * @return FieldDescriptorInterface[]
218
     */
219 3
    private function getFieldDescriptors()
220 3
    {
221
        return $this->get('sulu_core.list_builder.field_descriptor_factory')
222
            ->getFieldDescriptorForClass($this->getParameter('sulu.model.comment.class'));
223
    }
224
}
225