Completed
Push — develop ( 4b596e...fe09a2 )
by Serhii
03:24 queued 01:28
created

PerformanceEventsController::cgetAction()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 49

Duplication

Lines 17
Ratio 34.69 %

Importance

Changes 0
Metric Value
cc 6
nc 11
nop 1
dl 17
loc 49
rs 8.4905
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Api;
4
5
use App\Model\PerformanceEventsResponse;
6
use FOS\RestBundle\Request\ParamFetcher;
7
use FOS\RestBundle\Controller\Annotations\QueryParam;
8
use JMS\Serializer\SerializationContext;
9
use JMS\Serializer\SerializerInterface;
10
use Nelmio\ApiDocBundle\Annotation\Model;
11
use Swagger\Annotations as SWG;
12
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
/**
19
 * @Route("/api/performanceevents")
20
 */
21
class PerformanceEventsController extends AbstractController
22
{
23
    const MAX_DAYS_PER_GET = 367;
24
25
    private SerializerInterface $serializer;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
26
27
    public function __construct(SerializerInterface $serializer)
28
    {
29
        $this->serializer = $serializer;
30
    }
31
32
    /**
33
     * @Route("", name="get_performanceevents", methods={"GET"})
34
     * @SWG\Response(
35
     *     response=200,
36
     *     description="Returns a collection of theatre performanceEvents",
37
     *     @SWG\Schema(
38
     *         type="array",
39
     *         @SWG\Items(ref=@Model(type=PerformanceEventsResponse::class))
40
     *     )
41
     * )
42
     * @SWG\Response(
43
     *     response=400,
44
     *     description="Returns when date diff more than 1 year",
45
     * )
46
     *
47
     * @QueryParam(name="fromDate", default="today", requirements="\d{2}-\d{2}-\d{4}|today" , description="Find entries from this date, fromat=dd-mm-yyyy")
48
     * @QueryParam(name="toDate", default="+1 Year", requirements="\d{2}-\d{2}-\d{4}|\+1 Year" , description="Find entries to this date, fromat=dd-mm-yyyy")
49
     * @QueryParam(name="limit", default="all", requirements="\d+|all" , description="Count of entities in collection")
50
     * @QueryParam(name="performance", description="Performance slug")
51
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
52
     */
53
    public function cgetAction(ParamFetcher $paramFetcher)
54
    {
55
        $em = $this->getDoctrine()->getManager();
56
57
        $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate'));
58
59
        if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) {
60
            throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET));
61
        }
62
63
        $limit = 'all' == $paramFetcher->get('limit')
64
            ? null
65
            : (int) $paramFetcher->get('limit');
66
67
        $performanceEvents = $em->getRepository('App:PerformanceEvent')
68
            ->findByDateRangeAndSlug(
69
                new \DateTime($paramFetcher->get('fromDate')),
70
                new \DateTime($paramFetcher->get('toDate')),
71
                $paramFetcher->get('performance'),
72
                $limit
73
            )
74
        ;
75
76
        $performanceEventsResponse = new PerformanceEventsResponse();
77
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
78
79
        return new Response(
80
            $this->serializer->serialize(
81
                $performanceEventsResponse,
82
                'json',
83
                SerializationContext::create()->setGroups(array('poster')))
84
        );
85
    }
86
87
    /**
88
     * @Route("/{id}", name="get_performance_event", methods={"GET"})
89
     * @SWG\Response(
90
     *     response=200,
91
     *     description="Returns one PerformanceEvent by Id",
92
     *     @Model(type=PerformanceEvent::class)
93
     * )
94
     * @SWG\Response(
95
     *     response=404,
96
     *     description="Returns when PerformanceEvent by id was not found id database",
97
     * )
98
     * @SWG\Get(deprecated=true)
99
     *
100
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
101
     */
102
    public function getAction(ParamFetcher $paramFetcher, $id)
103
    {
104
        $em = $this->getDoctrine()->getManager();
105
106
        $performanceEvent = $em->getRepository('App:PerformanceEvent')->findOneById($id);
107
108
        if (!$performanceEvent) {
109
            throw $this->createNotFoundException('Unable to find '.$id.' entity');
110
        }
111
112
        $performanceEvent->setLocale($paramFetcher->get('locale'));
113
        $em->refresh($performanceEvent);
114
115
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
116
        $em->refresh($performanceEvent->getPerformance());
117
118
        if ($performanceEvent->getTranslations()) {
119
            $performanceEvent->unsetTranslations();
120
        }
121
122
        if ($performanceEvent->getPerformance()->getTranslations()) {
123
            $performanceEvent->getPerformance()->unsetTranslations();
124
        }
125
126
        return $performanceEvent;
127
    }
128
}
129