1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glooby\TaskBundle\Annotation; |
4
|
|
|
|
5
|
|
|
use Cron\CronExpression; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Emil Kilhage |
9
|
|
|
* @Annotation |
10
|
|
|
*/ |
11
|
|
|
class Schedule |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
private static $map = [ |
17
|
|
|
'@yearly' => '0 0 1 1 *', |
18
|
|
|
'@annually' => '0 0 1 1 *', |
19
|
|
|
'@monthly' => '0 0 1 * *', |
20
|
|
|
'@weekly' => '0 0 * * 0', |
21
|
|
|
'@daily' => '0 0 * * *', |
22
|
|
|
'@hourly' => '0 * * * *', |
23
|
|
|
'@semi_hourly' => '*/30 * * * *', |
24
|
|
|
'@twenty_minutes' => '*/20 * * * *', |
25
|
|
|
'@quarter_hourly' => '*/15 * * * *', |
26
|
|
|
'@ten_minutes' => '*/10 * * * *', |
27
|
|
|
'@five_minutes' => '*/5 * * * *', |
28
|
|
|
'*' => '* * * * *', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $interval; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
public $active = true; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var null|int |
43
|
|
|
*/ |
44
|
|
|
public $timeout = 0; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
public $version = 10; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
public $params = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $options |
58
|
|
|
* @throws \InvalidArgumentException |
59
|
|
|
*/ |
60
|
|
|
public function __construct(array $options) |
61
|
|
|
{ |
62
|
|
|
$options = $this->setDefault($options); |
63
|
|
|
$options = $this->ensureExpressionExist($options); |
64
|
|
|
$options = $this->mapExpression($options); |
65
|
|
|
|
66
|
|
|
$this->populate($options); |
67
|
|
|
|
68
|
|
|
$this->validateExpression(); |
69
|
|
|
$this->validateTimeout(); |
70
|
|
|
$this->validateParams(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
public function validateTimeout() |
77
|
|
|
{ |
78
|
|
|
if (isset($this->timeout) && !is_numeric($this->timeout)) { |
79
|
|
|
throw new \InvalidArgumentException('Property "timeout" must be an int'); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @throws \InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
public function validateParams() |
87
|
|
|
{ |
88
|
|
|
if (!is_array($this->params)) { |
89
|
|
|
throw new \InvalidArgumentException('Property "params" must be an array'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @throws \InvalidArgumentException |
95
|
|
|
*/ |
96
|
|
|
public function validateExpression() |
97
|
|
|
{ |
98
|
|
|
CronExpression::factory($this->interval); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $options |
103
|
|
|
* @throws \InvalidArgumentException |
104
|
|
|
*/ |
105
|
|
|
public function populate(array $options) |
106
|
|
|
{ |
107
|
|
|
foreach ($options as $key => $value) { |
108
|
|
|
if (!property_exists($this, $key)) { |
109
|
|
|
throw new \InvalidArgumentException(sprintf('Property "%s" does not exist', $key)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->$key = $value; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $options |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
public function mapExpression(array $options) |
121
|
|
|
{ |
122
|
|
|
if (isset(self::$map[$options['interval']])) { |
123
|
|
|
$options['interval'] = self::$map[$options['interval']]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $options; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array $options |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function setDefault(array $options) |
134
|
|
|
{ |
135
|
|
|
if (isset($options['value'])) { |
136
|
|
|
$options['interval'] = $options['value']; |
137
|
|
|
unset($options['value']); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $options; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param array $options |
145
|
|
|
* @throws \InvalidArgumentException |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
public function ensureExpressionExist(array $options) |
149
|
|
|
{ |
150
|
|
|
if (empty($options['interval'])) { |
151
|
|
|
throw new \InvalidArgumentException('Missing property interval'); |
152
|
|
|
} |
153
|
|
|
return $options; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|