src/Event/TimezoneSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\Security\Core\Security;
  7. class TimezoneSubscriber implements EventSubscriberInterface {
  8. public function __construct(private Security $security, private string $appTimezone) {
  9. }
  10. public static function getSubscribedEvents(): array {
  11. return [
  12. KernelEvents::CONTROLLER => ['onKernelController', 20],
  13. ];
  14. }
  15. public function onKernelController(ControllerEvent $event): void {
  16. if (!$event->isMainRequest()) {
  17. return;
  18. }
  19. if (!$this->security->getUser()) {
  20. date_default_timezone_set($this->appTimezone);
  21. return;
  22. }
  23. $customer = $this->security->getUser()->getCustomer() ?? null;
  24. if ($customer && $customer->getTimezone()) {
  25. date_default_timezone_set($customer->getTimezone());
  26. } else {
  27. date_default_timezone_set($this->appTimezone);
  28. }
  29. }
  30. }