ResponseListener   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 7
dl 0
loc 70
ccs 0
cts 27
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B onKernelResponseSaveUserTimeZone() 0 17 5
A getCookie() 0 4 1
1
<?php
2
/**
3
 * GpsLab component.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2016, Peter Gribanov
7
 * @license   http://opensource.org/licenses/MIT
8
 */
9
10
namespace GpsLab\Bundle\DateBundle\Event\Listener;
11
12
use GpsLab\Bundle\DateBundle\TimeZone\Keeper\KeeperInterface;
13
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
14
use Symfony\Component\HttpFoundation\Cookie;
15
16
class ResponseListener
17
{
18
    /**
19
     * @var KeeperInterface
20
     */
21
    private $keeper;
22
23
    /**
24
     * @var bool
25
     */
26
    private $cookie_used = true;
27
28
    /**
29
     * @var string
30
     */
31
    private $cookie_param_name = '';
32
33
    /**
34
     * @var string
35
     */
36
    private $cookie_param_offset = '';
37
38
    /**
39
     * @param KeeperInterface $tz_keeper
40
     * @param bool $cookie_used
41
     * @param string $cookie_param_name
42
     * @param string $cookie_param_offset
43
     */
44
    public function __construct(KeeperInterface $tz_keeper, $cookie_used, $cookie_param_name, $cookie_param_offset)
45
    {
46
        $this->keeper = $tz_keeper;
47
        $this->cookie_used = $cookie_used;
48
        $this->cookie_param_name = $cookie_param_name;
49
        $this->cookie_param_offset = $cookie_param_offset;
50
    }
51
52
    /**
53
     * Save user time zone.
54
     *
55
     * @param FilterResponseEvent $event
56
     */
57
    public function onKernelResponseSaveUserTimeZone(FilterResponseEvent $event)
58
    {
59
        if ($this->cookie_used && $event->isMasterRequest()) {
60
            $cookies = $event->getRequest()->cookies;
61
            $headers = $event->getResponse()->headers;
62
            $tz = $this->keeper->getUserTimeZone();
63
            $offset = $tz->getOffset($this->keeper->getDefaultDateTime());
64
65
            if (
66
                $cookies->get($this->cookie_param_name) != $tz->getName() ||
67
                $cookies->get($this->cookie_param_offset) != $offset
68
            ) {
69
                $headers->setCookie($this->getCookie($this->cookie_param_name, $tz->getName()));
70
                $headers->setCookie($this->getCookie($this->cookie_param_offset, $offset));
71
            }
72
        }
73
    }
74
75
    /**
76
     * @param string $name
77
     * @param string $value
78
     *
79
     * @return Cookie
80
     */
81
    protected function getCookie($name, $value)
82
    {
83
        return new Cookie($name, $value, strtotime('+1 year'), '/', null, false, false);
84
    }
85
}
86