Passed
Push — master ( e8f83f...f116c2 )
by Jan
05:37
created

SetUserTimezoneSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 10
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
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\EventSubscriber\UserSystem;
44
45
use App\Entity\UserSystem\User;
46
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
47
use Symfony\Component\HttpKernel\Event\ControllerEvent;
48
use Symfony\Component\HttpKernel\KernelEvents;
49
use Symfony\Component\Security\Core\Security;
50
51
/**
52
 * The purpose of this event listener is to set the timezone to the one preferred by the user.
53
 */
54
final class SetUserTimezoneSubscriber implements EventSubscriberInterface
55
{
56
    private $default_timezone;
57
    private $security;
58
59
    public function __construct(string $timezone, Security $security)
60
    {
61
        $this->default_timezone = $timezone;
62
        $this->security = $security;
63
    }
64
65
    public function setTimeZone(ControllerEvent $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

65
    public function setTimeZone(/** @scrutinizer ignore-unused */ ControllerEvent $event): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
    {
67
        $timezone = null;
68
69
        //Check if the user has set a timezone
70
        $user = $this->security->getUser();
71
        if ($user instanceof User && ! empty($user->getTimezone())) {
72
            $timezone = $user->getTimezone();
73
        }
74
75
        //Fill with default value if needed
76
        if (null === $timezone && ! empty($this->default_timezone)) {
77
            $timezone = $this->default_timezone;
78
        }
79
80
        //If timezone was configured anywhere set it, otherwise just use the one from php.ini
81
        if (null !== $timezone) {
82
            date_default_timezone_set($timezone);
83
        }
84
    }
85
86
    /**
87
     * Returns an array of event names this subscriber wants to listen to.
88
     *
89
     * The array keys are event names and the value can be:
90
     *
91
     *  * The method name to call (priority defaults to 0)
92
     *  * An array composed of the method name to call and the priority
93
     *  * An array of arrays composed of the method names to call and respective
94
     *    priorities, or 0 if unset
95
     *
96
     * For instance:
97
     *
98
     *  * ['eventName' => 'methodName']
99
     *  * ['eventName' => ['methodName', $priority]]
100
     *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
101
     *
102
     * @return array The event names to listen to
103
     */
104
    public static function getSubscribedEvents()
105
    {
106
        //Set the timezone shortly before executing the controller
107
        return [
108
            KernelEvents::CONTROLLER => 'setTimeZone',
109
        ];
110
    }
111
}
112