eventDispatcher = $eventDispatcher; $this->validator = $validator; $this->registry = $registry; $this->accompanyingPeriodACLAwareRepository = $accompanyingPeriodACLAwareRepository; $this->referralAvailable = $referralAvailable; } public function confirmApi($id, Request $request, $_format): Response { /** @var AccompanyingPeriod $accompanyingPeriod */ $accompanyingPeriod = $this->getEntity('participation', $id, $request); $this->checkACL('confirm', $request, $_format, $accompanyingPeriod); $workflow = $this->registry->get($accompanyingPeriod); if (FALSE === $workflow->can($accompanyingPeriod, 'confirm')) { // throw new BadRequestException('It is not possible to confirm this period'); $errors = $this->validator->validate($accompanyingPeriod, null, [$accompanyingPeriod::STEP_CONFIRMED]); if( count($errors) > 0 ){ return $this->json($errors, 422); } } $workflow->apply($accompanyingPeriod, 'confirm'); $this->getDoctrine()->getManager()->flush(); return $this->json($accompanyingPeriod, Response::HTTP_OK, [], [ 'groups' => [ 'read' ] ]); } public function participationApi($id, Request $request, $_format) { /** @var AccompanyingPeriod $accompanyingPeriod */ $accompanyingPeriod = $this->getEntity('participation', $id, $request); $person = $this->getSerializer() ->deserialize($request->getContent(), Person::class, $_format, []); if (NULL === $person) { throw new BadRequestException('person id not found'); } // TODO add acl // $this->onPostCheckACL('participation', $request, $_format, $accompanyingPeriod); switch ($request->getMethod()) { case Request::METHOD_POST: $participation = $accompanyingPeriod->createParticipationFor($person); break; case Request::METHOD_DELETE: $participation = $accompanyingPeriod->closeParticipationFor($person); break; default: throw new BadRequestException("This method is not supported"); } $errors = $this->validator->validate($accompanyingPeriod); if ($errors->count() > 0) { // only format accepted return $this->json($errors, 422); } $this->getDoctrine()->getManager()->flush(); return $this->json($participation, 200, [], ['groups' => [ 'read' ]]); } public function resourceApi($id, Request $request, string $_format): Response { $accompanyingPeriod = $this->getEntity('resource', $id, $request); $errors = $this->validator->validate($accompanyingPeriod); if ($errors->count() > 0) { return $this->json($errors, 422); } return $this->addRemoveSomething('resource', $id, $request, $_format, 'resource', Resource::class); } public function scopeApi($id, Request $request, string $_format): Response { return $this->addRemoveSomething('scope', $id, $request, $_format, 'scope', Scope::class, [ 'groups' => [ 'read' ] ]); } public function commentApi($id, Request $request, string $_format): Response { return $this->addRemoveSomething('comment', $id, $request, $_format, 'comment', Comment::class); } public function socialIssueApi($id, Request $request, string $_format): Response { return $this->addRemoveSomething('socialissue', $id, $request, $_format, 'socialIssue', SocialIssue::class, [ 'groups' => [ 'read' ] ]); } public function workApi($id, Request $request, string $_format): Response { return $this->addRemoveSomething( 'work', $id, $request, $_format, 'work', AccompanyingPeriodWork::class, [ 'groups' => [ 'accompanying_period_work:create' ] ], true // force persist ); } public function requestorApi($id, Request $request, string $_format): Response { /** @var AccompanyingPeriod $accompanyingPeriod */ $action = 'requestor'; $accompanyingPeriod = $this->getEntity($action, $id, $request); // a requestor may be a person or a thirdParty $this->checkACL($action, $request, $_format, $accompanyingPeriod); $this->onPostCheckACL($action, $request, $_format, $accompanyingPeriod); if (Request::METHOD_DELETE === $request->getMethod()) { $accompanyingPeriod->setRequestor(NULL); } elseif (Request::METHOD_POST === $request->getMethod()) { $requestor = null; $exceptions = []; foreach ([Person::class, ThirdParty::class] as $class) { try { $requestor = $this->getSerializer() ->deserialize($request->getContent(), $class, $_format, []); } catch (RuntimeException $e) { $exceptions[] = $e; } } if ($requestor === null) { throw new BadRequestException('Could not find any person or requestor', 0, $exceptions[0]); } $accompanyingPeriod->setRequestor($requestor); } else { throw new BadRequestException('method not supported'); } $errors = $this->validator->validate($accompanyingPeriod); if ($errors->count() > 0) { // only format accepted return $this->json($errors, 422); } $this->getDoctrine()->getManager()->flush(); return $this->json($accompanyingPeriod->getRequestor(), 200, [], ['groups' => [ 'read']]); } protected function onPostCheckACL(string $action, Request $request, string $_format, $entity): ?Response { $this->eventDispatcher->dispatch( AccompanyingPeriodPrivacyEvent::ACCOMPANYING_PERIOD_PRIVACY_EVENT, new AccompanyingPeriodPrivacyEvent($entity, [ 'action' => $action, 'request' => $request->getMethod() ]) ); return null; } /** * @ParamConverter("person", options={"id" = "person_id"}) */ public function getAccompanyingPeriodsByPerson(Person $person){ $accompanyingPeriods = $person->getCurrentAccompanyingPeriods(); $accompanyingPeriodsChecked = array_filter($accompanyingPeriods, function(AccompanyingPeriod $period){ return $this->isGranted(AccompanyingPeriodVoter::SEE, $period); }); return $this->json(\array_values($accompanyingPeriodsChecked), Response::HTTP_OK, [], ['groups' => [ 'read']]); } /** * @Route("/api/1.0/person/accompanying-course/{id}/referrers-suggested.{_format}", * requirements={ "_format"="json"}, * name="chill_api_person_accompanying_period_referrers_suggested") * @param AccompanyingPeriod $period * @return JsonResponse */ public function suggestReferrals(AccompanyingPeriod $period, string $_format = 'json'): JsonResponse { $this->denyAccessUnlessGranted(AccompanyingPeriodVoter::EDIT, $period); $total = $this->referralAvailable->countReferralSuggested($period); $paginator = $this->getPaginatorFactory()->create($total); if (0 < $total) { $users = $this->referralAvailable->findReferralSuggested($period, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber()); } else { $users = []; } return $this->json(new Collection($users, $paginator), Response::HTTP_OK, [], [ AbstractNormalizer::GROUPS => [ 'read' ]]); } }