ThreadController   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 149
Duplicated Lines 26.85 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 7
dl 40
loc 149
ccs 45
cts 50
cp 0.9
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFieldsAction() 0 4 1
B cgetAction() 5 33 4
A getAction() 0 9 2
A putAction() 15 15 2
A deleteAction() 7 7 1
A cdeleteAction() 13 13 2
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\Routing\ClassResourceInterface;
16
use Sulu\Bundle\CommentBundle\Entity\ThreadInterface;
17
use Sulu\Component\Rest\Exception\EntityNotFoundException;
18
use Sulu\Component\Rest\ListBuilder\FieldDescriptorInterface;
19
use Sulu\Component\Rest\ListBuilder\ListRepresentation;
20
use Sulu\Component\Rest\RequestParametersTrait;
21
use Sulu\Component\Rest\RestController;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
25
/**
26
 * Provides an api for threads.
27
 */
28
class ThreadController extends RestController implements ClassResourceInterface
29
{
30
    use RequestParametersTrait;
31
32
    /**
33
     * Returns list of field-descriptors.
34
     *
35
     * @Get("/threads/fields")
36
     *
37
     * @return Response
38
     */
39
    public function getFieldsAction()
40
    {
41
        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...oller\ThreadController>, 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...
42
    }
43
44
    /**
45
     * Returns list of threads.
46
     *
47
     * @param Request $request
48
     *
49
     * @return Response
50
     */
51 3
    public function cgetAction(Request $request)
52
    {
53 3
        $restHelper = $this->get('sulu_core.doctrine_rest_helper');
54 3
        $factory = $this->get('sulu_core.doctrine_list_builder_factory');
55 3
        $listBuilder = $factory->create($this->getParameter('sulu.model.thread.class'));
56
57 3
        $fieldDescriptors = $this->getFieldDescriptors();
58 3
        $restHelper->initializeListBuilder($listBuilder, $fieldDescriptors);
59
60 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...
61 3
            if (isset($fieldDescriptors[$filterKey])) {
62 3
                $listBuilder->where($fieldDescriptors[$filterKey], $filterValue);
63
            }
64
        }
65
66 3
        $typeParameter = $request->get('types');
67 3
        if ($typeParameter) {
68 1
            $listBuilder->in($fieldDescriptors['type'], array_filter(explode(',', $typeParameter)));
69
        }
70
71 3
        $items = $listBuilder->execute();
72 3
        $list = new ListRepresentation(
73
            $items,
74 3
            'threads',
75 3
            'get_threads',
76 3
            $request->query->all(),
77 3
            $listBuilder->getCurrentPage(),
78 3
            $listBuilder->getLimit(),
79 3
            $listBuilder->count()
80
        );
81
82 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...oller\ThreadController>, 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...
83
    }
84
85
    /**
86
     * Returns single thread.
87
     *
88
     * @param int $id
89
     *
90
     * @return Response
91
     *
92
     * @throws EntityNotFoundException
93
     */
94 1
    public function getAction($id)
95
    {
96 1
        $thread = $this->get('sulu.repository.thread')->findThreadById($id);
97 1
        if (!$thread) {
98
            throw new EntityNotFoundException(ThreadInterface::class, $id);
99
        }
100
101 1
        return $this->handleView($this->view($thread));
0 ignored issues
show
Documentation introduced by
$this->view($thread) is of type this<Sulu\Bundle\Comment...oller\ThreadController>, 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...
102
    }
103
104
    /**
105
     * Update thread.
106
     *
107
     * @param int $id
108
     * @param Request $request
109
     *
110
     * @return Response
111
     *
112
     * @throws EntityNotFoundException
113
     */
114 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...
115
    {
116
        /** @var ThreadInterface $thread */
117 1
        $thread = $this->get('sulu.repository.thread')->findThreadById($id);
118 1
        if (!$thread) {
119
            throw new EntityNotFoundException(ThreadInterface::class, $id);
120
        }
121
122 1
        $thread->setTitle($request->request->get('title'));
123
124 1
        $this->get('sulu_comment.manager')->updateThread($thread);
125 1
        $this->get('doctrine.orm.entity_manager')->flush();
126
127 1
        return $this->handleView($this->view($thread));
0 ignored issues
show
Documentation introduced by
$this->view($thread) is of type this<Sulu\Bundle\Comment...oller\ThreadController>, 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...
128
    }
129
130
    /**
131
     * Delete thread identified by id.
132
     *
133
     * @param int $id
134
     *
135
     * @return Response
136
     */
137 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...
138
    {
139 1
        $this->get('sulu_comment.manager')->deleteThreads([$id]);
140 1
        $this->get('doctrine.orm.entity_manager')->flush();
141
142 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...oller\ThreadController>, 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...
143
    }
144
145
    /**
146
     * Delete multiple threads identified by ids parameter.
147
     *
148
     * @param Request $request
149
     *
150
     * @return Response
151
     */
152 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...
153
    {
154 1
        $ids = array_filter(explode(',', $request->get('ids', '')));
155
156 1
        if (0 === count($ids)) {
157
            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...oller\ThreadController>, 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...
158
        }
159
160 1
        $this->get('sulu_comment.manager')->deleteThreads($ids);
161 1
        $this->get('doctrine.orm.entity_manager')->flush();
162
163 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...oller\ThreadController>, 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...
164
    }
165
166
    /**
167
     * Returns array of field-descriptors.
168
     *
169
     * @return FieldDescriptorInterface[]
170
     */
171 3
    private function getFieldDescriptors()
172
    {
173 3
        return $this->get('sulu_core.list_builder.field_descriptor_factory')
174 3
            ->getFieldDescriptorForClass($this->getParameter('sulu.model.thread.class'));
175
    }
176
}
177