|
1
|
|
|
<?php namespace Comodojo\Extender\Orm\Entities; |
|
2
|
|
|
|
|
3
|
|
|
use \Doctrine\ORM\Mapping as ORM; |
|
4
|
|
|
use \Comodojo\Extender\Traits\BaseEntityTrait; |
|
5
|
|
|
use \Comodojo\Extender\Traits\ProcessEntityTrait; |
|
6
|
|
|
use \Cron\CronExpression; |
|
7
|
|
|
use \Comodojo\Foundation\Validation\DataFilter; |
|
8
|
|
|
use \DateTime; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @package Comodojo Extender |
|
12
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
13
|
|
|
* @license MIT |
|
14
|
|
|
* |
|
15
|
|
|
* LICENSE: |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23
|
|
|
* THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
* @ORM\Table(name="extender_schedule") |
|
26
|
|
|
* @ORM\Entity |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
class Schedule { |
|
30
|
|
|
|
|
31
|
|
|
use BaseEntityTrait; |
|
32
|
|
|
use ProcessEntityTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string |
|
36
|
|
|
* |
|
37
|
|
|
* @ORM\Column(name="description", type="text", length=65535, nullable=true) |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $description; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
* |
|
44
|
|
|
* @ORM\Column(name="min", type="string", length=16, nullable=false) |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $min; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
* |
|
51
|
|
|
* @ORM\Column(name="hour", type="string", length=16, nullable=false) |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $hour; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
* |
|
58
|
|
|
* @ORM\Column(name="day", type="string", length=16, nullable=false) |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $day; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string |
|
64
|
|
|
* |
|
65
|
|
|
* @ORM\Column(name="month", type="string", length=16, nullable=false) |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $month; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var string |
|
71
|
|
|
* |
|
72
|
|
|
* @ORM\Column(name="weekday", type="string", length=16, nullable=false) |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $weekday; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @var string |
|
78
|
|
|
* |
|
79
|
|
|
* @ORM\Column(name="year", type="string", length=16, nullable=false) |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $year; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @var boolean |
|
85
|
|
|
* |
|
86
|
|
|
* @ORM\Column(name="enabled", type="boolean", nullable=false) |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $enabled = 0; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @var datetime |
|
92
|
|
|
* |
|
93
|
|
|
* @ORM\Column(name="firstrun", type="datetime", nullable=true) |
|
94
|
|
|
*/ |
|
95
|
|
|
protected $firstrun; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @var datetime |
|
99
|
|
|
* |
|
100
|
|
|
* @ORM\Column(name="lastrun", type="datetime", nullable=true) |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $lastrun; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get a brief job description |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getDescription() { |
|
110
|
|
|
|
|
111
|
|
|
return $this->description; |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Set brief job description |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $description |
|
119
|
|
|
* @return Schedule |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setDescription($description) { |
|
122
|
|
|
|
|
123
|
|
|
$this->description = $description; |
|
124
|
|
|
|
|
125
|
|
|
return $this; |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get cron expression of this schedule |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getExpression() { |
|
135
|
|
|
|
|
136
|
|
|
return (string) $this->buildExpression(); |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* set cron expression for this schedule |
|
142
|
|
|
* |
|
143
|
|
|
* @param srting $expression A cron-compatible expression |
|
144
|
|
|
* @return Schedule |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setExpression($expression) { |
|
147
|
|
|
|
|
148
|
|
|
$exp = CronExpression::factory($expression); |
|
149
|
|
|
|
|
150
|
|
|
$this->minute = $exp->getExpression(0); |
|
|
|
|
|
|
151
|
|
|
$this->hour = $exp->getExpression(1); |
|
152
|
|
|
$this->day = $exp->getExpression(2); |
|
153
|
|
|
$this->month = $exp->getExpression(3); |
|
154
|
|
|
$this->weekday = $exp->getExpression(4); |
|
155
|
|
|
$this->year = $exp->getExpression(5); |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* True if job is currently enabled |
|
163
|
|
|
* |
|
164
|
|
|
* @return bool |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getEnabled() { |
|
167
|
|
|
|
|
168
|
|
|
return $this->enabled; |
|
169
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Set enable/disable status |
|
174
|
|
|
* |
|
175
|
|
|
* @param bool $enable |
|
176
|
|
|
* @return Schedule |
|
177
|
|
|
*/ |
|
178
|
|
|
public function setEnable($enable) { |
|
179
|
|
|
|
|
180
|
|
|
$ena = DataFilter::filterBoolean($enable); |
|
181
|
|
|
|
|
182
|
|
|
$this->enable = $ena; |
|
|
|
|
|
|
183
|
|
|
|
|
184
|
|
|
return $this; |
|
185
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get the first-run-date of job |
|
190
|
|
|
* |
|
191
|
|
|
* @return DateTime |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getFirstrun() { |
|
194
|
|
|
|
|
195
|
|
|
return $this->firstrun; |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Set the first-run-date of job |
|
201
|
|
|
* |
|
202
|
|
|
* @param DateTime $datetime |
|
203
|
|
|
* @return Schedule |
|
204
|
|
|
*/ |
|
205
|
|
|
public function setFirstrun(DateTime $datetime) { |
|
206
|
|
|
|
|
207
|
|
|
$this->firstrun = $datetime; |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
return $this; |
|
210
|
|
|
|
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Get the first-run-date of job |
|
215
|
|
|
* |
|
216
|
|
|
* @return DateTime |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getLastrun() { |
|
219
|
|
|
|
|
220
|
|
|
return $this->lastrun; |
|
221
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Set the first-run-date of job |
|
226
|
|
|
* |
|
227
|
|
|
* @param DateTime $datetime |
|
228
|
|
|
* @return Schedule |
|
229
|
|
|
*/ |
|
230
|
|
|
public function setLastrun(DateTime $datetime) { |
|
231
|
|
|
|
|
232
|
|
|
$this->lastrun = $datetime; |
|
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
return $this; |
|
235
|
|
|
|
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
View Code Duplication |
public function shouldRunJob(DateTime $time) { |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
$last_run = $this->getLastrun(); |
|
241
|
|
|
|
|
242
|
|
|
if ( is_null($lastrun) ) { |
|
|
|
|
|
|
243
|
|
|
$next_run = $this->getFirstrun(); |
|
244
|
|
|
} else { |
|
245
|
|
|
$next_run = $this->buildExpression()->getNextRunDate($last_run); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
return $time < $next_run; |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
View Code Duplication |
public function getNextPlannedRun(DateTime $time) { |
|
|
|
|
|
|
253
|
|
|
|
|
254
|
|
|
$last_run = $this->getLastrun(); |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
if ( is_null($lastrun) ) { |
|
|
|
|
|
|
257
|
|
|
$next_run = $this->getFirstrun(); |
|
258
|
|
|
} else { |
|
259
|
|
|
$next_run = $this->buildExpression()->getNextRunDate($time); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
return $next_run; |
|
263
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
protected function buildExpression() { |
|
267
|
|
|
|
|
268
|
|
|
$exp_string = implode(" ", [ |
|
269
|
|
|
$this->minute, |
|
270
|
|
|
$this->hour, |
|
271
|
|
|
$this->day, |
|
272
|
|
|
$this->month, |
|
273
|
|
|
$this->weekday, |
|
274
|
|
|
$this->year |
|
275
|
|
|
]); |
|
276
|
|
|
|
|
277
|
|
|
return CronExpression::factory($exp_string); |
|
278
|
|
|
|
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: