Completed
Pull Request — master (#139)
by Serhii
14:07
created

PerformanceEventsController::cgetAction()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 58
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 31
nc 19
nop 1
dl 0
loc 58
rs 7.6045
c 0
b 0
f 0
ccs 23
cts 23
cp 1
crap 7

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\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 2
     *
56
     * @RestView
57 2
     */
58
    public function cgetAction(ParamFetcher $paramFetcher)
59 2
    {
60
        $em = $this->getDoctrine()->getManager();
61 2
62
        $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate'));
63
64
        if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) {
65 2
            throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET));
66 2
        }
67 2
68 2
        $performanceEvents = $em->getRepository('AppBundle:PerformanceEvent')
69 2
            ->findByDateRangeAndSlug(
70
                new \DateTime($paramFetcher->get('fromDate')),
71
                new \DateTime($paramFetcher->get('toDate')),
72
                $paramFetcher->get('performance')
73 2
            )
74
        ;
75
76
        if ('all' != $paramFetcher->get('limit')) {
77 2
            $performanceEvents = array_slice($performanceEvents, 0, $paramFetcher->get('limit'));
78
        }
79 2
80 2
        $performanceEventsTranslated = [];
81 2
82
        foreach ($performanceEvents as $performanceEvent) {
83 2
84 2
            /** @var PerformanceEvent $performanceEvent */
85
            $performanceEvent->setLocale($paramFetcher->get('locale'));
86 2
            $em->refresh($performanceEvent);
87 2
88
            $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
89
            $em->refresh($performanceEvent->getPerformance());
90 2
91 2
            $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
92
            $em->refresh($performanceEvent->getVenue());
93
94 2
            if ($performanceEvent->getTranslations()) {
95
                $performanceEvent->unsetTranslations();
96
            }
97 2
98
            if ($performanceEvent->getPerformance()->getTranslations()) {
99 2
                $performanceEvent->getPerformance()->unsetTranslations();
100 2
            }
101
102 2
            if ($performanceEvent->getVenue()->getTranslations()) {
103
                $performanceEvent->getVenue()->unsetTranslations();
104
            }
105
106
            $performanceEventsTranslated[] = $performanceEvent;
107
        }
108
109
        $performanceEvents = $performanceEventsTranslated;
110
111
        $performanceEventsResponse = new PerformanceEventsResponse();
112
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
113
114
        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 1
     * deprecated = true
130
     * )
131 1
     *
132
     * @QueryParam(
133 1
     *     name="locale",
134
     *     requirements="^[a-zA-Z]+",
135 1
     *     default="uk",
136 1
     *     description="Selects language of data you want to receive"
137
     * )
138
     *
139 1
     * @RestView
140 1
     */
141
    public function getAction(ParamFetcher $paramFetcher, $id)
142 1
    {
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 1
147
        if (!$performanceEvent) {
148
            throw $this->createNotFoundException('Unable to find '.$id.' entity');
149 1
        }
150 1
151
        $performanceEvent->setLocale($paramFetcher->get('locale'));
152
        $em->refresh($performanceEvent);
153 1
154
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
155
        $em->refresh($performanceEvent->getPerformance());
156
157
        $performanceEvent->getVenue()->setLocale($paramFetcher->get('locale'));
158
        $em->refresh($performanceEvent->getVenue());
159
160
        if ($performanceEvent->getTranslations()) {
161
            $performanceEvent->unsetTranslations();
162
        }
163
164
        if ($performanceEvent->getPerformance()->getTranslations()) {
165
            $performanceEvent->getPerformance()->unsetTranslations();
166
        }
167
168
        if ($performanceEvent->getVenue()->getTranslations()) {
169
            $performanceEvent->getVenue()->unsetTranslations();
170
        }
171
172
        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