<?php
declare(strict_types=1);
namespace App\EventSubscriber;
use App\Entity\Security\ShopManager;
use App\Helper\String\StringHelper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
use Twig\Extension\CoreExtension;
class TimeZoneEventSubscriber implements EventSubscriberInterface
{
private array $ignoredRoutePrefixes = [
'foxorders_shop_ordering_hours_',
'foxorders_shop_opening_hours_',
'foxorders_shop_closing_days_',
];
public function __construct(
private Security $security,
private Environment $twig,
private StringHelper $stringHelper,
) {
}
public function onKernelController(ControllerEvent $event): void
{
$request = $event->getRequest();
if (Request::METHOD_GET !== $request->getMethod()) {
return;
}
$route = $request->attributes->get('_route');
$isApiRoute = null !== $route && str_contains($route, 'api_') && false === str_starts_with($route, 'foxorders_documentation');
if (null === $route || true === $isApiRoute || true === $this->stringHelper->contain($route, $this->ignoredRoutePrefixes)) {
return;
}
$user = $this->security->getUser();
if (!$user instanceof ShopManager || null === $user->getShop()) {
return;
}
$this->twig->getExtension(CoreExtension::class)->setTimezone($user->getShop()->getTimeZone());
}
public static function getSubscribedEvents(): array
{
return [
'kernel.controller' => 'onKernelController',
];
}
}