PerformancesController::getRolesAction()   B
last analyzed

Complexity

Conditions 6
Paths 11

Size

Total Lines 42

Duplication

Lines 17
Ratio 40.48 %

Code Coverage

Tests 23
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 2
dl 17
loc 42
ccs 23
cts 23
cp 1
crap 6
rs 8.6257
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Model\Link;
6
use App\Model\PaginationLinks;
7
use App\Model\PerformancesResponse;
8
use FOS\RestBundle\Controller\Annotations\QueryParam;
9
use FOS\RestBundle\Request\ParamFetcher;
10
use Nelmio\ApiDocBundle\Annotation\Model;
11
use Swagger\Annotations as SWG;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/api/performances")
17
 */
18
class PerformancesController extends AbstractController
19
{
20
    /**
21
     * @Route("", name="get_performances", methods={"GET"})
22
     * @SWG\Response(
23
     *     response=200,
24
     *     description="Returns when all parameters were correct",
25
     *     @SWG\Schema(
26
     *         type="array",
27
     *         @SWG\Items(ref=@Model(type=PerformancesResponse::class))
28
     *     )
29
     * )
30
     * @SWG\Response(
31
     *     response=404,
32
     *     description="Returns when the entity is not found",
33
     * )
34
     *
35
     * @QueryParam(name="limit", requirements="\d+", default="10", description="Count entries at one page")
36
     * @QueryParam(name="page", requirements="\d+", default="1", description="Number of page to be shown")
37
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
38
     */
39 2
    public function getList(ParamFetcher $paramFetcher)
40
    {
41 2
        $em = $this->getDoctrine()->getManager();
42
43
        $performances = $em
44 2
            ->getRepository('App:Performance')
45 2
            ->findBy(
46 2
                ['festival' => null],
47 2
                ['premiere' => 'DESC'],
48 2
                $paramFetcher->get('limit'),
49 2
                ($paramFetcher->get('page')-1) * $paramFetcher->get('limit')
50
            );
51
52 2
        $performancesTranslated = array();
53
54 2 View Code Duplication
        foreach ($performances as $performance) {
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...
55 2
            $performance->setLocale($paramFetcher->get('locale'));
56 2
            $em->refresh($performance);
57
58 2
            if ($performance->getTranslations()) {
59 2
                $performance->unsetTranslations();
60
            }
61
62 2
            $performancesTranslated[] = $performance;
63
        }
64
65 2
        $performances = $performancesTranslated;
66
67 2
        $performancesResponse = new PerformancesResponse();
68 2
        $performancesResponse->setPerformances($performances);
69 2
        $performancesResponse->setTotalCount($this->getDoctrine()->getManager()->getRepository('App:Performance')->getCount());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getCount() does only exist in the following implementations of said interface: App\Repository\AbstractRepository, App\Repository\EmployeeRepository, App\Repository\HistoryRepository, App\Repository\PerformanceEventRepository, App\Repository\PerformanceRepository, App\Repository\PostRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
70 2
        $performancesResponse->setPageCount(ceil($performancesResponse->getTotalCount() / $paramFetcher->get('limit')));
71 2
        $performancesResponse->setPage($paramFetcher->get('page'));
72
73 2
        $self = $this->generateUrl('get_performances', [
74 2
            'locale' => $paramFetcher->get('locale'),
75 2
            'limit' => $paramFetcher->get('limit'),
76 2
            'page' => $paramFetcher->get('page'),
77 2
        ], true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
78
        );
79
80 2
        $first = $this->generateUrl('get_performances', [
81 2
            'locale' => $paramFetcher->get('locale'),
82 2
            'limit' => $paramFetcher->get('limit'),
83 2
        ], true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
84
        );
85
86 2
        $nextPage = $paramFetcher->get('page') < $performancesResponse->getPageCount() ?
87 1
            $this->generateUrl('get_performances', [
88 1
                'locale' => $paramFetcher->get('locale'),
89 1
                'limit' => $paramFetcher->get('limit'),
90 1
                'page' => $paramFetcher->get('page')+1,
91 1
            ], true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
92
            ) :
93 2
            'false';
94
95 2
        $previsiousPage = $paramFetcher->get('page') > 1 ?
96
            $this->generateUrl('get_performances', [
97
                'locale' => $paramFetcher->get('locale'),
98
                'limit' => $paramFetcher->get('limit'),
99
                'page' => $paramFetcher->get('page')-1,
100
            ], true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
101
            ) :
102 2
            'false';
103
104 2
        $last = $this->generateUrl('get_performances', [
105 2
            'locale' => $paramFetcher->get('locale'),
106 2
            'limit' => $paramFetcher->get('limit'),
107 2
            'page' => $performancesResponse->getPageCount(),
108 2
        ], true
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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 2
        $links = new PaginationLinks();
112
113 2
        $performancesResponse->setLinks($links->setSelf(new Link($self)));
114 2
        $performancesResponse->setLinks($links->setFirst(new Link($first)));
115 2
        $performancesResponse->setLinks($links->setNext(new Link($nextPage)));
116 2
        $performancesResponse->setLinks($links->setPrev(new Link($previsiousPage)));
117 2
        $performancesResponse->setLinks($links->setLast(new Link($last)));
118
119 2
        foreach ($performances as $performance) {
120 2
            $performance->setLinks([
121 2
                ['rel' => 'self', 'href' => $this->generateUrl('get_performance', ['slug' => $performance->getSlug()], true)],
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
122 2
                ['rel' => 'self.roles', 'href' => $this->generateUrl('get_performance_roles', ['slug' => $performance->getSlug()], true)],
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
123 2
                ['rel' => 'self.events', 'href' => $this->generateUrl('get_performanceevents', ['performance' => $performance->getSlug()], true)],
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

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...
124
            ]);
125
        }
126
127 2
        return $performancesResponse;
128
    }
129
130
    /**
131
     * @Route("/{slug}", name="get_performance", methods={"GET"})
132
     * @SWG\Response(
133
     *     response=200,
134
     *     description="Returns Performance by unique property {slug}",
135
     *     @Model(type=Performance::class)
136
     * )
137
     * @SWG\Response(
138
     *     response=404,
139
     *     description="Returns when Performance was not found in database",
140
     * )
141
     *
142
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
143
     */
144 1 View Code Duplication
    public function getAction(ParamFetcher $paramFetcher, $slug)
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
        $em = $this->getDoctrine()->getManager();
147
148
        $performance = $em
149 1
            ->getRepository('App:Performance')->findOneByslug($slug);
150
151 1
        if (!$performance) {
152 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
153
        }
154
155 1
        $performance->setLocale($paramFetcher->get('locale'));
156 1
        $em->refresh($performance);
157
158 1
        if ($performance->getTranslations()) {
159 1
            $performance->unsetTranslations();
160
        }
161
162 1
        return $performance;
163
    }
164
165
    /**
166
     * @Route("/{slug}/roles", name="get_performance_roles", methods={"GET"})
167
     * @SWG\Response(
168
     *     response=200,
169
     *     description="Returns Performance roles by his unique {slug}",
170
     *     @SWG\Schema(
171
     *         type="array",
172
     *         @SWG\Items(ref=@Model(type=Role::class))
173
     *     )
174
     * )
175
     * @SWG\Response(
176
     *     response=404,
177
     *     description="Returns when Performance by slug was not found in database",
178
     * )
179
     *
180
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
181
     */
182 1
    public function getRolesAction(ParamFetcher $paramFetcher, $slug)
183
    {
184 1
        $em = $this->getDoctrine()->getManager();
185
186
        $performance = $em
187 1
            ->getRepository('App:Performance')->findOneByslug($slug);
188
189 1
        if (!$performance) {
190 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
191
        }
192
193 1
        $performance->setLocale($paramFetcher->get('locale'));
194 1
        $em->refresh($performance);
195
196 1
        if ($performance->getTranslations()) {
197 1
            $performance->unsetTranslations();
198
        }
199
200 1
        $roles = $performance->getRoles();
201 1
        $rolesTrans = [];
202
203 1 View Code Duplication
        foreach ($roles as $role) {
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...
204 1
            $role->setLocale($paramFetcher->get('locale'));
205 1
            $em->refresh($role);
206
207 1
            if ($role->getTranslations()) {
208 1
                $role->unsetTranslations();
209
            }
210
211 1
            $role->getEmployee()->setLocale($paramFetcher->get('locale'));
212 1
            $em->refresh($role->getEmployee());
213
214 1
            if ($role->getEmployee()->getTranslations()) {
215 1
                $role->getEmployee()->unsetTranslations();
216
            }
217
218 1
            $rolesTrans[] = $role;
219
        }
220 1
        $roles = $rolesTrans;
221
222 1
        return $roles;
223
    }
224
225
    /**
226
     * @Route("/{slug}/performanceevents", name="get_performance_performance_events", methods={"GET"})
227
     * @SWG\Response(
228
     *     response=200,
229
     *     description="Returns Performance events by Performance {slug}",
230
     *     @SWG\Schema(
231
     *         type="array",
232
     *         @SWG\Items(ref=@Model(type=PerformanceEvent::class))
233
     *     )
234
     * )
235
     * @SWG\Response(
236
     *     response=404,
237
     *     description="Returns when Performance by {slug} was not found in database",
238
     * )
239
     * @SWG\Get(deprecated=true)
240
     *
241
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
242
     */
243 1
    public function getPerformanceEventsAction(ParamFetcher $paramFetcher, $slug)
244
    {
245 1
        $em = $this->getDoctrine()->getManager();
246
247 1
        $performance = $em->getRepository('App:Performance')->findOneByslug($slug);
248
249 1
        if (!$performance) {
250 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
251
        }
252
253 1
        $performance->setLocale($paramFetcher->get('locale'));
254 1
        $em->refresh($performance);
255
256 1
        if ($performance->getTranslations()) {
257 1
            $performance->unsetTranslations();
258
        }
259
260 1
        $performanceEvents = $performance->getPerformanceEvents();
261 1
        $performanceEventsTrans = [];
262
263 1 View Code Duplication
        foreach ($performanceEvents as $performanceEvent) {
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...
264 1
            $performanceEvent->setLocale($paramFetcher->get('locale'));
265 1
            $em->refresh($performanceEvent);
266 1
            if ($performanceEvent->getTranslations()) {
267 1
                $performanceEvent->unsetTranslations();
268
            }
269 1
            $performanceEventsTrans[] = $performanceEvent;
270
        }
271 1
        $performanceEvents = $performanceEventsTrans;
272
273 1
        return $performanceEvents;
274
    }
275
}
276