PerformanceEventsController   A
last analyzed

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B cgetAction() 17 49 6
A getAction() 0 26 4

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 Nelmio\ApiDocBundle\Annotation\Model;
9
use Swagger\Annotations as SWG;
10
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
/**
15
 * @Route("/api/performanceevents")
16
 */
17
class PerformanceEventsController extends AbstractController
18
{
19
    const MAX_DAYS_PER_GET = 367;
20
21
    /**
22
     * @Route("", name="get_performanceevents", methods={"GET"})
23
     * @SWG\Response(
24
     *     response=200,
25
     *     description="Returns a collection of theatre performanceEvents",
26
     *     @SWG\Schema(
27
     *         type="array",
28
     *         @SWG\Items(ref=@Model(type=PerformanceEventsResponse::class))
29
     *     )
30
     * )
31
     * @SWG\Response(
32
     *     response=400,
33
     *     description="Returns when date diff more than 1 year",
34
     * )
35
     *
36
     * @QueryParam(name="fromDate", default="today", requirements="\d{2}-\d{2}-\d{4}|today" , description="Find entries from this date, fromat=dd-mm-yyyy")
37
     * @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")
38
     * @QueryParam(name="limit", default="all", requirements="\d+|all" , description="Count of entities in collection")
39
     * @QueryParam(name="performance", description="Performance slug")
40
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
41
     */
42 2
    public function cgetAction(ParamFetcher $paramFetcher)
43
    {
44 2
        $em = $this->getDoctrine()->getManager();
45
46 2
        $dateDiff = strtotime($paramFetcher->get('toDate')) - strtotime($paramFetcher->get('fromDate'));
47
48 2
        if (self::MAX_DAYS_PER_GET < abs(floor($dateDiff/(60*60*24)))) {
49
            throw new BadRequestHttpException(sprintf('You can\'t get more than "%s" days', self::MAX_DAYS_PER_GET));
50
        }
51
52 2
        $performanceEvents = $em->getRepository('App:PerformanceEvent')
53 2
            ->findByDateRangeAndSlug(
54 2
                new \DateTime($paramFetcher->get('fromDate')),
55 2
                new \DateTime($paramFetcher->get('toDate')),
56 2
                $paramFetcher->get('performance')
57
            )
58
        ;
59
60 2
        if ('all' != $paramFetcher->get('limit')) {
61
            $performanceEvents = array_slice($performanceEvents, 0, $paramFetcher->get('limit'));
62
        }
63
64 2
        $performanceEventsTranslated = [];
65
66 2 View Code Duplication
        foreach ($performanceEvents as $performanceEvent) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67 1
            $performanceEvent->setLocale($paramFetcher->get('locale'));
68 1
            $em->refresh($performanceEvent);
69
70 1
            $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
71 1
            $em->refresh($performanceEvent->getPerformance());
72
73 1
            if ($performanceEvent->getTranslations()) {
74 1
                $performanceEvent->unsetTranslations();
75
            }
76
77 1
            if ($performanceEvent->getPerformance()->getTranslations()) {
78 1
                $performanceEvent->getPerformance()->unsetTranslations();
79
            }
80
81 1
            $performanceEventsTranslated[] = $performanceEvent;
82
        }
83
84 2
        $performanceEvents = $performanceEventsTranslated;
85
86 2
        $performanceEventsResponse = new PerformanceEventsResponse();
87 2
        $performanceEventsResponse->setPerformanceEvents($performanceEvents);
88
89 2
        return $performanceEventsResponse;
90
    }
91
92
    /**
93
     * @Route("/{id}", name="get_performance_event", methods={"GET"})
94
     * @SWG\Response(
95
     *     response=200,
96
     *     description="Returns one PerformanceEvent by Id",
97
     *     @Model(type=PerformanceEvent::class)
98
     * )
99
     * @SWG\Response(
100
     *     response=404,
101
     *     description="Returns when PerformanceEvent by id was not found id database",
102
     * )
103
     * @SWG\Get(deprecated=true)
104
     *
105
     * @QueryParam(name="locale", requirements="^[a-zA-Z]+", default="uk", description="Selects language of data you want to receive")
106
     */
107 1
    public function getAction(ParamFetcher $paramFetcher, $id)
108
    {
109 1
        $em = $this->getDoctrine()->getManager();
110
111 1
        $performanceEvent = $em->getRepository('App:PerformanceEvent')->findOneById($id);
112
113 1
        if (!$performanceEvent) {
114 1
            throw $this->createNotFoundException('Unable to find '.$id.' entity');
115
        }
116
117 1
        $performanceEvent->setLocale($paramFetcher->get('locale'));
118 1
        $em->refresh($performanceEvent);
119
120 1
        $performanceEvent->getPerformance()->setLocale($paramFetcher->get('locale'));
121 1
        $em->refresh($performanceEvent->getPerformance());
122
123 1
        if ($performanceEvent->getTranslations()) {
124 1
            $performanceEvent->unsetTranslations();
125
        }
126
127 1
        if ($performanceEvent->getPerformance()->getTranslations()) {
128 1
            $performanceEvent->getPerformance()->unsetTranslations();
129
        }
130
131 1
        return $performanceEvent;
132
    }
133
}
134