src/EventSubscriber/SiteSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Document;
  5. use App\Entity\Site;
  6. use App\Evaluation\EvaluationPrepare;
  7. use App\Services\SiteService;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Security\Core\Security;
  14. final class SiteSubscriber implements EventSubscriberInterface
  15. {
  16.     public function __construct(
  17.         private EvaluationPrepare $evaluationPrepare,
  18.         private Security $security,
  19.         private readonly ParameterBagInterface $parameterBag,
  20.         private SiteService $siteService
  21.     )
  22.     {
  23.         $this->security $security;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::VIEW => ['prepareSite'EventPriorities::POST_WRITE],
  29.         ];
  30.     }
  31.     public function prepareSite(ViewEvent $event): void
  32.     {
  33.         $method $event->getRequest()->getMethod();
  34.         $site $event->getControllerResult();
  35.         if ((!$site instanceof Site) || $method != 'PUT') {
  36.             return;
  37.         }
  38.         try {
  39.             $paramsArray  json_decode($event->getRequest()->getContent(), true);
  40.             $docsIds $paramsArray['docsIds'] ?? false;
  41.             $idUser $paramsArray['idUser'] ?? false;
  42.             if($docsIds) {
  43.                 $this->siteService->siteAffectationDoc($site$docsIds$idUser);
  44.             }
  45.         } catch (\Throwable $throwable) {
  46.             header('Access-Control-Allow-Origin: *');
  47.             dd($throwable->getMessage());
  48.         }
  49.     }
  50. }