Repository navigation
Expand file tree
/
Copy pathDeadlinePolicy.php
More file actions
65 lines (52 loc) · 2.62 KB
/
Copy pathDeadlinePolicy.php
File metadata and controls
65 lines (52 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace SchedulerBundle\SchedulePolicy;
use DateInterval;
use DateTimeImmutable;
use SchedulerBundle\Exception\RuntimeException;
use SchedulerBundle\Task\TaskInterface;
use SchedulerBundle\Task\TaskListInterface;
use function sprintf;
/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class DeadlinePolicy implements PolicyInterface
{
/**
* {@inheritdoc}
*/
public function sort(TaskListInterface $tasks): TaskListInterface
{
$tasks->walk(func: static function (TaskInterface $task): void {
$arrivalTime = $task->getArrivalTime();
if (!$arrivalTime instanceof DateTimeImmutable) {
throw new RuntimeException(message: sprintf('The arrival time must be defined, consider executing the task "%s" first', $task->getName()));
}
$executionRelativeDeadline = $task->getExecutionRelativeDeadline();
if (!$executionRelativeDeadline instanceof DateInterval) {
throw new RuntimeException(message: sprintf('The execution relative deadline must be defined, consider using %s::setExecutionRelativeDeadline()', TaskInterface::class));
}
$absoluteDeadlineDate = $arrivalTime->add(interval: $executionRelativeDeadline);
$task->setExecutionAbsoluteDeadline(dateInterval: $absoluteDeadlineDate->diff(targetObject: $arrivalTime));
});
return $tasks->uasort(func: static function (TaskInterface $task, TaskInterface $nextTask): int {
$dateTimeImmutable = new DateTimeImmutable();
$currentTaskExecutionAbsoluteDeadline = $task->getExecutionAbsoluteDeadline();
if (!$currentTaskExecutionAbsoluteDeadline instanceof DateInterval) {
throw new RuntimeException(message: sprintf('The execution absolute deadline must be defined, consider using %s::setExecutionAbsoluteDeadline()', TaskInterface::class));
}
$nextTaskExecutionAbsoluteDeadline = $nextTask->getExecutionAbsoluteDeadline();
if (!$nextTaskExecutionAbsoluteDeadline instanceof DateInterval) {
throw new RuntimeException(message: sprintf('The execution absolute deadline must be defined, consider using %s::setExecutionAbsoluteDeadline()', TaskInterface::class));
}
return $dateTimeImmutable->add(interval: $nextTaskExecutionAbsoluteDeadline) <=> $dateTimeImmutable->add(interval: $currentTaskExecutionAbsoluteDeadline);
});
}
/**
* {@inheritdoc}
*/
public function support(string $policy): bool
{
return 'deadline' === $policy;
}
}