Completed
Push — master ( 6a6e14...e9047f )
by Julián
02:21
created

AbstractScheduled::setTimeZone()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 5
eloc 11
nc 6
nop 1
1
<?php
2
/**
3
 * Effortless maintenance management (http://juliangut.com/janitor)
4
 *
5
 * @link https://github.com/juliangut/janitor for the canonical source repository
6
 *
7
 * @license https://github.com/juliangut/janitor/blob/master/LICENSE
8
 */
9
10
namespace Janitor\Watcher\Scheduled;
11
12
use Janitor\ScheduledWatcher;
13
14
/**
15
 * Class AbstractScheduled
16
 */
17
abstract class AbstractScheduled implements ScheduledWatcher
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
            $timeZone = @timezone_open((string) $timeZone);
53
            if ($timeZone === false) {
54
                throw new \InvalidArgumentException(sprintf('"%s" is not a valid time zone', $timeZone));
55
            }
56
        }
57
58
        $this->timeZone = $timeZone;
59
    }
60
}
61