src/EventSubscriber/AbonnementSubcriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Company;
  4. use App\Entity\User;
  5. use App\Service\Utils\Constante\AbonnementConst;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  12. use Twig\Error\LoaderError;
  13. use Twig\Error\RuntimeError;
  14. use Twig\Error\SyntaxError;
  15. class AbonnementSubcriber implements EventSubscriberInterface
  16. {
  17.     private TokenStorageInterface $tokenStorage;
  18.     public function __construct(TokenStorageInterface $tokenStorage)
  19.     {
  20.         $this->tokenStorage $tokenStorage;
  21.     }
  22.     /**
  23.      * @param RequestEvent $event
  24.      * @return void
  25.      * @throws LoaderError
  26.      * @throws RuntimeError
  27.      * @throws SyntaxError
  28.      */
  29.     public function onKernelRequest(ControllerEvent $event)
  30.     {
  31.         if (!$event->isMainRequest()) {
  32.             // don't do anything if it's not the master request
  33.             return;
  34.         }
  35.         if(!$this->tokenStorage->getToken() instanceof AnonymousToken and $this->tokenStorage->getToken() !== null){
  36.             /* @var $userCompany Company*/
  37.             $userCompany $this->tokenStorage->getToken()->getUser()->getCompany();
  38.             //abonnement suspendu = déco
  39.             if($userCompany->getStatus() === AbonnementConst::STATUS_SUSPENDED){
  40.                 $this->tokenStorage->setToken(null);
  41.             }
  42.         }
  43.     }
  44.     public static function getSubscribedEvents()
  45.     {
  46.         return [
  47.             KernelEvents::CONTROLLER=> [
  48.                 ['onKernelRequest',10]
  49.             ],
  50.         ];
  51.     }
  52. }