eventDispatcher = $eventDispatcher; $this->validator = $validator; } 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->addPerson($person); break; case Request::METHOD_DELETE: $participation = $accompanyingPeriod->removePerson($person); $participation->setEndDate(new \DateTimeImmutable('now')); 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); } $this->getDoctrine()->getManager()->flush(); return $this->json($participation); } 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; } }