Completed
Pull Request — dev (#23)
by nonanerz
02:42
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(['error' => 'Event has not been created'], 412);
41
        }
42
        $em = $this->getDoctrine()->getManager();
43
        $event = new Event();
44
        $event->setGoogleId($result->id);
45
        $event->setUsers($user);
46
        $user->setEvent($event);
47
48
        $em->persist($user);
49
        $em->persist($event);
50
        $em->flush();
51
52
        return $this->json([
53
            'success' => 'Event created',
54
        ], 201);
55
    }
56
57
    /**
58
     * @param $id
59
     * @Route("/schedule/event/{id}")
60
     * @Method("GET")
61
     *
62
     * @return JsonResponse
63
     */
64
    public function singleEventAction($id)
65
    {
66
        $event = $this->get('app.google_calendar')
67
            ->getEventById($id);
68
        if (!$event) {
69
            return $this->json(['error' => 'Event not found'], 404);
70
        }
71
72
        return $this->json($event, 200);
73
    }
74
75
    /**
76
     * @param User $user
77
     * @Route("/schedule/event/user/{id}")
78
     * @Method("GET")
79
     *
80
     * @return JsonResponse
81
     */
82
    public function usersEventAction(User $user)
83
    {
84
        $events = $user->getEvents();
85
86
        $googleEvents = [];
87
88
        $calendar = $this->get('app.google_calendar');
89
        foreach ($events as $event) {
90
            $googleEvents[] = $calendar
91
                ->getEventById($event->getGoogleId());
92
        }
93
        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...
94
            return $this->json(['error' => 'Events not found'], 404);
95
        }
96
97
        return $this->json($googleEvents);
98
    }
99
100
    /**
101
     * @param $id
102
     * @Route("/schedule/event/remove/{id}")
103
     *
104
     * @return JsonResponse
105
     */
106
    public function removeEventAction($id)
107
    {
108
        $result = $this->get('app.google_calendar')
109
            ->deleteEvent($id);
110
111
        return $this->json($result);
112
    }
113
114
    /**
115
     * @param $id
116
     * @Route("/schedule/event/edit/{id}")
117
     *
118
     * @return JsonResponse
119
     */
120
    public function editEventAction($id)
121
    {
122
        $result = $this->get('app.google_calendar')
123
            ->editEvent($id);
124
125
        return $this->json($result);
126
    }
127
128
    /**
129
     * FOR DEV ONLY.
130
     *
131
     * @Route("/schedule/clear")
132
     */
133
    public function clearAction()
134
    {
135
        $result = $this->get('app.google_calendar')->clear();
136
137
        return $this->json($result);
138
    }
139
}
140