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

CalendarController::usersEventAction()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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