1 | <?php |
||
25 | class Task |
||
26 | { |
||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | const STATUS_ENABLED = 1; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | const STATUS_DISABLED = 0; |
||
36 | |||
37 | /** |
||
38 | * @ORM\Id |
||
39 | * @ORM\GeneratedValue |
||
40 | * @ORM\Column(type="integer") |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $id; |
||
45 | |||
46 | /** |
||
47 | * @ORM\Column(type="string", length=128) |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $command = ''; |
||
52 | |||
53 | /** |
||
54 | * @ORM\Column(type="datetime", nullable=true) |
||
55 | * @Assert\DateTime() |
||
56 | * |
||
57 | * @var \DateTime|null |
||
58 | */ |
||
59 | protected $last_run; |
||
60 | |||
61 | /** |
||
62 | * @ORM\Column(type="datetime") |
||
63 | * @Assert\DateTime() |
||
64 | * |
||
65 | * @var \DateTime |
||
66 | */ |
||
67 | protected $next_run; |
||
68 | |||
69 | /** |
||
70 | * A date/time string. |
||
71 | * |
||
72 | * Valid formats are explained in Date and Time Formats. |
||
73 | * |
||
74 | * @link http://www.php.net/manual/en/datetime.formats.php |
||
75 | * @ORM\Column(type="string", length=128, nullable=true) |
||
76 | * |
||
77 | * @var string |
||
78 | */ |
||
79 | protected $modify = ''; |
||
80 | |||
81 | /** |
||
82 | * @ORM\Column(type="integer") |
||
83 | * @Assert\Choice(callback = "getStatuses") |
||
84 | * |
||
85 | * @var int |
||
86 | */ |
||
87 | protected $status = self::STATUS_DISABLED; |
||
88 | |||
89 | 27 | public function __construct() |
|
90 | { |
||
91 | 27 | $this->next_run = new \DateTime(); |
|
92 | 27 | } |
|
93 | |||
94 | /** |
||
95 | * @param string $command |
||
96 | * |
||
97 | * @return Task |
||
98 | */ |
||
99 | 1 | public function setCommand($command) |
|
100 | { |
||
101 | 1 | $this->command = $command; |
|
102 | |||
103 | 1 | return $this; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | public function getCommand() |
|
110 | { |
||
111 | 1 | return $this->command; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @return int |
||
116 | */ |
||
117 | 1 | public function getId() |
|
118 | { |
||
119 | 1 | return $this->id; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * @param \DateTime $last_run |
||
124 | * |
||
125 | * @return Task |
||
126 | */ |
||
127 | 4 | public function setLastRun(\DateTime $last_run) |
|
128 | { |
||
129 | 4 | $this->last_run = clone $last_run; |
|
130 | |||
131 | 4 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return \DateTime|null |
||
136 | */ |
||
137 | 4 | public function getLastRun() |
|
138 | { |
||
139 | 4 | return $this->last_run ? clone $this->last_run : null; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param \DateTime $next_run |
||
144 | * |
||
145 | * @return Task |
||
146 | */ |
||
147 | 2 | public function setNextRun(\DateTime $next_run) |
|
148 | { |
||
149 | 2 | $this->next_run = clone $next_run; |
|
150 | |||
151 | 2 | return $this; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * @return \DateTime |
||
156 | */ |
||
157 | 2 | public function getNextRun() |
|
158 | { |
||
159 | 2 | return clone $this->next_run; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @param int $interval |
||
164 | * |
||
165 | * @return Task |
||
166 | */ |
||
167 | 3 | public function setInterval($interval) |
|
168 | { |
||
169 | 3 | if ($interval > 0) { |
|
170 | 1 | $this->setModify(sprintf('+%s second', (int) $interval)); |
|
171 | } |
||
172 | |||
173 | 3 | return $this; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @param string $modify |
||
178 | * |
||
179 | * @return Task |
||
180 | */ |
||
181 | 5 | public function setModify($modify) |
|
182 | { |
||
183 | 5 | $this->modify = $modify; |
|
184 | |||
185 | 5 | return $this; |
|
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 9 | public function getModify() |
|
195 | |||
196 | /** |
||
197 | * @param int $status |
||
198 | * |
||
199 | * @return Task |
||
200 | */ |
||
201 | 4 | public function setStatus($status) |
|
207 | |||
208 | /** |
||
209 | * @return int |
||
210 | */ |
||
211 | 4 | public function getStatus() |
|
212 | { |
||
213 | 4 | return $this->status; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * @return int[] |
||
218 | */ |
||
219 | 1 | public static function getStatuses() |
|
220 | { |
||
221 | return [ |
||
222 | 1 | self::STATUS_DISABLED, |
|
223 | 1 | self::STATUS_ENABLED, |
|
224 | ]; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param ExecutionContextInterface $context |
||
229 | */ |
||
230 | 2 | public function isModifyValid(ExecutionContextInterface $context) |
|
239 | |||
240 | /** |
||
241 | * Update task after execution. |
||
242 | */ |
||
243 | 3 | public function executed() |
|
244 | { |
||
245 | 3 | $this->setLastRun(new \DateTime()); |
|
246 | 3 | if (!$this->getModify()) { |
|
247 | 2 | $this->setStatus(self::STATUS_DISABLED); |
|
248 | } |
||
263 | } |
||
264 |