Schedule   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 107
rs 10
c 0
b 0
f 0
wmc 18

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnabled() 0 3 1
A getExpression() 0 3 1
A getDescription() 0 3 1
A serialize() 0 3 1
A setDescription() 0 5 1
A setEnabled() 0 5 1
A import() 0 7 6
A export() 0 8 1
A unserialize() 0 5 1
A create() 0 6 1
A setExpression() 0 9 2
A createFromExport() 0 6 1
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