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

CalendarController::removeEventAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
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\Form\EventType;
9
use Mcfedr\JsonFormBundle\Controller\JsonController;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
12
use Symfony\Component\HttpFoundation\JsonResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class CalendarController extends JsonController
17
{
18
    /**
19
     * @Route("/schedule/events")
20
     * @Method("GET")
21
     *
22
     * @return JsonResponse
23
     */
24
    public function eventsListAction()
25
    {
26
        $result = $this->get('app.google_calendar')
27
            ->getEventList();
28
        $response = ['events' => $result];
29
30
        return new JsonResponse($response);
31
    }
32
33
    /**
34
     * @param Request $request
35
     * @param User    $user
36
     * @Route("/api/schedule/event/new/{id}")
37
     * @Method({"GET", "POST"})
38
     *
39
     * @return JsonResponse
40
     */
41
    public function newEventAction(Request $request, User $user)
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
        $event = new Event();
53
        $event->setGoogleId($result->id);
54
        $event->setUsers($user);
55
        $user->setEvent($event);
56
57
        $em->persist($user);
58
        $em->persist($event);
59
        $em->flush();
60
61
        return $this->json(['success' => 'Event created'], 201);
62
    }
63
64
    /**
65
     * @param $id
66
     * @Route("/schedule/event/{id}")
67
     * @Method("GET")
68
     *
69
     * @return JsonResponse
70
     */
71
    public function singleEventAction($id)
72
    {
73
        $event = $this->get('app.google_calendar')
74
            ->getEventById($id);
75
        if (!$event) {
76
            return $this->json(['error' => 'Event not found'], 404);
77
        }
78
79
        return $this->json($event, 200);
80
    }
81
82
    /**
83
     * @param User $user
84
     * @Route("/schedule/event/user/{id}")
85
     * @Method("GET")
86
     *
87
     * @return JsonResponse
88
     */
89
    public function usersEventAction(User $user)
90
    {
91
        $events = $user->getEvents();
92
93
        $googleEvents = [];
94
95
        $calendar = $this->get('app.google_calendar');
96
        foreach ($events as $event) {
97
            $googleEvents[] = $calendar
98
                ->getEventById($event->getGoogleId());
99
        }
100
        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...
101
            return $this->json(['error' => 'Events not found'], 404);
102
        }
103
104
        $response = ['events' => $googleEvents];
105
106
        return new JsonResponse($response);
107
    }
108
109
    /**
110
     * @param $id
111
     * @Route("/schedule/event/remove/{id}")
112
     *
113
     * @return JsonResponse
114
     */
115
    public function removeEventAction($id)
116
    {
117
        $result = $this->get('app.google_calendar')
118
            ->deleteEvent($id);
119
120
        return $this->json($result);
121
    }
122
123
    /**
124
     * @param Request $request
125
     * @param $id
126
     * @Route("/schedule/event/edit/{id}")
127
     *
128
     * @return JsonResponse
129
     */
130
    public function editEventAction(Request $request, $id)
131
    {
132
        $data = $request->getContent();
133
        $result = $this->get('app.google_calendar')
134
            ->editEvent($id, json_decode($data, true));
135
136
        return $this->json($result);
137
    }
138
139
    /**
140
     * FOR DEV ONLY.
141
     *
142
     * @Route("/schedule/clear")
143
     */
144
    public function clearAction()
145
    {
146
        $result = $this->get('app.google_calendar')->clear();
147
148
        return $this->json($result);
149
    }
150
}
151