mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Chill\PersonBundle\Controller;
|
|
|
|
use Chill\MainBundle\CRUD\Controller\ApiController;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
|
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
use Chill\PersonBundle\Privacy\AccompanyingPeriodPrivacyEvent;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
|
|
class AccompanyingCourseApiController extends ApiController
|
|
{
|
|
protected EventDispatcherInterface $eventDispatcher;
|
|
|
|
protected ValidatorInterface $validator;
|
|
|
|
public function __construct(EventDispatcherInterface $eventDispatcher, $validator)
|
|
{
|
|
$this->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;
|
|
}
|
|
}
|