AbstractScheduled::getTimeZone()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * janitor (http://juliangut.com/janitor).
5
 * Effortless maintenance management.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/janitor
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Janitor\Watcher;
13
14
/**
15
 * Abstract scheduled class.
16
 */
17
abstract class AbstractScheduled implements ScheduledWatcherInterface
18
{
19
    /**
20
     * Scheduled time zone.
21
     *
22
     * @var \DateTimeZone
23
     */
24
    protected $timeZone;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function getTimeZone()
30
    {
31
        if ($this->timeZone === null) {
32
            $this->timeZone = new \DateTimeZone(date_default_timezone_get());
33
        }
34
35
        return $this->timeZone;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function setTimeZone($timeZone)
42
    {
43
        if (!$timeZone instanceof \DateTimeZone) {
44
            if (is_numeric($timeZone)) {
45
                $timeZoneName = timezone_name_from_abbr(null, $timeZone * 3600, true);
46
                if ($timeZoneName === false) {
47
                    throw new \InvalidArgumentException(sprintf('"%s" is not a valid time zone', $timeZone));
48
                }
49
                $timeZone = $timeZoneName;
50
            }
51
52
            try {
53
                $timeZone = new \DateTimeZone((string) $timeZone);
54
            } catch (\Exception $exception) {
55
                throw new \InvalidArgumentException(sprintf('"%s" is not a valid time zone', $timeZone));
56
            }
57
        }
58
59
        $this->timeZone = $timeZone;
60
    }
61
}
62