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

CalendarController::userEventsAction()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 20
rs 9.2
c 2
b 1
f 0
cc 4
eloc 14
nc 6
nop 1
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("/")
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("")
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->addUser($user);
73
        $user->setEvent($event);
74
75
        $em->persist($user);
76
        $em->flush();
77
        $event = new DtoEvent($result);
78
79
        return $this->json(['event' => $event]);
80
    }
81
82
    /**
83
     * @param $id
84
     * @Route("/{id}")
85
     * @Method("GET")
86
     *
87
     * @return JsonResponse
88
     */
89
    public function singleEventAction($id)
90
    {
91
        /** @var Event $event */
92
        $event = $this->getDoctrine()->getRepository('AppBundle:Event')
93
            ->findByGoogleId($id);
94
        $user = $event->getUsers()->first();
0 ignored issues
show
Bug introduced by
The method first cannot be called on $event->getUsers() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
95
        if (!$user) {
96
            throw new JsonHttpException(404, 'User not found.');
97
        }
98
        $googleEvent = $this->get('app.google_calendar')
99
            ->getEventById($id);
100
        $event = new DtoEvent($googleEvent);
101
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
102
103
        return new JsonResponse(['user' => $user, 'event' => $event]);
104
    }
105
106
    /**
107
     * @Route("/user/{id}")
108
     * @Method("GET")
109
     *
110
     * @return JsonResponse
111
     */
112
    public function userEventsAction($id)
113
    {
114
        $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id);
115
        $events = $user->getEvents();
116
        $calendar = $this->get('app.google_calendar');
117
        $googleEvents = [];
118
        foreach ($events as $event) {
119
            $googleEvents[] = $calendar
120
                ->getEventById($event->getGoogleId());
121
        }
122
        $events = [];
123
        foreach ($googleEvents as $event) {
124
            if ($event) {
125
                $events[] = new DtoEvent($event);
126
            }
127
        }
128
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
129
130
        return new JsonResponse(['user' => $user, 'events' => $events]);
131
    }
132
133
    /**
134
     * @param $id
135
     * @Route("/{id}")
136
     * @Method("DELETE")
137
     *
138
     * @return JsonResponse
139
     */
140
    public function removeEventAction($id)
141
    {
142
        $this->get('app.google_calendar')
143
            ->deleteEvent($id);
144
        $em = $this->getDoctrine()->getManager();
145
        /** @var Event $event */
146
        $event = $em->getRepository('AppBundle:Event')->findByGoogleId($id);
147
        $em->remove($event);
148
        $em->flush();
149
150
        return $this->json(['success' => 'Event was removed']);
151
    }
152
153
    /**
154
     * @param Request $request
155
     * @Method("PATCH")
156
     * @Route("/{id}")
157
     *
158
     * @return JsonResponse
159
     */
160
    public function editEventAction(Request $request, $id)
161
    {
162
        $data = json_decode($request->getContent(), true);
163
164 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...
165
            throw new JsonHttpException(400, 'Bad request.');
166
        }
167
168
        $dtoEvent = new DtoEvent();
169
        $form = $this->createForm(EventType::class, $dtoEvent);
170
        $this->handleJsonForm($form, $request);
171
        $result = $this->get('app.google_calendar')
172
            ->editEvent($dtoEvent, $id, $request->query->all());
173
174
        $event = new DtoEvent($result);
175
        return $this->json(['event' => $event]);
176
    }
177
178
    /**
179
     * FOR DEV ONLY.
180
     *
181
     * @Method("PUT")
182
     * @Route("/clear")
183
     */
184
    public function clearAction()
185
    {
186
        $result = $this->get('app.google_calendar')->clear();
187
188
        return $this->json($result);
189
    }
190
}
191