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

CalendarController::editEventAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace AppBundle\Controller;
4
5
use AppBundle\Entity\DTO\DtoEvent;
6
use AppBundle\Entity\Event;
7
use AppBundle\Entity\User;
8
use AppBundle\Form\EventType;
9
use Mcfedr\JsonFormBundle\Controller\JsonController;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\Routing\Annotation\Route;
14
15
class CalendarController extends JsonController
16
{
17
    /**
18
     * @Route("/schedule/events")
19
     * @Method("GET")
20
     *
21
     * @return JsonResponse
22
     */
23
    public function eventsListAction()
24
    {
25
        $result = $this->get('app.google_calendar')
26
            ->getEventList();
27
        $response = ['events' => $result];
28
29
        return new JsonResponse($response);
30
    }
31
32
    /**
33
     * @param Request $request
34
     * @param User    $user
0 ignored issues
show
Bug introduced by
There is no parameter named $user. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     * @Route("/api/schedule/event/new/")
36
     * @Method("POST")
37
     *
38
     * @return JsonResponse
39
     */
40
    public function newEventAction(Request $request)
41
    {
42
        $dtoEvent = new DtoEvent();
43
        $form = $this->createForm(EventType::class, $dtoEvent);
44
        $this->handleJsonForm($form, $request);
45
        $result = $this->get('app.google_calendar')
46
            ->createEvent($dtoEvent);
47
        if (!$result) {
48
            return $this->json(['error' => 'Event has not been created'], 412);
49
        }
50
        $user = $this->getUser();
51
        $em = $this->getDoctrine()->getManager();
52
        $event = new Event();
53
        $event->setGoogleId($result->id);
54
        $event->setUsers($user);
55
        $user->setEvent($event);
56
57
        $em->persist($user);
58
        $em->persist($event);
59
        $em->flush();
60
61
        return $this->json(['success' => 'Event created'], 201);
62
    }
63
64
    /**
65
     * @param $id
66
     * @Route("/schedule/event/{id}")
67
     * @Method("GET")
68
     *
69
     * @return JsonResponse
70
     */
71
    public function singleEventAction($id)
72
    {
73
        $event = $this->get('app.google_calendar')
74
            ->getEventById($id);
75
        if (!$event) {
76
            return $this->json(['error' => 'Event not found'], 404);
77
        }
78
79
        return $this->json($event, 200);
80
    }
81
82
    /**
83
     * @param User $user
84
     * @Route("/schedule/event/user/{id}")
85
     * @Method("GET")
86
     *
87
     * @return JsonResponse
88
     */
89
    public function usersEventAction(User $user)
90
    {
91
        $events = $user->getEvents();
92
93
        $googleEvents = [];
94
95
        $calendar = $this->get('app.google_calendar');
96
        foreach ($events as $event) {
97
            $googleEvents[] = $calendar
98
                ->getEventById($event->getGoogleId());
99
        }
100
        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...
101
            return $this->json(['error' => 'Events not found'], 404);
102
        }
103
104
        $response = ['events' => $googleEvents];
105
106
        return new JsonResponse($response);
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 Request $request
125
     * @param $id
126
     * @Route("/schedule/event/edit/{id}")
127
     *
128
     * @return JsonResponse
129
     */
130
    public function editEventAction(Request $request, $id)
131
    {
132
        $data = $request->getContent();
133
        $result = $this->get('app.google_calendar')
134
            ->editEvent($id, json_decode($data, true));
135
136
        return $this->json($result);
137
    }
138
139
    /**
140
     * FOR DEV ONLY.
141
     *
142
     * @Route("/schedule/clear")
143
     */
144
    public function clearAction()
145
    {
146
        $result = $this->get('app.google_calendar')->clear();
147
148
        return $this->json($result);
149
    }
150
}
151