Completed
Pull Request — master (#191)
by Serhii
02:32
created

PerformanceEventsController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 14.53 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.12%

Importance

Changes 0
Metric Value
dl 17
loc 117
ccs 39
cts 41
cp 0.9512
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 2
     * @SWG\Response(
43
     *     response=400,
44 2
     *     description="Returns when date diff more than 1 year",
45
     * )
46 2
     *
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 2
     * @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 2
     */
53 2
    public function cgetAction(ParamFetcher $paramFetcher)
54 2
    {
55 2
        $em = $this->getDoctrine()->getManager();
56 2
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 2
            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 2
            ? null
65
            : (int) $paramFetcher->get('limit');
66 2
67 1
        $performanceEvents = $em->getRepository('App:PerformanceEvent')
68 1
            ->findByDateRangeAndSlug(
69
                new \DateTime($paramFetcher->get('fromDate')),
70 1
                new \DateTime($paramFetcher->get('toDate')),
71 1
                $paramFetcher->get('performance'),
72
                $limit
73 1
            )
74 1
        ;
75
76
        $performanceEventsResponse = new PerformanceEventsResponse();
77 1
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
78 1
79
        return new Response(
80
            $this->serializer->serialize(
81 1
                $performanceEventsResponse,
82
                'json',
83
                SerializationContext::create()->setGroups(array('poster')))
84 2
        );
85
    }
86 2
87 2
    /**
88
     * @Route("/{id}", name="get_performance_event", methods={"GET"})
89 2
     * @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 1
108
        if (!$performanceEvent) {
109 1
            throw $this->createNotFoundException('Unable to find '.$id.' entity');
110
        }
111 1
112
        $performanceEvent->setLocale($paramFetcher->get('locale'));
113 1
        $em->refresh($performanceEvent);
114 1
115
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
116
        $em->refresh($performanceEvent->getPerformance());
117 1
118 1
        if ($performanceEvent->getTranslations()) {
119
            $performanceEvent->unsetTranslations();
120 1
        }
121 1
122
        if ($performanceEvent->getPerformance()->getTranslations()) {
123 1
            $performanceEvent->getPerformance()->unsetTranslations();
124 1
        }
125
126
        return $performanceEvent;
127 1
    }
128
}
129