1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Setono\SyliusSchedulerPlugin\Model; |
||
6 | |||
7 | use Cron\CronExpression; |
||
8 | use Doctrine\Common\Collections\ArrayCollection; |
||
9 | use Doctrine\Common\Collections\Collection; |
||
10 | use Doctrine\Common\Collections\Criteria; |
||
11 | |||
12 | class Schedule implements ScheduleInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | private $id; |
||
18 | |||
19 | /** |
||
20 | * @var string|null |
||
21 | */ |
||
22 | private $code; |
||
23 | |||
24 | /** |
||
25 | * @var string|null |
||
26 | */ |
||
27 | private $name; |
||
28 | |||
29 | /** |
||
30 | * @var string|null |
||
31 | */ |
||
32 | private $command; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $args = []; |
||
38 | |||
39 | /** |
||
40 | * @var string|null |
||
41 | */ |
||
42 | private $queue = JobInterface::DEFAULT_QUEUE; |
||
43 | |||
44 | /** |
||
45 | * @var int|null |
||
46 | */ |
||
47 | private $priority = JobInterface::PRIORITY_DEFAULT; |
||
48 | |||
49 | /** |
||
50 | * @var string|null |
||
51 | */ |
||
52 | private $cronExpression = self::DEFAULT_CRON_EXPRESSION; |
||
53 | |||
54 | /** |
||
55 | * @var \DateTime |
||
56 | */ |
||
57 | private $createdAt; |
||
58 | |||
59 | /** |
||
60 | * @var Collection|JobInterface[] |
||
61 | */ |
||
62 | private $jobs; |
||
63 | |||
64 | /** |
||
65 | * @var CronExpression |
||
66 | */ |
||
67 | private $cronExpressionParsed; |
||
68 | |||
69 | public function __construct() |
||
70 | { |
||
71 | $this->jobs = new ArrayCollection(); |
||
72 | $this->createdAt = new \DateTime(); |
||
73 | $this->initializeCronExpressionParsed(); |
||
74 | } |
||
75 | |||
76 | private function initializeCronExpressionParsed(): void |
||
77 | { |
||
78 | $this->cronExpressionParsed = CronExpression::factory( |
||
79 | CronExpression::isValidExpression($this->cronExpression) ? $this->cronExpression : self::DEFAULT_CRON_EXPRESSION |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | public function onPostLoad(): void |
||
84 | { |
||
85 | $this->initializeCronExpressionParsed(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | public function __toString() |
||
92 | { |
||
93 | return sprintf( |
||
94 | 'Schedule (id = %s, command = "%s")', |
||
95 | $this->id, |
||
96 | $this->command |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function isNextJobShouldBeCreated($currentTime = 'now'): bool |
||
104 | { |
||
105 | return $this->getNextRunDate($currentTime)->getTimestamp() > $this->getJobWithGreatestExecuteAfterTimestamp(); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function getNextRunDate($currentTime = 'now'): \DateTime |
||
112 | { |
||
113 | return $this->cronExpressionParsed->getNextRunDate($currentTime); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | protected function getJobWithGreatestExecuteAfterTimestamp(): int |
||
120 | { |
||
121 | $latestJob = $this->getJobWithGreatestExecuteAfter(); |
||
122 | if (!$latestJob instanceof JobInterface) { |
||
123 | return 0; |
||
124 | } |
||
125 | |||
126 | $executeAfter = $latestJob->getExecuteAfter(); |
||
127 | if (!$executeAfter instanceof \DateTime) { |
||
128 | return 0; |
||
129 | } |
||
130 | |||
131 | return $executeAfter->getTimestamp(); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | */ |
||
137 | public function getJobWithGreatestExecuteAfter(): ?JobInterface |
||
138 | { |
||
139 | if ($this->jobs->isEmpty()) { |
||
140 | return null; |
||
141 | } |
||
142 | |||
143 | $criteria = Criteria::create()->orderBy([ |
||
144 | 'executeAfter' => Criteria::ASC, |
||
145 | ]); |
||
146 | |||
147 | return $this->jobs->matching($criteria)->last(); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | public function getId() |
||
154 | { |
||
155 | return $this->id; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function getCode(): ?string |
||
162 | { |
||
163 | return $this->code; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function setCode(?string $code): void |
||
170 | { |
||
171 | $this->code = $code; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | public function getName(): ?string |
||
178 | { |
||
179 | return $this->name; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | public function setName(?string $name): void |
||
186 | { |
||
187 | $this->name = $name; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * {@inheritdoc} |
||
192 | */ |
||
193 | public function getCommand(): ?string |
||
194 | { |
||
195 | return $this->command; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | public function setCommand(?string $command): void |
||
202 | { |
||
203 | $this->command = $command; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | public function getArgs(): array |
||
210 | { |
||
211 | return $this->args; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | public function setArgs(array $args): void |
||
218 | { |
||
219 | $this->args = $args; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | public function setQueue(?string $queue): void |
||
226 | { |
||
227 | $this->queue = $queue; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function getQueue(): ?string |
||
234 | { |
||
235 | return $this->queue; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | public function setPriority(?int $priority): void |
||
242 | { |
||
243 | $this->priority = $priority; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function getPriority(): ?int |
||
250 | { |
||
251 | return $this->priority; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | public function getCronExpression(): ?string |
||
258 | { |
||
259 | return $this->cronExpression; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function setCronExpression(?string $cronExpression): void |
||
266 | { |
||
267 | $this->cronExpression = $cronExpression; |
||
268 | $this->cronExpressionParsed->setExpression( |
||
269 | CronExpression::isValidExpression($this->cronExpression) ? $this->cronExpression : self::DEFAULT_CRON_EXPRESSION |
||
270 | ); |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function setCreatedAt(\DateTime $createdAt): void |
||
277 | { |
||
278 | $this->createdAt = $createdAt; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | */ |
||
284 | public function getCreatedAt(): \DateTime |
||
285 | { |
||
286 | return $this->createdAt; |
||
287 | } |
||
288 | |||
289 | /** |
||
290 | * {@inheritdoc} |
||
291 | */ |
||
292 | public function addJob(JobInterface $job): void |
||
293 | { |
||
294 | if (!$this->hasJob($job)) { |
||
295 | $this->jobs->add($job); |
||
296 | } |
||
297 | |||
298 | if ($this !== $job->getSchedule()) { |
||
0 ignored issues
–
show
|
|||
299 | $job->setSchedule($this); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * {@inheritdoc} |
||
305 | */ |
||
306 | public function getJobs(): Collection |
||
307 | { |
||
308 | return $this->jobs; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * {@inheritdoc} |
||
313 | */ |
||
314 | public function hasJob(JobInterface $job): bool |
||
315 | { |
||
316 | return $this->jobs->contains($job); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * {@inheritdoc} |
||
321 | */ |
||
322 | public function hasJobs(): bool |
||
323 | { |
||
324 | return !$this->jobs->isEmpty(); |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * {@inheritdoc} |
||
329 | */ |
||
330 | public function removeJob(JobInterface $job): void |
||
331 | { |
||
332 | if ($this->hasJob($job)) { |
||
333 | $job->setSchedule(null); |
||
334 | $this->jobs->removeElement($job); |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 |