Completed
Push — master ( 28be56...359087 )
by Serhii
13:20
created

getPriceCategoriesAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 0
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
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 Nelmio\ApiDocBundle\Annotation\ApiDoc;
15
use AppBundle\Model\PerformanceEventsResponse;
16
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
17
18
/**
19
 * @RouteResource("PerformanceEvent")
20
 */
21
class PerformanceEventsController extends Controller
22
{
23
    const MAX_DAYS_PER_GET = 367;
24
25
    /**
26
     * @ApiDoc(
27
     * resource=true,
28
     *  description="Returns a collection of theatre performanceEvents",
29
     *  statusCodes={
30
     *      200="Returned when all parameters were correct",
31
     *      400="Returned when date diff more than 1 year",
32
     * },
33
     *  output = "array<AppBundle\Model\PerformanceEventsResponse>"
34
     * )
35
     *
36
     * @QueryParam(
37
     *     name="fromDate",
38
     *     default="today",
39
     *     requirements="\d{2}-\d{2}-\d{4}|today" ,
40
     *     description="Find entries from this date, fromat=dd-mm-yyyy"
41
     * )
42
     * @QueryParam(
43
     *     name="toDate",
44
     *     default="+1 Year",
45
     *     requirements="\d{2}-\d{2}-\d{4}|\+1 Year",
46
     *     description="Find entries to this date, fromat=dd-mm-yyyy"
47
     * )
48
     * @QueryParam(name="limit", default="all", requirements="\d+|all" , description="Count of entities in collection")
49
     * @QueryParam(name="performance", description="Performance slug")
50
     * @QueryParam(
51
     *     name="locale",
52
     *     requirements="^[a-zA-Z]+",
53
     *     default="uk",
54
     *     description="Selects language of data you want to receive"
55
     * )
56
     *
57
     * @RestView
58
     */
59 2
    public function cgetAction(ParamFetcher $paramFetcher)
60
    {
61 2
        $em = $this->getDoctrine()->getManager();
62
63 2
        $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate'));
64
65 2
        if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) {
66
            throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET));
67
        }
68
69 2
        $performanceEvents = $em->getRepository('AppBundle:PerformanceEvent')
70 2
            ->findByDateRangeAndSlug(
71 2
                new \DateTime($paramFetcher->get('fromDate')),
72 2
                new \DateTime($paramFetcher->get('toDate')),
73 2
                $paramFetcher->get('performance')
74
            )
75
        ;
76
77 2
        if ('all' != $paramFetcher->get('limit')) {
78
            $performanceEvents = array_slice($performanceEvents, 0, $paramFetcher->get('limit'));
79
        }
80
81 2
        $performanceEventsTranslated = [];
82
83 2
        foreach ($performanceEvents as $performanceEvent) {
84
85
            /** @var PerformanceEvent $performanceEvent */
86 2
            $performanceEvent->setLocale($paramFetcher->get('locale'));
87 2
            $em->refresh($performanceEvent);
88
89 2
            $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
90 2
            $em->refresh($performanceEvent->getPerformance());
91
92 2
            $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
93 2
            $em->refresh($performanceEvent->getVenue());
94
95 2
            if ($performanceEvent->getTranslations()) {
96 2
                $performanceEvent->unsetTranslations();
97
            }
98
99 2
            if ($performanceEvent->getPerformance()->getTranslations()) {
100 2
                $performanceEvent->getPerformance()->unsetTranslations();
101
            }
102
103 2
            if ($performanceEvent->getVenue()->getTranslations()) {
104 2
                $performanceEvent->getVenue()->unsetTranslations();
105
            }
106
107 2
            $performanceEventsTranslated[] = $performanceEvent;
108
        }
109
110 2
        $performanceEvents = $performanceEventsTranslated;
111
112 2
        $performanceEventsResponse = new PerformanceEventsResponse();
113 2
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
114
115 2
        return $performanceEventsResponse;
116
    }
117
118
    /**
119
     * @ApiDoc(
120
     *  resource=true,
121
     *  description="Returns one PerformanceEvent by Id",
122
     *  statusCodes={
123
     *      200="Returned when PerformanceEvent by id was found in database",
124
     *      404="Returned when PerformanceEvent by id was not found id database",
125
     *  },
126
     *  parameters={
127
     *      {"name"="id", "dataType"="string", "required"=true, "description"="PerformanceEvent id"}
128
     *  },
129
     *  output = "AppBundle\Entity\PerformanceEvent",
130
     * deprecated = true
131
     * )
132
     *
133
     * @QueryParam(
134
     *     name="locale",
135
     *     requirements="^[a-zA-Z]+",
136
     *     default="uk",
137
     *     description="Selects language of data you want to receive"
138
     * )
139
     *
140
     * @RestView
141
     */
142 1
    public function getAction(ParamFetcher $paramFetcher, $id)
143
    {
144 1
        $em = $this->getDoctrine()->getManager();
145
146 1
        $performanceEvent = $em->getRepository('AppBundle:PerformanceEvent')->find($id);
147
148 1
        if (!$performanceEvent) {
149 1
            throw $this->createNotFoundException('Unable to find '.$id.' entity');
150
        }
151
152 1
        $performanceEvent->setLocale($paramFetcher->get('locale'));
153 1
        $em->refresh($performanceEvent);
154
155 1
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
156 1
        $em->refresh($performanceEvent->getPerformance());
157
158 1
        $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
159 1
        $em->refresh($performanceEvent->getVenue());
160
161 1
        if ($performanceEvent->getTranslations()) {
162 1
            $performanceEvent->unsetTranslations();
163
        }
164
165 1
        if ($performanceEvent->getPerformance()->getTranslations()) {
166 1
            $performanceEvent->getPerformance()->unsetTranslations();
167
        }
168
169 1
        if ($performanceEvent->getVenue()->getTranslations()) {
170 1
            $performanceEvent->getVenue()->unsetTranslations();
171
        }
172
173 1
        return $performanceEvent;
174
    }
175
176
    /**
177
     * @Get(requirements={"id" = "\d+"})
178
     * @RestView(serializerGroups={"get_ticket"})
179
     * @ParamConverter("id", class="AppBundle:PerformanceEvent")
180
     */
181
    public function cgetTicketsAction(PerformanceEvent $id)
182
    {
183
        //This done not in right way (PerformanceEvent $performanceEvent)
184
        // to have RESTfully looking route: /performanceevents/{id}/tickets
185
        $performanceEvent = $id;
186
187
        $em = $this->getDoctrine()->getManager();
188
        $tickets = $em
189
            ->getRepository(Ticket::class)
190
            ->findBy(['performanceEvent' => $performanceEvent]);
191
192
        return $tickets;
193
    }
194
195
196
    /**
197
     * @Get("/performanceevents/{id}/pricecategories", requirements={"id" = "\d+"})
198
     * @RestView
199
     * @ParamConverter("performanceEvent", class="AppBundle:PerformanceEvent")
200
     * @QueryParam(
201
     *     name="locale",
202
     *     requirements="^[a-zA-Z]+",
203
     *     default="uk",
204
     *     description="Selects language of data you want to receive"
205
     * )
206
     * @QueryParam(
207
     *     name="id",
208
     *     requirements="\d+",
209
     *     description="PerformanceEvent ID"
210
     * )
211
     */
212
    public function getPriceCategoriesAction(ParamFetcher $paramFetcher, PerformanceEvent $performanceEvent)
213
    {
214
        $priceCategory = $performanceEvent->getPriceCategories();
215
216
        $em = $this->getDoctrine()->getManager();
217
218
        foreach ($priceCategory as $category) {
219
            $category->getVenueSector()->setLocale($paramFetcher->get('locale'));
220
            $em->refresh($category->getVenueSector());
221
222
            if ($category->getVenueSector()->getTranslations()) {
223
                $category->getVenueSector()->unsetTranslations();
224
            }
225
        }
226
        return $priceCategory;
227
    }
228
}
229