Completed
Pull Request — dev (#26)
by nonanerz
04:08
created

CalendarController::editEventAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
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\Form\EventType;
9
use Mcfedr\JsonFormBundle\Controller\JsonController;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Annotation\Route;
16
17
class CalendarController extends JsonController
18
{
19
    /**
20
     * @Route("/schedule/events")
21
     * @Method("GET")
22
     *
23
     * @return JsonResponse
24
     */
25
    public function eventsListAction()
26
    {
27
        $result = $this->get('app.google_calendar')
28
            ->getEventList();
29
        $response = ['events' => $result];
30
31
        return new JsonResponse($response);
32
    }
33
34
    /**
35
     * @param Request $request
36
     * @Route("/schedule/event/new")
37
     * @Method("POST")
38
     *
39
     * @return JsonResponse
40
     */
41
    public function newEventAction(Request $request)
42
    {
43
        $dtoEvent = new DtoEvent();
44
        $form = $this->createForm(EventType::class, $dtoEvent);
45
        $this->handleJsonForm($form, $request);
46
        $result = $this->get('app.google_calendar')
47
            ->createEvent($dtoEvent);
48
        if (!$result) {
49
            return $this->json(['error' => 'Event has not been created'], 412);
50
        }
51
        $em = $this->getDoctrine()->getManager();
52
        $user = $em->getRepository('AppBundle:User')
53
            ->find($dtoEvent->getUser());
54
        $event = new Event();
55
        $event->setGoogleId($result->id);
56
        $event->setUsers($user);
57
        $user->setEvent($event);
58
59
        $em->persist($user);
60
        $em->persist($event);
61
        $em->flush();
62
63
        return $this->json(['success' => 'Event Created']);
64
    }
65
66
    /**
67
     * @param $id
68
     * @Route("/schedule/event/{id}")
69
     * @Method("GET")
70
     *
71
     * @return JsonResponse
72
     */
73
    public function singleEventAction($id)
74
    {
75
        $event = $this->get('app.google_calendar')
76
            ->getEventById($id);
77
        if (!$event) {
78
            return $this->json(['error' => 'Event not found'], 404);
79
        }
80
81
        return new JsonResponse($event);
82
    }
83
84
    /**
85
     * @Route("/api/schedule/event/user")
86
     * @Method("GET")
87
     *
88
     * @return JsonResponse
89
     */
90
    public function currentUserEventsAction()
91
    {
92
        $user = $this->getUser();
93
94
        $events = $user->getEvents();
95
96
        $googleEvents = [];
97
98
        $calendar = $this->get('app.google_calendar');
99
        foreach ($events as $event) {
100
            $googleEvents[] = $calendar
101
                ->getEventById($event->getGoogleId());
102
        }
103
        if (!$googleEvents) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $googleEvents of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
            return $this->json(['error' => 'Events not found'], 404);
105
        }
106
107
        $response = ['events' => $googleEvents];
108
109
        return new JsonResponse($response);
110
    }
111
112
    /**
113
     * @Route("/schedule/event/user/{id}")
114
     * @Method("GET")
115
     *
116
     * @return JsonResponse
117
     */
118
    public function userEventsAction(User $user)
119
    {
120
        $events = $user->getEvents();
121
122
        $googleEvents = [];
123
124
        $calendar = $this->get('app.google_calendar');
125
        foreach ($events as $event) {
126
            $googleEvents[] = $calendar
127
                ->getEventById($event->getGoogleId());
128
        }
129
        if (!$googleEvents) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $googleEvents of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
130
            return $this->json(['error' => 'Events not found'], 404);
131
        }
132
133
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
134
135
        $response = ['user' => $user, 'events' => $googleEvents];
136
137
        return new JsonResponse($response);
138
    }
139
140
    /**
141
     * @param $id
142
     * @Route("/schedule/event/{id}")
143
     * @Method("DELETE")
144
     * @return JsonResponse
145
     */
146
    public function removeEventAction($id)
147
    {
148
        $result = $this->get('app.google_calendar')
149
            ->deleteEvent($id);
150
151
        return $this->json($result);
152
    }
153
154
    /**
155
     * @param Request $request
156
     * @Method("PATCH")
157
     * @Route("/schedule/event/{id}")
158
     *
159
     * @return JsonResponse
160
     */
161
    public function editEventAction(Request $request, $id)
162
    {
163
        $dtoEvent = new DtoEvent();
164
        $form = $this->createForm(EventType::class, $dtoEvent);
165
        $this->handleJsonForm($form, $request);
166
        $this->get('app.google_calendar')
167
            ->editEvent($dtoEvent, $id);
168
169
        return $this->json(['success' => 'Event edited']);
170
    }
171
172
    /**
173
     * FOR DEV ONLY.
174
     *
175
     * @Route("/schedule/clear")
176
     */
177
    public function clearAction()
178
    {
179
        $result = $this->get('app.google_calendar')->clear();
180
181
        return $this->json($result);
182
    }
183
}
184