Completed
Pull Request — dev (#23)
by nonanerz
02:16
created

CalendarController::eventsListAction()   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 0
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\Routing\Annotation\Route;
11
12
class CalendarController extends Controller
13
{
14
    /**
15
     * @Route("/schedule/events")
16
     * @Method("GET")
17
     *
18
     * @return JsonResponse
19
     */
20
    public function eventsListAction()
21
    {
22
        $result = $this->get('app.google_calendar')
23
            ->getEventList();
24
25
        return new JsonResponse($result);
26
    }
27
28
    /**
29
     * @param User $user
30
     * @Route("/schedule/event/new/{id}")
31
     * @Method({"GET", "POST"})
32
     *
33
     * @return JsonResponse
34
     */
35
    public function newEventAction(User $user)
36
    {
37
        $result = $this->get('app.google_calendar')
38
            ->newEvent();
39
        if (!$result) {
40
            return $this->json([
41
                'error' => 'Event has not been created',
42
            ],
43
                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([
56
            'success' => 'Event created',
57
        ], 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([
73
                'error' => 'Event not found',
74
            ],
75
                404);
76
        }
77
78
        return $this->json($event, 200);
79
    }
80
81
    /**
82
     * @param User $user
83
     * @Route("/schedule/event/user/{id}")
84
     * @Method("GET")
85
     *
86
     * @return JsonResponse
87
     */
88
    public function usersEventAction(User $user)
89
    {
90
        $events = $user->getEvents();
91
92
        $googleEvents = [];
93
94
        $calendar = $this->get('app.google_calendar');
95
        foreach ($events as $event) {
96
            $googleEvents[] = $calendar
97
                ->getEventById($event->getGoogleId());
98
        }
99
        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...
100
            return $this->json([
101
                'error' => 'Events not found',
102
            ],
103
                404);
104
        }
105
106
        return $this->json($googleEvents);
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 $id
125
     * @Route("/schedule/event/edit/{id}")
126
     *
127
     * @return JsonResponse
128
     */
129
    public function editEventAction($id)
130
    {
131
        $result = $this->get('app.google_calendar')
132
            ->editEvent($id);
133
134
        return $this->json($result);
135
    }
136
137
    /**
138
     * FOR DEV ONLY.
139
     *
140
     * @Route("/schedule/clear")
141
     */
142
    public function clearAction()
143
    {
144
        $result = $this->get('app.google_calendar')->clear();
145
146
        return $this->json($result);
147
    }
148
}
149