Completed
Pull Request — dev (#29)
by nonanerz
02:59
created

CalendarController::currentUserEventsAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\DTO\DtoEvent;
6
use AppBundle\Entity\Event;
7
use AppBundle\Exception\JsonHttpException;
8
use AppBundle\Form\EventType;
9
use Mcfedr\JsonFormBundle\Controller\JsonController;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
/**
16
 * @Route("/schedule/events")
17
 */
18
class CalendarController extends JsonController
19
{
20
    /**
21
     * @Route("/")
22
     * @Method("GET")
23
     *
24
     * @return JsonResponse
25
     */
26
    public function eventsListAction(Request $request)
27
    {
28
        return new JsonResponse($this->get('app.google_calendar')
29
            ->getEventList($request->query->all()));
30
    }
31
32
    /**
33
     * @param Request $request
34
     * @Route("")
35
     * @Method("POST")
36
     *
37
     * @return JsonResponse
38
     */
39
    public function newEventAction(Request $request)
40
    {
41
        $dtoEvent = new DtoEvent();
42
        $form = $this->createForm(EventType::class, $dtoEvent);
43
        $this->handleJsonForm($form, $request);
44
        $result = $this->get('app.google_calendar')
45
            ->createEvent($dtoEvent, $request->query->all());
46
        if (!$result) {
47
            throw new JsonHttpException(412, 'Event has not been created');
48
        }
49
        $em = $this->getDoctrine()->getManager();
50
        $user = $em->getRepository('AppBundle:User')
51
            ->find($dtoEvent->getUser());
52
53
        if (!$user) {
54
            throw new JsonHttpException(404, 'User not found.');
55
        }
56
        $event = new Event();
57
        $event->setGoogleId($result->id);
58
        $event->addUser($user);
59
        $user->setEvent($event);
60
61
        $em->persist($user);
62
        $em->flush();
63
64
        return $this->json(['success' => 'Event Created']);
65
    }
66
67
    /**
68
     * @param $id
69
     * @Route("/{id}")
70
     * @Method("GET")
71
     *
72
     * @return JsonResponse
73
     */
74
    public function singleEventAction($id)
75
    {
76
        /** @var Event $event */
77
        $event = $this->getDoctrine()->getRepository('AppBundle:Event')
78
            ->findByGoogleId($id);
79
        $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...
80
        if (!$user) {
81
            throw new JsonHttpException(404, 'User not found.');
82
        }
83
        $googleEvent = $this->get('app.google_calendar')
84
            ->getEventById($id);
85
        if (!$googleEvent) {
86
            throw new JsonHttpException(404, 'Event not found');
87
        }
88
        $user = $this->get('serializer')
89
            ->normalize($user, null, ['groups' => ['Short']]
90
        );
91
        return new JsonResponse(['user' => $user, 'event' => $googleEvent]);
92
    }
93
94
    /**
95
     * @Route("/user/{id}")
96
     * @Method("GET")
97
     *
98
     * @return JsonResponse
99
     */
100
    public function userEventsAction($id)
101
    {
102
        $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id);
103
        $events = $user->getEvents();
104
        $googleEvents = [];
105
        $calendar = $this->get('app.google_calendar');
106
107
        foreach ($events as $event) {
108
            $googleEvents[] = $calendar
109
                ->getEventById($event->getGoogleId());
110
        }
111
        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...
112
            throw new JsonHttpException(404, 'Events not found');
113
        }
114
115
        $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]);
116
117
        $response = array_merge(['user' => $user], ['events' => $googleEvents]);
118
119
        return new JsonResponse($response);
120
    }
121
122
    /**
123
     * @param $id
124
     * @Route("/{id}")
125
     * @Method("DELETE")
126
     * @return JsonResponse
127
     */
128
    public function removeEventAction($id)
129
    {
130
        $this->get('app.google_calendar')
131
            ->deleteEvent($id);
132
        $em = $this->getDoctrine()->getManager();
133
        /** @var Event $event */
134
        $event = $em->getRepository('AppBundle:Event')->findByGoogleId($id);
135
        $em->remove($event);
136
        $em->flush();
137
138
        return $this->json(['success' => 'Event was removed']);
139
    }
140
141
    /**
142
     * @param Request $request
143
     * @Method("PATCH")
144
     * @Route("/{id}")
145
     *
146
     * @return JsonResponse
147
     */
148
    public function editEventAction(Request $request, $id)
149
    {
150
        $dtoEvent = new DtoEvent();
151
        $form = $this->createForm(EventType::class, $dtoEvent);
152
        $this->handleJsonForm($form, $request);
153
        $this->get('app.google_calendar')
154
            ->editEvent($dtoEvent, $id, $request->query->all());
155
156
        return $this->json(['success' => 'Event edited']);
157
    }
158
159
    /**
160
     * FOR DEV ONLY.
161
     * @Method("PUT")
162
     * @Route("/clear")
163
     */
164
    public function clearAction()
165
    {
166
        $result = $this->get('app.google_calendar')->clear();
167
168
        return $this->json($result);
169
    }
170
}
171