src/EventSubscriber/Validation/ParticipationSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Validation;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Participation\Coupon;
  5. use App\Entity\Participation\Odr;
  6. use App\Entity\Participation\Participation;
  7. use App\Entity\Participation\Refund;
  8. use App\Entity\Participation\Step;
  9. use App\Entity\User\User;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Event\ViewEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  15. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  16. final class ParticipationSubscriber implements EventSubscriberInterface
  17. {
  18.     private $user;
  19.     private AuthorizationCheckerInterface $authChecker;
  20.     public function __construct(TokenStorageInterface $tokenStorageAuthorizationCheckerInterface $authChecker)
  21.     {
  22.         if (!$tokenStorage->getToken()) {
  23.             return;
  24.         }
  25.         $this->user $tokenStorage->getToken()->getUser();
  26.         $this->authChecker $authChecker;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             KernelEvents::VIEW => [
  32.                 ['addCompany'EventPriorities::PRE_VALIDATE],
  33.                 ['setUser'EventPriorities::PRE_VALIDATE],
  34.             ],
  35.         ];
  36.     }
  37.     public function setUser(ViewEvent $event)
  38.     {
  39.         $part $event->getControllerResult();
  40.         $method $event->getRequest()->getMethod();
  41.         if (
  42.             !$part instanceof Participation &&
  43.             !$part instanceof Coupon &&
  44.             !$part instanceof Refund &&
  45.             !$part instanceof Step ||
  46.             !$part instanceof Odr ||
  47.             !in_array($method, [Request::METHOD_POSTRequest::METHOD_PUT])) {
  48.             return;
  49.         }
  50.         if(!$this->user instanceof User) {
  51.             throw new \Exception("Invalid User");
  52.         }
  53.         $part->setUser($this->user);
  54.     }
  55.     public function addCompany(ViewEvent $event)
  56.     {
  57.         $part $event->getControllerResult();
  58.         $method $event->getRequest()->getMethod();
  59.         if (
  60.             !$part instanceof Participation &&
  61.             !$part instanceof Coupon &&
  62.             !$part instanceof Refund &&
  63.             !$part instanceof Step ||
  64.             !$part instanceof Odr ||
  65.             !in_array($method, [Request::METHOD_POSTRequest::METHOD_PUT])) {
  66.             return;
  67.         }
  68.         if(!$this->user instanceof User) {
  69.             throw new \Exception("Invalid User");
  70.         }
  71.         if (null === $this->user->getCompany()) {
  72.             throw new \Exception("Invalid Company");
  73.         }
  74.         $company $this->user->getCompany();
  75.         $part->setCompany($company);
  76.     }
  77. }