src/EventSubscriber/ControllerListenerSubscriber.php line 38

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Controller\App\AppController;
  4. use App\Controller\App\BuyController;
  5. use App\Controller\App\CompanyController;
  6. use App\Controller\App\OrderController;
  7. use Twig\Environment;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. class ControllerListenerSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var \Twig\Environment
  18.      */
  19.     private $twig;
  20.     /**
  21.      * @var \Doctrine\ORM\EntityManager
  22.      */
  23.     private $manager;
  24.     private $tokenStorage;
  25.     public function __construct(Environment $twigEntityManagerInterface $managerTokenStorageInterface $tokenStorage)
  26.     {
  27.         $this->twig $twig;
  28.         $this->manager $manager;
  29.         $this->tokenStorage $tokenStorage;
  30.     }
  31.     public function onKernelController(ControllerEvent $event)
  32.     {
  33.         if (!$event->isMainRequest())
  34.             return;
  35.         if (!$token $this->tokenStorage->getToken())
  36.             return;
  37.         // if (!$token->isAuthenticated()) return;
  38.         if (!$user $token->getUser())
  39.             return;
  40.         /** @var $user App\Entity\User */
  41.         $user $token->getUser();
  42.         $company $user->getCompany();
  43.         $controller $event->getController();
  44.         if (!is_array($controller)) {
  45.             return;
  46.         }
  47.         if (!$company) {
  48.             if (!$this->isCompanyController($controller)) {
  49.                 $event->setController(
  50.                     function () {
  51.                         return new RedirectResponse('/app/company/new/company'Response::HTTP_PERMANENTLY_REDIRECT);
  52.                     }
  53.                 );
  54.             }
  55.         } else {
  56.             if (!$company->isTrial() && !$company->hasActivePlan()) {
  57.                 if (!$this->isAllowedController($controller)) {
  58.                     $event->setController(
  59.                         function () {
  60.                             return new RedirectResponse('/app/buy/current/license'Response::HTTP_PERMANENTLY_REDIRECT);
  61.                         }
  62.                     );
  63.                 }
  64.             }
  65.         }
  66.         // $cockie = $this->manager->getRepository(Cockie::class)->findAll()[0];
  67.         $this->twig->addGlobal('company'$company);
  68.         // $this->twig->addGlobal( 'cockie', $cockie );
  69.     }
  70.     public static function getSubscribedEvents()
  71.     {
  72.         return [
  73.             'kernel.controller' => 'onKernelController',
  74.         ];
  75.     }
  76.     private function isCompanyController(array $controller)
  77.     {
  78.         return $controller[0] instanceof CompanyController true false;
  79.     }
  80.     private function isAllowedController(array $controller)
  81.     {
  82.         return ($controller[0] instanceof OrderController ||
  83.             $controller[0] instanceof BuyController ||
  84.             $controller[0] instanceof AppController ||
  85.             $controller[0] instanceof CompanyController) ? true false;
  86.     }
  87. }