1 | <?php |
||
19 | class CalendarController extends JsonController |
||
20 | { |
||
21 | /** |
||
22 | * @Route("/") |
||
23 | * @Method("GET") |
||
24 | * |
||
25 | * @return JsonResponse |
||
26 | */ |
||
27 | public function eventsListAction(Request $request) |
||
28 | { |
||
29 | return new JsonResponse($this->get('app.google_calendar') |
||
30 | ->getEventList($request->query->all())); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param Request $request |
||
35 | * @Route("") |
||
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, $request->query->all()); |
||
47 | if (!$result) { |
||
48 | throw new JsonHttpException(412, 'Event has not been created'); |
||
49 | } |
||
50 | $em = $this->getDoctrine()->getManager(); |
||
51 | /** @var User $user */ |
||
52 | $user = $em->getRepository('AppBundle:User') |
||
53 | ->find($dtoEvent->getUser()); |
||
54 | |||
55 | if (!$user) { |
||
56 | throw new JsonHttpException(404, 'User not found.'); |
||
57 | } |
||
58 | $event = new Event(); |
||
59 | $event->setGoogleId($result->id); |
||
60 | $event->addUser($user); |
||
61 | $user->setEvent($event); |
||
62 | |||
63 | $em->persist($user); |
||
64 | $em->flush(); |
||
65 | |||
66 | return $this->json(['success' => 'Event Created']); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param $id |
||
71 | * @Route("/{id}") |
||
72 | * @Method("GET") |
||
73 | * |
||
74 | * @return JsonResponse |
||
75 | */ |
||
76 | public function singleEventAction($id) |
||
77 | { |
||
78 | /** @var Event $event */ |
||
79 | $event = $this->getDoctrine()->getRepository('AppBundle:Event') |
||
80 | ->findByGoogleId($id); |
||
81 | $user = $event->getUsers()->first(); |
||
|
|||
82 | if (!$user) { |
||
83 | throw new JsonHttpException(404, 'User not found.'); |
||
84 | } |
||
85 | $googleEvent = $this->get('app.google_calendar') |
||
86 | ->getEventById($id); |
||
87 | if (!$googleEvent) { |
||
88 | throw new JsonHttpException(404, 'Event not found'); |
||
89 | } |
||
90 | $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]); |
||
91 | return new JsonResponse(['user' => $user, 'event' => $googleEvent]); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @Route("/user/{id}") |
||
96 | * @Method("GET") |
||
97 | * |
||
98 | * @return JsonResponse |
||
99 | */ |
||
100 | public function userEventsAction($id) |
||
101 | { |
||
102 | $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id); |
||
103 | $events = $user->getEvents(); |
||
104 | $googleEvents = []; |
||
105 | $calendar = $this->get('app.google_calendar'); |
||
106 | |||
107 | foreach ($events as $event) { |
||
108 | $googleEvents[] = $calendar |
||
109 | ->getEventById($event->getGoogleId()); |
||
110 | } |
||
111 | if (!$googleEvents) { |
||
112 | throw new JsonHttpException(404, 'Events not found'); |
||
113 | } |
||
114 | |||
115 | $user = $this->get('serializer')->normalize($user, null, ['groups' => ['Short']]); |
||
116 | |||
117 | return new JsonResponse(['user' => $user, 'events' => $googleEvents]); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param $id |
||
122 | * @Route("/{id}") |
||
123 | * @Method("DELETE") |
||
124 | * @return JsonResponse |
||
125 | */ |
||
126 | public function removeEventAction($id) |
||
138 | |||
139 | /** |
||
140 | * @param Request $request |
||
141 | * @Method("PATCH") |
||
142 | * @Route("/{id}") |
||
143 | * |
||
144 | * @return JsonResponse |
||
145 | */ |
||
146 | public function editEventAction(Request $request, $id) |
||
156 | |||
157 | /** |
||
158 | * FOR DEV ONLY. |
||
159 | * @Method("PUT") |
||
160 | * @Route("/clear") |
||
161 | */ |
||
162 | public function clearAction() |
||
168 | } |
||
169 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.