Completed
Pull Request — master (#162)
by
unknown
03:18
created

PerformancesController::cgetAction()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 126
Code Lines 78

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 72
CRAP Score 6.0163

Importance

Changes 0
Metric Value
cc 6
eloc 78
nc 24
nop 1
dl 0
loc 126
ccs 72
cts 78
cp 0.9231
crap 6.0163
rs 8.1463
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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