src/EventSubscriber/TimeZoneEventSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Entity\Security\ShopManager;
  5. use App\Helper\String\StringHelper;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\Security\Core\Security;
  10. use Twig\Environment;
  11. use Twig\Extension\CoreExtension;
  12. class TimeZoneEventSubscriber implements EventSubscriberInterface
  13. {
  14.     private array $ignoredRoutePrefixes = [
  15.         'foxorders_shop_ordering_hours_',
  16.         'foxorders_shop_opening_hours_',
  17.         'foxorders_shop_closing_days_',
  18.     ];
  19.     public function __construct(
  20.         private Security $security,
  21.         private Environment $twig,
  22.         private StringHelper $stringHelper,
  23.     ) {
  24.     }
  25.     public function onKernelController(ControllerEvent $event): void
  26.     {
  27.         $request $event->getRequest();
  28.         if (Request::METHOD_GET !== $request->getMethod()) {
  29.             return;
  30.         }
  31.         $route $request->attributes->get('_route');
  32.         $isApiRoute null !== $route && str_contains($route'api_') && false === str_starts_with($route'foxorders_documentation');
  33.         if (null === $route || true === $isApiRoute || true === $this->stringHelper->contain($route$this->ignoredRoutePrefixes)) {
  34.             return;
  35.         }
  36.         $user $this->security->getUser();
  37.         if (!$user instanceof ShopManager || null === $user->getShop()) {
  38.             return;
  39.         }
  40.         $this->twig->getExtension(CoreExtension::class)->setTimezone($user->getShop()->getTimeZone());
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             'kernel.controller' => 'onKernelController',
  46.         ];
  47.     }
  48. }