1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Task; |
4
|
|
|
|
5
|
|
|
use Zenstruck\ScheduleBundle\Schedule\RunContext; |
6
|
|
|
use Zenstruck\ScheduleBundle\Schedule\ScheduleRunContext; |
7
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Kevin Bond <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
final class TaskRunContext extends RunContext |
13
|
|
|
{ |
14
|
|
|
private $scheduleRunContext; |
15
|
|
|
private $task; |
16
|
|
|
|
17
|
|
|
private $result; |
18
|
|
|
|
19
|
56 |
|
public function __construct(ScheduleRunContext $scheduleRunContext, Task $task) |
20
|
|
|
{ |
21
|
56 |
|
$this->scheduleRunContext = $scheduleRunContext; |
22
|
56 |
|
$this->task = $task; |
23
|
|
|
|
24
|
56 |
|
parent::__construct(); |
25
|
56 |
|
} |
26
|
|
|
|
27
|
1 |
|
public function __toString(): string |
28
|
|
|
{ |
29
|
1 |
|
return "{$this->task()->getType()}: {$this->task()}"; |
30
|
|
|
} |
31
|
|
|
|
32
|
23 |
|
public function scheduleRunContext(): ScheduleRunContext |
33
|
|
|
{ |
34
|
23 |
|
return $this->scheduleRunContext; |
35
|
|
|
} |
36
|
|
|
|
37
|
46 |
|
public function task(): Task |
38
|
|
|
{ |
39
|
46 |
|
return $this->task; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws \LogicException if has not yet run |
44
|
|
|
*/ |
45
|
31 |
|
public function result(): Result |
46
|
|
|
{ |
47
|
31 |
|
$this->ensureHasRun(); |
48
|
|
|
|
49
|
30 |
|
return $this->result; |
50
|
|
|
} |
51
|
|
|
|
52
|
31 |
|
public function setResult(Result $result): void |
53
|
|
|
{ |
54
|
31 |
|
$resultTask = $result->getTask(); |
55
|
|
|
|
56
|
31 |
|
if ($resultTask->getId() !== $this->task()->getId()) { |
57
|
1 |
|
throw new \LogicException(\sprintf("The result's task (%s: %s) does not match the context's task (%s: %s).", $resultTask->getType(), $resultTask, $this->task()->getType(), $this->task())); |
58
|
|
|
} |
59
|
|
|
|
60
|
30 |
|
$this->markAsRun(\memory_get_usage(true)); |
61
|
|
|
|
62
|
30 |
|
$this->result = $result; |
63
|
30 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @throws \LogicException if has not yet run |
67
|
|
|
*/ |
68
|
30 |
|
public function isSuccessful(): bool |
69
|
|
|
{ |
70
|
30 |
|
return $this->result()->isSuccessful(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws \LogicException if has not yet run |
75
|
|
|
*/ |
76
|
30 |
|
public function isFailure(): bool |
77
|
|
|
{ |
78
|
30 |
|
return $this->result()->isFailure(); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|