Completed
Push — master ( adf5db...d2bae3 )
by Jan
90:12 queued 86:14
created

TimezoneListener::setTimeZone()   A

Complexity

Conditions 6
Paths 8

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 8
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 18
rs 9.2222
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\EventSubscriber;
33
34
35
use App\Entity\UserSystem\User;
36
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
37
use Symfony\Component\HttpKernel\Event\ControllerEvent;
38
use Symfony\Component\HttpKernel\KernelEvents;
39
use Symfony\Component\Security\Core\Security;
40
41
/**
42
 * The purpose of this event listener is to set the timezone to the one preferred by the user.
43
 * @package App\EventSubscriber
44
 */
45
class TimezoneListener implements EventSubscriberInterface
46
{
47
48
    protected $default_timezone;
49
    protected $security;
50
51
    public function __construct(string $timezone, Security $security)
52
    {
53
        $this->default_timezone = $timezone;
54
        $this->security = $security;
55
    }
56
57
    public function setTimeZone(ControllerEvent $event)
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

57
    public function setTimeZone(/** @scrutinizer ignore-unused */ ControllerEvent $event)

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