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

CalendarController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 7
dl 0
loc 133
rs 10

7 Methods

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