security = $security; $this->entityManager = $entityManager; } /** * Give an answer to a calendar invite. * * @Route("/api/1.0/calendar/calendar/{id}/answer/{answer}.json", methods={"post"}) */ public function answer(Calendar $calendar, string $answer): Response { $user = $this->security->getUser(); if (!$user instanceof User) { throw new AccessDeniedHttpException('not a regular user'); } if (null === $invite = $calendar->getInviteForUser($user)) { throw new AccessDeniedHttpException('not invited to this calendar'); } if (!$this->security->isGranted(InviteVoter::ANSWER, $invite)) { throw new AccessDeniedHttpException('not allowed to answer on this invitation'); } if (!in_array($answer, Invite::STATUSES, true) || Invite::PENDING === $answer) { throw new BadRequestHttpException('answer not valid'); } $invite->setStatus($answer); $this->entityManager->flush(); return new JsonResponse(null, Response::HTTP_ACCEPTED, [], false); } }