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

PerformanceEventsController::cgetAction()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 54
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6.0012

Importance

Changes 0
Metric Value
cc 6
eloc 30
nc 10
nop 1
dl 0
loc 54
ccs 29
cts 30
cp 0.9667
crap 6.0012
rs 8.7449
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\PerformanceEvent;
6
use AppBundle\Entity\Ticket;
7
use FOS\RestBundle\Controller\Annotations\Get;
8
use FOS\RestBundle\Request\ParamFetcher;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use FOS\RestBundle\Controller\Annotations\View as RestView;
12
use FOS\RestBundle\Controller\Annotations\RouteResource;
13
use FOS\RestBundle\Controller\Annotations\QueryParam;
14
use AppBundle\Model\PerformanceEventsResponse;
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
17
/**
18
 * @RouteResource("PerformanceEvent")
19
 */
20
class PerformanceEventsController extends Controller
21
{
22
    const MAX_DAYS_PER_GET = 367;
23
24
    /**
25
     * @Get("/performanceevents")
26
     * @QueryParam(
27
     *     name="fromDate",
28
     *     default="today",
29
     *     requirements="\d{2}-\d{2}-\d{4}|today" ,
30
     *     description="Find entries from this date, fromat=dd-mm-yyyy"
31
     * )
32
     * @QueryParam(
33
     *     name="toDate",
34
     *     default="+1 Year",
35
     *     requirements="\d{2}-\d{2}-\d{4}|\+1 Year",
36
     *     description="Find entries to this date, fromat=dd-mm-yyyy"
37
     * )
38
     * @QueryParam(name="limit", description="Count of entities in collection")
39
     * @QueryParam(name="performance", description="Performance slug")
40
     * @QueryParam(
41
     *     name="locale",
42
     *     requirements="uk|en",
43
     *     default="uk",
44
     *     description="Selects language of data you want to receive"
45
     * )
46
     *
47
     * @RestView
48
     */
49 5
    public function cgetAction(ParamFetcher $paramFetcher)
50
    {
51 5
        $em = $this->getDoctrine()->getManager();
52
53 5
        $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate'));
54
55 5
        if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) {
56
            throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET));
57
        }
58
59 5
        $performanceEvents = $em->getRepository('AppBundle:PerformanceEvent')
60 5
            ->findByDateRangeAndSlug(
61 5
                new \DateTime($paramFetcher->get('fromDate')),
62 5
                new \DateTime($paramFetcher->get('toDate')),
63 5
                $paramFetcher->get('limit'),
64 5
                $paramFetcher->get('performance')
65
            )
66
        ;
67
68 5
        $performanceEventsTranslated = [];
69
70 5
        foreach ($performanceEvents as $performanceEvent) {
71
72
            /** @var PerformanceEvent $performanceEvent */
73 5
            $performanceEvent->setLocale($paramFetcher->get('locale'));
74 5
            $em->refresh($performanceEvent);
75
76 5
            $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
77 5
            $em->refresh($performanceEvent->getPerformance());
78
79 5
            $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
80 5
            $em->refresh($performanceEvent->getVenue());
81
82 5
            if ($performanceEvent->getTranslations()) {
83 5
                $performanceEvent->unsetTranslations();
84
            }
85
86 5
            if ($performanceEvent->getPerformance()->getTranslations()) {
87 5
                $performanceEvent->getPerformance()->unsetTranslations();
88
            }
89
90 5
            if ($performanceEvent->getVenue()->getTranslations()) {
91 5
                $performanceEvent->getVenue()->unsetTranslations();
92
            }
93 5
            $performanceEventsTranslated[] = $performanceEvent;
94
        }
95
96 5
        $performanceEvents = $performanceEventsTranslated;
97
98 5
        $performanceEventsResponse = new PerformanceEventsResponse();
99 5
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
100
101 5
        return $performanceEventsResponse;
102
    }
103
104
    /**
105
     * @Get("/performanceevents/{id}", requirements={"id" = "\d+"})
106
     * @ParamConverter("performanceEvent", class="AppBundle:PerformanceEvent")
107
     * @QueryParam(
108
     *     name="locale",
109
     *     requirements="uk|en",
110
     *     default="uk",
111
     *     description="Selects language of data you want to receive"
112
     * )
113
     * @QueryParam(
114
     *     name="id",
115
     *     requirements="\d+",
116
     *     description="PerformanceEvent ID"
117
     * )
118
     * @RestView
119
     */
120 1
    public function getAction(ParamFetcher $paramFetcher, PerformanceEvent $performanceEvent)
121
    {
122 1
        $em = $this->getDoctrine()->getManager();
123
124 1
        $performanceEvent->setLocale($paramFetcher->get('locale'));
125 1
        $em->refresh($performanceEvent);
126
127 1
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
128 1
        $em->refresh($performanceEvent->getPerformance());
129
130 1
        $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
131 1
        $em->refresh($performanceEvent->getVenue());
132
133 1
        if ($performanceEvent->getTranslations()) {
134 1
            $performanceEvent->unsetTranslations();
135
        }
136
137 1
        if ($performanceEvent->getPerformance()->getTranslations()) {
138 1
            $performanceEvent->getPerformance()->unsetTranslations();
139
        }
140
141 1
        if ($performanceEvent->getVenue()->getTranslations()) {
142 1
            $performanceEvent->getVenue()->unsetTranslations();
143
        }
144
145 1
        return $performanceEvent;
146
    }
147
148
    /**
149
     * @Get("/performanceevents/{id}/tickets", requirements={"id" = "\d+"})
150
     * @RestView(serializerGroups={"get_ticket"})
151
     * @ParamConverter("performanceEvent", class="AppBundle:PerformanceEvent")
152
     */
153 1
    public function cgetTicketsAction(PerformanceEvent $performanceEvent)
154
    {
155 1
        $em = $this->getDoctrine()->getManager();
156
        $tickets = $em
157 1
            ->getRepository(Ticket::class)
158 1
            ->findBy(['performanceEvent' => $performanceEvent]);
159
160 1
        return $tickets;
161
    }
162
163
164
    /**
165
     * @Get("/performanceevents/{id}/pricecategories", requirements={"id" = "\d+"})
166
     * @RestView
167
     * @ParamConverter("performanceEvent", class="AppBundle:PerformanceEvent")
168
     * @QueryParam(
169
     *     name="locale",
170
     *     requirements="uk|en",
171
     *     default="uk",
172
     *     description="Selects language of data you want to receive"
173
     * )
174
     * @QueryParam(
175
     *     name="id",
176
     *     requirements="\d+",
177
     *     description="PerformanceEvent ID"
178
     * )
179
     */
180 1
    public function getPriceCategoriesAction(ParamFetcher $paramFetcher, PerformanceEvent $performanceEvent)
181
    {
182 1
        $priceCategory = $performanceEvent->getPriceCategories();
183
184 1
        $em = $this->getDoctrine()->getManager();
185
186 1
        foreach ($priceCategory as $category) {
187 1
            $category->getVenueSector()->setLocale($paramFetcher->get('locale'));
188 1
            $em->refresh($category->getVenueSector());
189
190 1
            if ($category->getVenueSector()->getTranslations()) {
191 1
                $category->getVenueSector()->unsetTranslations();
192
            }
193
        }
194 1
        return $priceCategory;
195
    }
196
}
197