|
1
|
|
|
<?php namespace Comodojo\Extender\Socket\Messages\Scheduler; |
|
2
|
|
|
|
|
3
|
|
|
use \Comodojo\Foundation\Validation\DataFilter; |
|
4
|
|
|
use \Comodojo\Extender\Traits\BaseEntityTrait; |
|
5
|
|
|
use \Cron\CronExpression; |
|
6
|
|
|
use \Serializable; |
|
7
|
|
|
use \Exception; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @package Comodojo Extender |
|
11
|
|
|
* @author Marco Giovinazzi <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
* |
|
14
|
|
|
* LICENSE: |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
class Schedule implements Serializable { |
|
26
|
|
|
|
|
27
|
|
|
use BaseEntityTrait; |
|
28
|
|
|
|
|
29
|
|
|
protected $description; |
|
30
|
|
|
|
|
31
|
|
|
protected $enabled = false; |
|
32
|
|
|
|
|
33
|
|
|
protected $expression; |
|
34
|
|
|
|
|
35
|
|
|
public function getDescription() { |
|
36
|
|
|
|
|
37
|
|
|
return $this->description; |
|
38
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function setDescription($description) { |
|
42
|
|
|
|
|
43
|
|
|
$this->description = $description; |
|
44
|
|
|
|
|
45
|
|
|
return $this; |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getExpression() { |
|
50
|
|
|
|
|
51
|
|
|
return $this->expression; |
|
52
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function setExpression($expression) { |
|
56
|
|
|
|
|
57
|
|
|
if ( !CronExpression::isValidExpression($expression) ) { |
|
58
|
|
|
throw new Exception("Invalid cron expression $expression"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->expression = $expression; |
|
62
|
|
|
|
|
63
|
|
|
return $this; |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getEnabled() { |
|
68
|
|
|
|
|
69
|
|
|
return $this->enabled; |
|
70
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setEnabled($enabled) { |
|
74
|
|
|
|
|
75
|
|
|
$this->enabled = DataFilter::filterBoolean($enabled); |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function import(array $data) { |
|
82
|
|
|
|
|
83
|
|
|
if ( !empty($data['id']) ) $this->setId($data['id']); |
|
84
|
|
|
if ( !empty($data['name']) ) $this->setName($data['name']); |
|
85
|
|
|
if ( !empty($data['description']) ) $this->setDescription($data['description']); |
|
86
|
|
|
if ( !empty($data['enabled']) ) $this->setEnabled($data['enabled']); |
|
87
|
|
|
if ( !empty($data['expression']) ) $this->setExpression($data['expression']); |
|
88
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function export() { |
|
92
|
|
|
|
|
93
|
|
|
return [ |
|
94
|
|
|
'id' => $this->getId(), |
|
95
|
|
|
'name' => $this->getName(), |
|
96
|
|
|
'description' => $this->getDescription(), |
|
97
|
|
|
'enabled' => $this->getEnabled(), |
|
98
|
|
|
'expression' => $this->getExpression() |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function serialize() { |
|
104
|
|
|
|
|
105
|
|
|
return serialize($this->export()); |
|
106
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function unserialize($data) { |
|
110
|
|
|
|
|
111
|
|
|
$data = unserialize($data); |
|
112
|
|
|
|
|
113
|
|
|
$this->import($data); |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function create($name, $expression) { |
|
118
|
|
|
|
|
119
|
|
|
$s = new Schedule(); |
|
120
|
|
|
|
|
121
|
|
|
return $s->setName($name) |
|
122
|
|
|
->setExpression($expression); |
|
123
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public static function createFromExport(array $export) { |
|
127
|
|
|
|
|
128
|
|
|
$s = new Schedule(); |
|
129
|
|
|
$s->import($export); |
|
130
|
|
|
|
|
131
|
|
|
return $s; |
|
132
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|