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
|
|
|
* Fixed date scheduled maintenance status watcher. |
16
|
|
|
* |
17
|
|
|
* Maintenance mode is considered to be On if current date is |
18
|
|
|
* - higher than start if only start is defined |
19
|
|
|
* - lower than end if only end is defined |
20
|
|
|
* - higher than start and lower than end if both are defined |
21
|
|
|
*/ |
22
|
|
|
class Fixed extends AbstractScheduled |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Maintenance start time. |
26
|
|
|
* |
27
|
|
|
* @var \DateTime |
28
|
|
|
*/ |
29
|
|
|
protected $start; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Maintenance end time. |
33
|
|
|
* |
34
|
|
|
* @var \DateTime |
35
|
|
|
*/ |
36
|
|
|
protected $end; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Fixed constructor. |
40
|
|
|
* |
41
|
|
|
* @param \DateTime|string $start |
42
|
|
|
* @param \DateTime|string $end |
43
|
|
|
* @param \DateTimeZone|string|int $timeZone |
|
|
|
|
44
|
|
|
* |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
*/ |
47
|
|
|
public function __construct($start, $end, $timeZone = null) |
48
|
|
|
{ |
49
|
|
|
$this->setStart($start); |
50
|
|
|
$this->setEnd($end); |
51
|
|
|
if ($timeZone !== null) { |
52
|
|
|
$this->setTimeZone($timeZone); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set scheduled start time. |
58
|
|
|
* |
59
|
|
|
* @param \DateTime|string $start |
60
|
|
|
* |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
* |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
|
|
public function setStart($start) |
66
|
|
|
{ |
67
|
|
|
if (!$start instanceof \DateTime) { |
68
|
|
|
try { |
69
|
|
|
$start = new \DateTime($start, $this->getTimeZone()); |
70
|
|
|
} catch (\Exception $exception) { |
71
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid DateTime', $start)); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($this->end && $start > $this->end) { |
76
|
|
|
throw new \InvalidArgumentException('Start time should come before end time'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->start = $start; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getStart() |
88
|
|
|
{ |
89
|
|
|
return $this->start; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set scheduled end time. |
94
|
|
|
* |
95
|
|
|
* @param \DateTime|string $end |
96
|
|
|
* |
97
|
|
|
* @throws \InvalidArgumentException |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setEnd($end) |
102
|
|
|
{ |
103
|
|
|
if (!$end instanceof \DateTime) { |
104
|
|
|
try { |
105
|
|
|
$end = new \DateTime($end, $this->getTimeZone()); |
106
|
|
|
} catch (\Exception $exception) { |
107
|
|
|
throw new \InvalidArgumentException(sprintf('"%s" is not a valid DateTime', $end)); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->start && $end < $this->start) { |
112
|
|
|
throw new \InvalidArgumentException('End time should come after start time'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$this->end = $end; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function getEnd() |
124
|
|
|
{ |
125
|
|
|
return $this->end; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function getScheduledTimes($count = 5) |
132
|
|
|
{ |
133
|
|
|
if (!$this->isScheduled()) { |
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return [ |
138
|
|
|
[ |
139
|
|
|
'start' => $this->start, |
140
|
|
|
'end' => $this->end, |
141
|
|
|
], |
142
|
|
|
]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public function isScheduled() |
149
|
|
|
{ |
150
|
|
|
$now = new \DateTime('now', $this->getTimeZone()); |
151
|
|
|
|
152
|
|
|
return $this->start && $now < $this->start; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function isActive() |
159
|
|
|
{ |
160
|
|
|
$now = new \DateTime(); |
161
|
|
|
|
162
|
|
|
return !($now < $this->start || $this->end < $now); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.