RequestResolver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 47
ccs 12
cts 12
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUserTimeZone() 0 14 3
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\TimeZone\Resolver;
11
12
use Symfony\Component\HttpFoundation\RequestStack;
13
14
class RequestResolver implements ResolverInterface
15
{
16
    /**
17
     * @var RequestStack
18
     */
19
    private $request_stack;
20
21
    /**
22
     * @var bool
23
     */
24
    private $cookie_used = true;
25
26
    /**
27
     * @var string
28
     */
29
    private $cookie_param_name = '';
30
31
    /**
32
     * @param RequestStack $request_stack
33
     * @param bool $cookie_used
34
     * @param string $cookie_param_name
35
     */
36 4
    public function __construct(RequestStack $request_stack, $cookie_used, $cookie_param_name)
37
    {
38 4
        $this->request_stack = $request_stack;
39 4
        $this->cookie_used = $cookie_used;
40 4
        $this->cookie_param_name = $cookie_param_name;
41 4
    }
42
43
    /**
44
     * @return \DateTimeZone|null
45
     */
46 4
    public function getUserTimeZone()
47
    {
48 4
        if (!$this->cookie_used) {
49 1
            return null;
50
        }
51
52 3
        $time_zone = $this->request_stack->getMasterRequest()->cookies->get($this->cookie_param_name);
53
54
        try { // \DateTimeZone::listIdentifiers() not return Etc/GMT-6 and etc.
55 3
            return new \DateTimeZone($time_zone);
56 1
        } catch (\Exception $e) {
57 1
            return null;
58
        }
59
    }
60
}
61