Part-DB /
Part-DB-symfony
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
||
| 4 | * |
||
| 5 | * Copyright (C) 2019 - 2022 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 | |||
| 21 | declare(strict_types=1); |
||
| 22 | |||
| 23 | namespace App\EventSubscriber\UserSystem; |
||
| 24 | |||
| 25 | use App\Entity\UserSystem\User; |
||
| 26 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
||
| 27 | use Symfony\Component\HttpKernel\Event\ControllerEvent; |
||
| 28 | use Symfony\Component\HttpKernel\KernelEvents; |
||
| 29 | use Symfony\Component\Security\Core\Security; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The purpose of this event listener is to set the timezone to the one preferred by the user. |
||
| 33 | */ |
||
| 34 | final class SetUserTimezoneSubscriber implements EventSubscriberInterface |
||
| 35 | { |
||
| 36 | private string $default_timezone; |
||
| 37 | private Security $security; |
||
| 38 | |||
| 39 | public function __construct(string $timezone, Security $security) |
||
| 40 | { |
||
| 41 | $this->default_timezone = $timezone; |
||
| 42 | $this->security = $security; |
||
| 43 | } |
||
| 44 | |||
| 45 | public function setTimeZone(ControllerEvent $event): void |
||
|
0 ignored issues
–
show
|
|||
| 46 | { |
||
| 47 | $timezone = null; |
||
| 48 | |||
| 49 | //Check if the user has set a timezone |
||
| 50 | $user = $this->security->getUser(); |
||
| 51 | if ($user instanceof User && !empty($user->getTimezone())) { |
||
| 52 | $timezone = $user->getTimezone(); |
||
| 53 | } |
||
| 54 | |||
| 55 | //Fill with default value if needed |
||
| 56 | if (null === $timezone && !empty($this->default_timezone)) { |
||
| 57 | $timezone = $this->default_timezone; |
||
| 58 | } |
||
| 59 | |||
| 60 | //If timezone was configured anywhere set it, otherwise just use the one from php.ini |
||
| 61 | if (null !== $timezone) { |
||
| 62 | date_default_timezone_set($timezone); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns an array of event names this subscriber wants to listen to. |
||
| 68 | * |
||
| 69 | * The array keys are event names and the value can be: |
||
| 70 | * |
||
| 71 | * * The method name to call (priority defaults to 0) |
||
| 72 | * * An array composed of the method name to call and the priority |
||
| 73 | * * An array of arrays composed of the method names to call and respective |
||
| 74 | * priorities, or 0 if unset |
||
| 75 | * |
||
| 76 | * For instance: |
||
| 77 | * |
||
| 78 | * * ['eventName' => 'methodName'] |
||
| 79 | * * ['eventName' => ['methodName', $priority]] |
||
| 80 | * * ['eventName' => [['methodName1', $priority], ['methodName2']]] |
||
| 81 | * |
||
| 82 | * @return array The event names to listen to |
||
| 83 | */ |
||
| 84 | public static function getSubscribedEvents(): array |
||
| 85 | { |
||
| 86 | //Set the timezone shortly before executing the controller |
||
| 87 | return [ |
||
| 88 | KernelEvents::CONTROLLER => 'setTimeZone', |
||
| 89 | ]; |
||
| 90 | } |
||
| 91 | } |
||
| 92 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.