Completed
Pull Request — master (#139)
by
unknown
16:38 queued 06:45
created

PerformanceEventsController::cgetTicketsAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\PerformanceEvent;
6
use AppBundle\Entity\Ticket;
7
use FOS\RestBundle\Request\ParamFetcher;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use FOS\RestBundle\Controller\Annotations\View as RestView;
11
use FOS\RestBundle\Controller\Annotations\RouteResource;
12
use FOS\RestBundle\Controller\Annotations\QueryParam;
13
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
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
     * @ApiDoc(
26
     * resource=true,
27
     *  description="Returns a collection of theatre performanceEvents",
28
     *  statusCodes={
29
     *      200="Returned when all parameters were correct",
30
     *      400="Returned when date diff more than 1 year",
31
     * },
32
     *  output = "array<AppBundle\Model\PerformanceEventsResponse>"
33
     * )
34
     *
35
     * @QueryParam(
36
     *     name="fromDate",
37
     *     default="today",
38
     *     requirements="\d{2}-\d{2}-\d{4}|today" ,
39
     *     description="Find entries from this date, fromat=dd-mm-yyyy"
40
     * )
41
     * @QueryParam(
42
     *     name="toDate",
43
     *     default="+1 Year",
44
     *     requirements="\d{2}-\d{2}-\d{4}|\+1 Year",
45
     *     description="Find entries to this date, fromat=dd-mm-yyyy"
46
     * )
47
     * @QueryParam(name="limit", default="all", requirements="\d+|all" , description="Count of entities in collection")
48
     * @QueryParam(name="performance", description="Performance slug")
49
     * @QueryParam(
50
     *     name="locale",
51
     *     requirements="^[a-zA-Z]+",
52
     *     default="uk",
53
     *     description="Selects language of data you want to receive"
54
     * )
55
     *
56
     * @RestView
57
     */
58 2
    public function cgetAction(ParamFetcher $paramFetcher)
59
    {
60 2
        $em = $this->getDoctrine()->getManager();
61
62 2
        $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate'));
63
64 2
        if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) {
65
            throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET));
66
        }
67
68 2
        $performanceEvents = $em->getRepository('AppBundle:PerformanceEvent')
69 2
            ->findByDateRangeAndSlug(
70 2
                new \DateTime($paramFetcher->get('fromDate')),
71 2
                new \DateTime($paramFetcher->get('toDate')),
72 2
                $paramFetcher->get('performance')
73
            )
74
        ;
75
76 2
        if ('all' != $paramFetcher->get('limit')) {
77
            $performanceEvents = array_slice($performanceEvents, 0, $paramFetcher->get('limit'));
78
        }
79
80 2
        $performanceEventsTranslated = [];
81
82 2
        foreach ($performanceEvents as $performanceEvent) {
83
84
            /** @var PerformanceEvent $performanceEvent */
85 2
            $performanceEvent->setLocale($paramFetcher->get('locale'));
86 2
            $em->refresh($performanceEvent);
87
88 2
            $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
89 2
            $em->refresh($performanceEvent->getPerformance());
90
91 2
            $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
92 2
            $em->refresh($performanceEvent->getVenue());
93
94 2
            if ($performanceEvent->getTranslations()) {
95 2
                $performanceEvent->unsetTranslations();
96
            }
97
98 2
            if ($performanceEvent->getPerformance()->getTranslations()) {
99 2
                $performanceEvent->getPerformance()->unsetTranslations();
100
            }
101
102 2
            if ($performanceEvent->getVenue()->getTranslations()) {
103 2
                $performanceEvent->getVenue()->unsetTranslations();
104
            }
105
106 2
            $performanceEventsTranslated[] = $performanceEvent;
107
        }
108
109 2
        $performanceEvents = $performanceEventsTranslated;
110
111 2
        $performanceEventsResponse = new PerformanceEventsResponse();
112 2
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
113
114 2
        return $performanceEventsResponse;
115
    }
116
117
    /**
118
     * @ApiDoc(
119
     *  resource=true,
120
     *  description="Returns one PerformanceEvent by Id",
121
     *  statusCodes={
122
     *      200="Returned when PerformanceEvent by id was found in database",
123
     *      404="Returned when PerformanceEvent by id was not found id database",
124
     *  },
125
     *  parameters={
126
     *      {"name"="id", "dataType"="string", "required"=true, "description"="PerformanceEvent id"}
127
     *  },
128
     *  output = "AppBundle\Entity\PerformanceEvent",
129
     * deprecated = true
130
     * )
131
     *
132
     * @QueryParam(
133
     *     name="locale",
134
     *     requirements="^[a-zA-Z]+",
135
     *     default="uk",
136
     *     description="Selects language of data you want to receive"
137
     * )
138
     *
139
     * @RestView
140
     */
141 1
    public function getAction(ParamFetcher $paramFetcher, $id)
142
    {
143 1
        $em = $this->getDoctrine()->getManager();
144
145 1
        $performanceEvent = $em->getRepository('AppBundle:PerformanceEvent')->findOneById($id);
0 ignored issues
show
Bug introduced by
The method findOneById() does not exist on Doctrine\Common\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
146
147 1
        if (!$performanceEvent) {
148 1
            throw $this->createNotFoundException('Unable to find '.$id.' entity');
149
        }
150
151 1
        $performanceEvent->setLocale($paramFetcher->get('locale'));
152 1
        $em->refresh($performanceEvent);
153
154 1
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
155 1
        $em->refresh($performanceEvent->getPerformance());
156
157 1
        $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
158 1
        $em->refresh($performanceEvent->getVenue());
159
160 1
        if ($performanceEvent->getTranslations()) {
161 1
            $performanceEvent->unsetTranslations();
162
        }
163
164 1
        if ($performanceEvent->getPerformance()->getTranslations()) {
165 1
            $performanceEvent->getPerformance()->unsetTranslations();
166
        }
167
168 1
        if ($performanceEvent->getVenue()->getTranslations()) {
169 1
            $performanceEvent->getVenue()->unsetTranslations();
170
        }
171
172 1
        return $performanceEvent;
173
    }
174
175
    /**
176
     * @RestView(serializerGroups={"cget_ticket"})
177
     * @ParamConverter("performanceEvent", class="AppBundle:PerformanceEvent")
178
     */
179
    public function cgetTicketsAction(PerformanceEvent $performanceEvent)
180
    {
181
        $em = $this->getDoctrine()->getManager();
182
        $tickets = $em
183
            ->getRepository(Ticket::class)
184
            ->findBy(['performanceEvent' => $performanceEvent]);
185
186
        return $tickets;
187
    }
188
}
189