Completed
Pull Request — master (#164)
by Ihor
25:00 queued 13:13
created

PerformancesController   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 313
Duplicated Lines 6.39 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 95.38%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 20
loc 313
ccs 124
cts 130
cp 0.9538
rs 10
wmc 20
lcom 1
cbo 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAction() 20 20 3
B getPerformanceeventsAction() 0 32 5
B getRolesAction() 0 42 6
B cgetAction() 0 126 6

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 AppBundle\Controller;
4
5
use AppBundle\Model\Link;
6
use AppBundle\Model\PaginationLinks;
7
use FOS\RestBundle\Request\ParamFetcher;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use FOS\RestBundle\Controller\Annotations\View as RestView;
10
use FOS\RestBundle\Controller\Annotations\RouteResource;
11
use FOS\RestBundle\Controller\Annotations\QueryParam;
12
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
13
use AppBundle\Model\PerformancesResponse;
14
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15
16
/**
17
 * @RouteResource("Performance")
18
 */
19
class PerformancesController extends Controller
20
{
21
    /**
22
     * @ApiDoc(
23
     * resource=true,
24
     *  description="Returns a collection of Performances",
25
     *  statusCodes={
26
     *      200="Returned when all parameters were correct",
27
     *      404="Returned when the entity is not found",
28
     *  },
29
     *  output = "array<AppBundle\Model\PerformancesResponse>"
30
     * )
31
     *
32
     * @QueryParam(name="limit", requirements="\d+", default="10", description="Count entries at one page")
33
     * @QueryParam(name="page", requirements="\d+", default="1", description="Number of page to be shown")
34
     * @QueryParam(
35
     *     name="locale",
36
     *     requirements="^[a-zA-Z]+",
37
     *     default="uk",
38
     *     description="Selects language of data you want to receive"
39
     * )
40
     *
41
     * @RestView
42
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
43
     */
44 2
    public function cgetAction(ParamFetcher $paramFetcher)
45
    {
46 2
        $em = $this->getDoctrine()->getManager();
47
48 2
        $performances = $em->getRepository('AppBundle:Performance')->findBy(
49 2
            ['festival' => null],
50 2
            ['premiere' => 'DESC'],
51 2
            $paramFetcher->get('limit'),
52 2
            ($paramFetcher->get('page')-1) * $paramFetcher->get('limit')
53
        );
54
55 2
        $performancesTranslated = array();
56
57 2
        foreach ($performances as $performance) {
58 2
            $performance->setLocale($paramFetcher->get('locale'));
59 2
            $em->refresh($performance);
60
61 2
            if ($performance->getTranslations()) {
62 2
                $performance->unsetTranslations();
63
            }
64
65 2
            $performancesTranslated[] = $performance;
66
        }
67
68 2
        $performances = $performancesTranslated;
69
70 2
        $performancesResponse = new PerformancesResponse();
71 2
        $performancesResponse->setPerformances($performances);
72 2
        $performancesResponse->setTotalCount(
73 2
            $this->getDoctrine()->getManager()->getRepository('AppBundle:Performance')->getCount()
74
        );
75 2
        $performancesResponse->setPageCount(ceil($performancesResponse->getTotalCount() / $paramFetcher->get('limit')));
76 2
        $performancesResponse->setPage($paramFetcher->get('page'));
77
78 2
        $self = $this->generateUrl(
79 2
            'get_performances',
80
            [
81 2
                'locale' => $paramFetcher->get('locale'),
82 2
                'limit' => $paramFetcher->get('limit'),
83 2
                'page' => $paramFetcher->get('page'),
84
            ],
85 2
            UrlGeneratorInterface::ABSOLUTE_URL
86
        );
87
88 2
        $first = $this->generateUrl(
89 2
            'get_performances',
90
            [
91 2
                'locale' => $paramFetcher->get('locale'),
92 2
                'limit' => $paramFetcher->get('limit'),
93
            ],
94 2
            UrlGeneratorInterface::ABSOLUTE_URL
95
        );
96
97 2
        $nextPage = $paramFetcher->get('page') < $performancesResponse->getPageCount() ?
98 1
            $this->generateUrl(
99 1
                'get_performances',
100
                [
101 1
                    'locale' => $paramFetcher->get('locale'),
102 1
                    'limit' => $paramFetcher->get('limit'),
103 1
                    'page' => $paramFetcher->get('page')+1,
104
                ],
105 1
                UrlGeneratorInterface::ABSOLUTE_URL
106
            ) :
107 2
            'false';
108
109 2
        $previsiousPage = $paramFetcher->get('page') > 1 ?
110
            $this->generateUrl(
111
                'get_performances',
112
                [
113
                    'locale' => $paramFetcher->get('locale'),
114
                    'limit' => $paramFetcher->get('limit'),
115
                    'page' => $paramFetcher->get('page')-1,
116
                ],
117
                UrlGeneratorInterface::ABSOLUTE_URL
118
            ) :
119 2
            'false';
120
121 2
        $last = $this->generateUrl(
122 2
            'get_performances',
123
            [
124 2
                'locale' => $paramFetcher->get('locale'),
125 2
                'limit' => $paramFetcher->get('limit'),
126 2
                'page' => $performancesResponse->getPageCount(),
127
            ],
128 2
            UrlGeneratorInterface::ABSOLUTE_URL
129
        );
130
131 2
        $links = new PaginationLinks();
132
133 2
        $performancesResponse->setLinks($links->setSelf(new Link($self)));
134 2
        $performancesResponse->setLinks($links->setFirst(new Link($first)));
135 2
        $performancesResponse->setLinks($links->setNext(new Link($nextPage)));
136 2
        $performancesResponse->setLinks($links->setPrev(new Link($previsiousPage)));
137 2
        $performancesResponse->setLinks($links->setLast(new Link($last)));
138
139 2
        foreach ($performances as $performance) {
140 2
            $performance->setLinks([
141
                [
142 2
                    'rel' => 'self',
143 2
                    'href' => $this->generateUrl(
144 2
                        'get_performance',
145 2
                        ['slug' => $performance->getSlug()],
146 2
                        UrlGeneratorInterface::ABSOLUTE_URL
147
                    )
148
                ],
149
                [
150 2
                    'rel' => 'self.roles',
151 2
                    'href' => $this->generateUrl(
152 2
                        'get_performance_roles',
153 2
                        ['slug' => $performance->getSlug()],
154 2
                        UrlGeneratorInterface::ABSOLUTE_URL
155
                    )
156
                ],
157
                [
158 2
                    'rel' => 'self.events',
159 2
                    'href' => $this->generateUrl(
160 2
                        'get_performanceevents',
161 2
                        ['performance' => $performance->getSlug()],
162 2
                        UrlGeneratorInterface::ABSOLUTE_URL
163
                    )
164
                ],
165
            ]);
166
        }
167
168 2
        return $performancesResponse;
169
    }
170
171
    /**
172
     * @ApiDoc(
173
     * resource=true,
174
     *  description="Returns Performance by unique property {slug}",
175
     *  statusCodes={
176
     *      200="Returned when Performance was found in database",
177
     *      404="Returned when Performance was not found in database",
178
     *  },
179
     *  output = "AppBundle\Entity\Performance"
180
     * )
181
     *
182
     * @QueryParam(
183
     *     name="locale",
184
     *     requirements="^[a-zA-Z]+",
185
     *     default="uk",
186
     *     description="Selects language of data you want to receive"
187
     * )
188
     *
189
     * @RestView
190
     */
191 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...
192
    {
193 1
        $em = $this->getDoctrine()->getManager();
194
195
        $performance = $em
196 1
            ->getRepository('AppBundle:Performance')->findOneByslug($slug);
197
198 1
        if (!$performance) {
199 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
200
        }
201
202 1
        $performance->setLocale($paramFetcher->get('locale'));
203 1
        $em->refresh($performance);
204
205 1
        if ($performance->getTranslations()) {
206 1
            $performance->unsetTranslations();
207
        }
208
209 1
        return $performance;
210
    }
211
212
    /**
213
     * @ApiDoc(
214
     * resource=true,
215
     *  description="Returns Performance roles by his unique {slug}",
216
     *  statusCodes={
217
     *      200="Returned when Performance by slug was found in database",
218
     *      404="Returned when Performance by slug was not found in database",
219
     *  },
220
     *  output = "array<AppBundle\Entity\Role>"
221
     * )
222
     *
223
     * @QueryParam(
224
     *     name="locale",
225
     *     requirements="^[a-zA-Z]+",
226
     *     default="uk",
227
     *     description="Selects language of data you want to receive"
228
     * )
229
     *
230
     * @RestView
231
     */
232 1
    public function getRolesAction(ParamFetcher $paramFetcher, $slug)
233
    {
234 1
        $em = $this->getDoctrine()->getManager();
235
236
        $performance = $em
237 1
            ->getRepository('AppBundle:Performance')->findOneByslug($slug);
238
239 1
        if (!$performance) {
240 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
241
        }
242
243 1
        $performance->setLocale($paramFetcher->get('locale'));
244 1
        $em->refresh($performance);
245
246 1
        if ($performance->getTranslations()) {
247 1
            $performance->unsetTranslations();
248
        }
249
250 1
        $roles = $performance->getRoles();
251 1
        $rolesTrans = [];
252
253 1
        foreach ($roles as $role) {
254 1
            $role->setLocale($paramFetcher->get('locale'));
255 1
            $em->refresh($role);
256
257 1
            if ($role->getTranslations()) {
258 1
                $role->unsetTranslations();
259
            }
260
261 1
            $role->getEmployee()->setLocale($paramFetcher->get('locale'));
262 1
            $em->refresh($role->getEmployee());
263
264 1
            if ($role->getEmployee()->getTranslations()) {
265 1
                $role->getEmployee()->unsetTranslations();
266
            }
267
268 1
            $rolesTrans[] = $role;
269
        }
270 1
        $roles = $rolesTrans;
271
272 1
        return $roles;
273
    }
274
275
    /**
276
     * @ApiDoc(
277
     * resource=true,
278
     *  description="Returns Performance events by Performance {slug}",
279
     *  statusCodes={
280
     *      200="Returned when Performance by {slug} was found in database",
281
     *      404="Returned when Performance by {slug} was not found in database",
282
     *  },
283
     *  parameters={
284
     *      {"name"="slug", "dataType"="string", "required"=true, "description"="Performance unique name"}
285
     *  },
286
     *  output = "array<AppBundle\Entity\PerformanceEvent>",
287
     * deprecated = true
288
     * )
289
     *
290
     * @QueryParam(
291
     *     name="locale",
292
     *     requirements="^[a-zA-Z]+",
293
     *     default="uk",
294
     *     description="Selects language of data you want to receive"
295
     * )
296
     *
297
     * @RestView
298
     */
299 1
    public function getPerformanceeventsAction(ParamFetcher $paramFetcher, $slug)
300
    {
301 1
        $em = $this->getDoctrine()->getManager();
302
303 1
        $performance = $em->getRepository('AppBundle:Performance')->findOneByslug($slug);
304
305 1
        if (!$performance) {
306 1
            throw $this->createNotFoundException('Unable to find '.$slug.' entity');
307
        }
308
309 1
        $performance->setLocale($paramFetcher->get('locale'));
310 1
        $em->refresh($performance);
311
312 1
        if ($performance->getTranslations()) {
313 1
            $performance->unsetTranslations();
314
        }
315
316 1
        $performanceEvents = $performance->getPerformanceEvents();
317 1
        $performanceEventsTrans = [];
318
319 1
        foreach ($performanceEvents as $performanceEvent) {
320 1
            $performanceEvent->setLocale($paramFetcher->get('locale'));
321 1
            $em->refresh($performanceEvent);
322 1
            if ($performanceEvent->getTranslations()) {
323 1
                $performanceEvent->unsetTranslations();
324
            }
325 1
            $performanceEventsTrans[] = $performanceEvent;
326
        }
327 1
        $performanceEvents = $performanceEventsTrans;
328
329 1
        return $performanceEvents;
330
    }
331
}
332