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

CalendarController::newEventAction()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 34
Code Lines 23

Duplication

Lines 3
Ratio 8.82 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 3
loc 34
rs 8.439
cc 6
eloc 23
nc 4
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 Aws\S3\S3Client;
11
use Mcfedr\JsonFormBundle\Controller\JsonController;
12
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
/**
18
 * @Route("/schedule/events")
19
 */
20
class CalendarController extends JsonController
21
{
22
    /**
23
     * @Route("/")
24
     * @Method("GET")
25
     *
26
     * @return JsonResponse
27
     */
28
    public function eventsListAction(Request $request)
29
    {
30
        $googleEvents = $this->get('app.google_calendar')
31
            ->getEventList($request->query->all());
32
        $events = [];
33
        foreach ($googleEvents['events'] as $event) {
34
            $events[] = new DtoEvent($event);
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
78
        return $this->json(['success' => 'Event Created']);
79
    }
80
81
    /**
82
     * @param $id
83
     * @Route("/{id}")
84
     * @Method("GET")
85
     *
86
     * @return JsonResponse
87
     */
88
    public function singleEventAction($id)
89
    {
90
        /** @var Event $event */
91
        $event = $this->getDoctrine()->getRepository('AppBundle:Event')
92
            ->findByGoogleId($id);
93
        $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...
94
        if (!$user) {
95
            throw new JsonHttpException(404, 'User not found.');
96
        }
97
        $googleEvent = $this->get('app.google_calendar')
98
            ->getEventById($id);
99
        $event = new DtoEvent($googleEvent);
100
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
101
        return new JsonResponse(['user' => $user, 'event' => $event]);
102
    }
103
104
    /**
105
     * @Route("/user/{id}")
106
     * @Method("GET")
107
     *
108
     * @return JsonResponse
109
     */
110
    public function userEventsAction($id)
111
    {
112
        $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id);
113
        $events = $user->getEvents();
114
        $calendar = $this->get('app.google_calendar');
115
        $googleEvents = [];
116
        foreach ($events as $event) {
117
            $googleEvents[] = $calendar
118
                ->getEventById($event->getGoogleId());
119
        }
120
        $events = [];
121
        foreach ($googleEvents as $event) {
122
            if ($event) {
123
                $events[] = new DtoEvent($event);
124
            }
125
        }
126
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
127
128
        return new JsonResponse(['user' => $user, 'events' => $events]);
129
    }
130
131
    /**
132
     * @param $id
133
     * @Route("/{id}")
134
     * @Method("DELETE")
135
     * @return JsonResponse
136
     */
137
    public function removeEventAction($id)
138
    {
139
        $this->get('app.google_calendar')
140
            ->deleteEvent($id);
141
        $em = $this->getDoctrine()->getManager();
142
        /** @var Event $event */
143
        $event = $em->getRepository('AppBundle:Event')->findByGoogleId($id);
144
        $em->remove($event);
145
        $em->flush();
146
147
        return $this->json(['success' => 'Event was removed']);
148
    }
149
150
    /**
151
     * @param Request $request
152
     * @Method("PATCH")
153
     * @Route("/{id}")
154
     *
155
     * @return JsonResponse
156
     */
157
    public function editEventAction(Request $request, $id)
158
    {
159
        $data = json_decode($request->getContent(), true);
160
161 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...
162
            throw new JsonHttpException(400, 'Bad request.');
163
        }
164
165
        $dtoEvent = new DtoEvent();
166
        $form = $this->createForm(EventType::class, $dtoEvent);
167
        $this->handleJsonForm($form, $request);
168
        $this->get('app.google_calendar')
169
            ->editEvent($dtoEvent, $id, $request->query->all());
170
171
        return $this->json(['success' => 'Event edited']);
172
    }
173
174
    /**
175
     * FOR DEV ONLY.
176
     * @Method("PUT")
177
     * @Route("/clear")
178
     */
179
    public function clearAction()
180
    {
181
        $result = $this->get('app.google_calendar')->clear();
182
183
        return $this->json($result);
184
    }
185
}
186