src/EventSubscriber/TwigGlobalSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\AccountingFirm;
  4. use App\Services\SellsyService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use Twig\Environment;
  10. class TwigGlobalSubscriber implements EventSubscriberInterface
  11. {
  12.     private Environment $twig;
  13.     private TokenStorageInterface $tokenStorage;
  14.     private SellsyService $sellsyService;
  15.     public function __construct(
  16.         Environment $twig,
  17.         TokenStorageInterface $tokenStorage,
  18.         SellsyService $sellsyService
  19.     ) {
  20.         $this->twig $twig;
  21.         $this->tokenStorage $tokenStorage;
  22.         $this->sellsyService $sellsyService;
  23.     }
  24.     public function onKernelController(ControllerEvent $event): void
  25.     {
  26.         $token $this->tokenStorage->getToken();
  27.         $user $token $token->getUser() : null;
  28.         $hasWebsite false;
  29.         $hasInternWebsite false;
  30.         $platformEnabled false;
  31.         if (is_object($user) && method_exists($user'getAccountingFirm')) {
  32.             $firm $user->getAccountingFirm();
  33.             if ($firm instanceof AccountingFirm) {
  34.                 $siteInternetValue $this->sellsyService->getWebsiteModuleValue($firm);
  35.                 $hasWebsite $firm->getWebsiteCreatedAt() !== null;
  36.                 // Site interne uniquement
  37.                 $hasInternWebsite $siteInternetValue === 'Interne';
  38.                 $platformEnabled $hasInternWebsite || $firm->isPlatformEnabled();
  39.             }
  40.         }
  41.         $this->twig->addGlobal('has_website'$hasWebsite);
  42.         $this->twig->addGlobal('has_intern_website'$hasInternWebsite);
  43.         $this->twig->addGlobal('platform_enabled'$platformEnabled);
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             KernelEvents::CONTROLLER => 'onKernelController',
  49.         ];
  50.     }
  51. }