1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glooby\TaskBundle\Annotation; |
4
|
|
|
use Cron\CronExpression; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* @author Emil Kilhage |
8
|
|
|
* @Annotation |
9
|
|
|
*/ |
10
|
|
|
class Schedule |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private static $map = [ |
16
|
|
|
'@yearly' => '0 0 1 1 *', |
17
|
|
|
'@annually' => '0 0 1 1 *', |
18
|
|
|
'@monthly' => '0 0 1 * *', |
19
|
|
|
'@weekly' => '0 0 * * 0', |
20
|
|
|
'@daily' => '0 0 * * *', |
21
|
|
|
'@hourly' => '0 * * * *', |
22
|
|
|
'@semi_hourly' => '*/30 * * * *', |
23
|
|
|
'@quarter_hourly' => '*/15 * * * *', |
24
|
|
|
'*' => '* * * * *', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $runEvery; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
public $active = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null|int |
39
|
|
|
*/ |
40
|
|
|
public $timeout = 0; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
public $version = 10; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
public $params = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $options |
54
|
|
|
*/ |
55
|
|
|
public function __construct(array $options) |
56
|
|
|
{ |
57
|
|
|
if (isset($options['value'])) { |
58
|
|
|
$options['runEvery'] = $options['value']; |
59
|
|
|
unset($options['value']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (empty($options['runEvery'])) { |
63
|
|
|
throw new \InvalidArgumentException('Missing property runEvery'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (isset(self::$map[$options['runEvery']])) { |
67
|
|
|
$options['runEvery'] = self::$map[$options['runEvery']]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
foreach ($options as $key => $value) { |
71
|
|
|
if (!property_exists($this, $key)) { |
72
|
|
|
throw new \InvalidArgumentException(sprintf('Property "%s" does not exist', $key)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->$key = $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
CronExpression::factory($this->runEvery); |
79
|
|
|
|
80
|
|
|
if (isset($this->timeout) && !is_numeric($this->timeout)) { |
81
|
|
|
throw new \InvalidArgumentException('Property "timeout" must be an int'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (!is_array($this->params)) { |
85
|
|
|
throw new \InvalidArgumentException('Property "params" must be an array'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|