<?php
namespace App\EventSubscriber;
use ApiPlatform\Symfony\EventListener\EventPriorities;
use App\Entity\Document;
use App\Entity\Site;
use App\Evaluation\EvaluationPrepare;
use App\Services\SiteService;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
final class SiteSubscriber implements EventSubscriberInterface
{
public function __construct(
private EvaluationPrepare $evaluationPrepare,
private Security $security,
private readonly ParameterBagInterface $parameterBag,
private SiteService $siteService
)
{
$this->security = $security;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['prepareSite', EventPriorities::POST_WRITE],
];
}
public function prepareSite(ViewEvent $event): void
{
$method = $event->getRequest()->getMethod();
$site = $event->getControllerResult();
if ((!$site instanceof Site) || $method != 'PUT') {
return;
}
try {
$paramsArray = json_decode($event->getRequest()->getContent(), true);
$docsIds = $paramsArray['docsIds'] ?? false;
$idUser = $paramsArray['idUser'] ?? false;
if($docsIds) {
$this->siteService->siteAffectationDoc($site, $docsIds, $idUser);
}
} catch (\Throwable $throwable) {
header('Access-Control-Allow-Origin: *');
dd($throwable->getMessage());
}
}
}