Completed
Push — develop ( 4d482c...1d1f3c )
by Michael
7s
created

ZoneInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of the Teazee package.
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @license    MIT License
9
 */
10
namespace Teazee;
11
12
use DateTimeImmutable;
13
use DateTimeZone;
14
15
/**
16
 * Value Object for a DateTimeZone at a specific moment in time.
17
 *
18
 * @author Michael Crumm <[email protected]>
19
 */
20
class ZoneInfo extends DateTimeZone
21
{
22
    /**
23
     * @var DateTimeImmutable
24
     */
25
    private $dateTime;
26
27
    /**
28
     * @var int
29
     */
30
    private $timestamp;
31
32
    /**
33
     * @var string
34
     */
35
    private $country;
36
37
    /**
38
     * @var array
39
     */
40
    private $info;
41
42
    /**
43
     * ZoneInfo constructor.
44
     *
45
     * @param string $id        IANA Timezone identifier.
46
     * @param int    $timestamp Seconds since January 1, 1970 UTC.
47
     */
48
    public function __construct($id, $timestamp)
49
    {
50 11
        parent::__construct($id);
51
52 11
        $this->timestamp = (int) $timestamp;
53 11
        $this->dateTime = new DateTimeImmutable('@'.$this->timestamp, $this);
54 11
        $this->country = $this->getLocation()['country_code'];
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getAbbreviation()
61
    {
62 5
        return $this->loadInfo()['abbr'];
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getCountry()
69
    {
70 5
        return $this->country;
71
    }
72
73
    /**
74
     * @return DateTimeImmutable
75
     */
76
    public function getDateTime()
77
    {
78 5
        return $this->dateTime;
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public function getTimestamp()
85
    {
86 10
        return $this->timestamp;
87
    }
88
89
    /**
90
     * @return int
91
     */
92
    public function getUtcOffset()
93
    {
94 7
        return $this->loadInfo()['offset'];
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function isDst()
101
    {
102 9
        return $this->loadInfo()['isdst'];
103
    }
104
105
    /**
106
     * Lazily loads the transition info.
107
     *
108
     * @return array
109
     */
110
    private function loadInfo()
111
    {
112
        if (null === $this->info) {
113 13
            $this->info = $this->getCurrentTransition($this->timestamp);
114
        }
115
116 21
        return $this->info;
117
    }
118
119
    /**
120
     * @param int $timestamp
121
     *
122
     * @return array
123
     */
124
    private function getCurrentTransition($timestamp)
125
    {
126 13
        $transitions = $this->getTransitions($timestamp, $timestamp);
127
        if (false === $transitions) {
128 1
            $transitions = [['abbr' => null, 'isdst' => null, 'offset' => null]];
129
        }
130
131 13
        return $transitions[0];
132
    }
133
}
134