Completed
Pull Request — master (#30)
by
unknown
05:17
created

CalendarController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 168
Duplicated Lines 3.57 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 7
Bugs 1 Features 2
Metric Value
wmc 19
c 7
b 1
f 2
lcom 1
cbo 8
dl 6
loc 168
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A userEventsAction() 0 21 4
A removeEventAction() 0 12 1
A singleEventAction() 0 16 2
B editEventAction() 3 24 4
A eventsListAction() 0 11 2
B newEventAction() 3 36 6

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 AppBundle\Controller;
4
5
use AppBundle\Entity\DTO\DtoEvent;
6
use AppBundle\Entity\Event;
7
use AppBundle\Entity\User;
8
use AppBundle\Exception\JsonHttpException;
9
use AppBundle\Form\EventType;
10
use Mcfedr\JsonFormBundle\Controller\JsonController;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
/**
17
 * @Route("/schedule/events")
18
 */
19
class CalendarController extends JsonController
20
{
21
    /**
22
     * @Route("/", name="events-list", options={"expose"=true})
23
     * @Method("GET")
24
     *
25
     * @return JsonResponse
26
     */
27
    public function eventsListAction(Request $request)
28
    {
29
        $googleEvents = $this->get('app.google_calendar')
30
            ->getEventList($request->query->all());
31
        $events = [];
32
        foreach ($googleEvents['events'] as $event) {
33
            $events[] = new DtoEvent($event);
34
        }
35
36
        return $this->json(['pageToken' => $googleEvents['pageToken'], 'events' => $events]);
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @Route("", name="new-event", options={"expose"=true})
42
     * @Method("POST")
43
     *
44
     * @return JsonResponse
45
     */
46
    public function newEventAction(Request $request)
47
    {
48
        $data = json_decode($request->getContent(), true);
49
50 View Code Duplication
        if (!$data['event']['start'] || !$data['event']['end'] || !$data['event']['user']) {
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...
51
            throw new JsonHttpException(400, 'Bad request.');
52
        }
53
54
        $dtoEvent = new DtoEvent();
55
        $form = $this->createForm(EventType::class, $dtoEvent);
56
        $this->handleJsonForm($form, $request);
57
        $result = $this->get('app.google_calendar')
58
            ->createEvent($dtoEvent, $request->query->all());
59
        if (!$result) {
60
            throw new JsonHttpException(412, 'Event has not been created');
61
        }
62
        $em = $this->getDoctrine()->getManager();
63
        /** @var User $user */
64
        $user = $em->getRepository('AppBundle:User')
65
            ->find($dtoEvent->getUser());
66
67
        if (!$user) {
68
            throw new JsonHttpException(404, 'User not found.');
69
        }
70
        $event = new Event();
71
        $event->setGoogleId($result->id);
72
        $event->setUser($user);
73
        $event->setExpiredAt(new \DateTime($result->getEnd()->dateTime));
74
        $user->setEvent($event);
75
76
        $em->persist($user);
77
        $em->flush();
78
        $event = new DtoEvent($result);
79
80
        return $this->json(['event' => $event]);
81
    }
82
83
    /**
84
     * @param $id
85
     * @Route("/{id}", name="single-event", options={"expose"=true})
86
     * @Method("GET")
87
     *
88
     * @return JsonResponse
89
     */
90
    public function singleEventAction($id)
91
    {
92
        /** @var Event $event */
93
        $event = $this->getDoctrine()->getRepository('AppBundle:Event')
94
            ->findByGoogleId($id);
95
        $user = $event->getUser();
96
        if (!$user) {
97
            throw new JsonHttpException(404, 'User not found.');
98
        }
99
        $googleEvent = $this->get('app.google_calendar')
100
            ->getEventById($id);
101
        $event = new DtoEvent($googleEvent);
102
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
103
104
        return new JsonResponse(['user' => $user, 'event' => $event]);
105
    }
106
107
    /**
108
     * @Route("/user/{id}", name="user-events", options={"expose"=true})
109
     * @Method("GET")
110
     *
111
     * @return JsonResponse
112
     */
113
    public function userEventsAction($id)
114
    {
115
        $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id);
116
        $events = $this->getDoctrine()->getRepository(Event::class)
117
        ->selectNotExpiredByUser($user);
118
        $calendar = $this->get('app.google_calendar');
119
        $googleEvents = [];
120
        foreach ($events as $event) {
121
            $googleEvents[] = $calendar
122
                ->getEventById($event->getGoogleId());
123
        }
124
        $events = [];
125
        foreach ($googleEvents as $event) {
126
            if ($event) {
127
                $events[] = new DtoEvent($event);
128
            }
129
        }
130
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
131
132
        return new JsonResponse(['user' => $user, 'events' => $events]);
133
    }
134
135
    /**
136
     * @param $id
137
     * @Route("/{id}", name="remove-event", options={"expose"=true})
138
     * @Method("DELETE")
139
     *
140
     * @return JsonResponse
141
     */
142
    public function removeEventAction($id)
143
    {
144
        $this->get('app.google_calendar')
145
            ->deleteEvent($id);
146
        $em = $this->getDoctrine()->getManager();
147
        /** @var Event $event */
148
        $event = $em->getRepository('AppBundle:Event')->findByGoogleId($id);
149
        $em->remove($event);
150
        $em->flush();
151
152
        return $this->json(['success' => 'Event was removed']);
153
    }
154
155
    /**
156
     * @param Request $request
157
     * @Method("PATCH")
158
     * @Route("/{id}", name="edit-event", options={"expose"=true})
159
     *
160
     * @return JsonResponse
161
     */
162
    public function editEventAction(Request $request, $id)
163
    {
164
        $data = json_decode($request->getContent(), true);
165
166 View Code Duplication
        if (!$data['event']['start'] || !$data['event']['end'] || !$data['event']['user']) {
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...
167
            throw new JsonHttpException(400, 'Bad request.');
168
        }
169
170
        $dtoEvent = new DtoEvent();
171
        $form = $this->createForm(EventType::class, $dtoEvent);
172
        $this->handleJsonForm($form, $request);
173
        $result = $this->get('app.google_calendar')
174
            ->editEvent($dtoEvent, $id, $request->query->all());
175
        $event = new DtoEvent($result);
176
177
        $em = $this->getDoctrine()->getManager();
178
        $user = $em->getRepository(User::class)->find($dtoEvent->getUser());
179
        /** @var Event $ev */
180
        $ev = $this->getDoctrine()->getRepository(Event::class)->findByGoogleId($id);
181
        $ev->setUser($user);
182
        $em->flush();
183
184
        return $this->json(['event' => $event]);
185
    }
186
}
187