src/EventSubscriber/UserSystem/SetUserTimezoneSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  4.  *
  5.  * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU Affero General Public License as published
  9.  * by the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU Affero General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Affero General Public License
  18.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  19.  */
  20. declare(strict_types=1);
  21. /**
  22.  * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
  23.  *
  24.  * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
  25.  *
  26.  * This program is free software; you can redistribute it and/or
  27.  * modify it under the terms of the GNU General Public License
  28.  * as published by the Free Software Foundation; either version 2
  29.  * of the License, or (at your option) any later version.
  30.  *
  31.  * This program is distributed in the hope that it will be useful,
  32.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  34.  * GNU General Public License for more details.
  35.  *
  36.  * You should have received a copy of the GNU General Public License
  37.  * along with this program; if not, write to the Free Software
  38.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  39.  */
  40. namespace App\EventSubscriber\UserSystem;
  41. use App\Entity\UserSystem\User;
  42. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  43. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  44. use Symfony\Component\HttpKernel\KernelEvents;
  45. use Symfony\Component\Security\Core\Security;
  46. /**
  47.  * The purpose of this event listener is to set the timezone to the one preferred by the user.
  48.  */
  49. final class SetUserTimezoneSubscriber implements EventSubscriberInterface
  50. {
  51.     private $default_timezone;
  52.     private $security;
  53.     public function __construct(string $timezoneSecurity $security)
  54.     {
  55.         $this->default_timezone $timezone;
  56.         $this->security $security;
  57.     }
  58.     public function setTimeZone(ControllerEvent $event): void
  59.     {
  60.         $timezone null;
  61.         //Check if the user has set a timezone
  62.         $user $this->security->getUser();
  63.         if ($user instanceof User && !empty($user->getTimezone())) {
  64.             $timezone $user->getTimezone();
  65.         }
  66.         //Fill with default value if needed
  67.         if (null === $timezone && !empty($this->default_timezone)) {
  68.             $timezone $this->default_timezone;
  69.         }
  70.         //If timezone was configured anywhere set it, otherwise just use the one from php.ini
  71.         if (null !== $timezone) {
  72.             date_default_timezone_set($timezone);
  73.         }
  74.     }
  75.     /**
  76.      * Returns an array of event names this subscriber wants to listen to.
  77.      *
  78.      * The array keys are event names and the value can be:
  79.      *
  80.      *  * The method name to call (priority defaults to 0)
  81.      *  * An array composed of the method name to call and the priority
  82.      *  * An array of arrays composed of the method names to call and respective
  83.      *    priorities, or 0 if unset
  84.      *
  85.      * For instance:
  86.      *
  87.      *  * ['eventName' => 'methodName']
  88.      *  * ['eventName' => ['methodName', $priority]]
  89.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  90.      *
  91.      * @return array The event names to listen to
  92.      */
  93.     public static function getSubscribedEvents(): array
  94.     {
  95.         //Set the timezone shortly before executing the controller
  96.         return [
  97.             KernelEvents::CONTROLLER => 'setTimeZone',
  98.         ];
  99.     }
  100. }