Completed
Pull Request — dev (#25)
by nonanerz
03:20
created

CalendarController::newEventAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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