Files
kimai2/src/EventSubscriber/TimezoneSubscriber.php
2019-10-31 00:17:31 +01:00

54 lines
1.3 KiB
PHP

<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\EventSubscriber;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class TimezoneSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
protected $storage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->storage = $tokenStorage;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['setTimezone', 100],
];
}
public function setTimezone(RequestEvent $event)
{
if (null === $this->storage->getToken()) {
return;
}
$user = $this->storage->getToken()->getUser();
if (null === $user) {
return;
}
if ($user instanceof User) {
date_default_timezone_set($user->getTimezone());
}
}
}