1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule; |
4
|
|
|
|
5
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
6
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule; |
7
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\Result; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Kevin Bond <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
final class ScheduleRunContext extends RunContext |
13
|
|
|
{ |
14
|
|
|
private $schedule; |
15
|
|
|
private $dueTasks; |
16
|
|
|
|
17
|
|
|
private $results; |
18
|
|
|
private $skipReason; |
19
|
|
|
|
20
|
|
|
private $successful; |
21
|
|
|
private $failures; |
22
|
|
|
private $skipped; |
23
|
|
|
private $run; |
24
|
|
|
|
25
|
76 |
|
public function __construct(Schedule $schedule, Task ...$forcedTasks) |
26
|
|
|
{ |
27
|
76 |
|
$this->schedule = $schedule; |
28
|
76 |
|
$this->dueTasks = empty($forcedTasks) ? $schedule->due() : $forcedTasks; |
29
|
|
|
|
30
|
76 |
|
parent::__construct(); |
31
|
76 |
|
} |
32
|
|
|
|
33
|
1 |
|
public function __toString(): string |
34
|
|
|
{ |
35
|
1 |
|
return 'The Schedule'; |
36
|
|
|
} |
37
|
|
|
|
38
|
38 |
|
public function schedule(): Schedule |
39
|
|
|
{ |
40
|
38 |
|
return $this->schedule; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Task[] |
45
|
|
|
*/ |
46
|
35 |
|
public function dueTasks(): array |
47
|
|
|
{ |
48
|
35 |
|
return $this->dueTasks; |
49
|
|
|
} |
50
|
|
|
|
51
|
33 |
|
public function setResults(Result ...$results): void |
52
|
|
|
{ |
53
|
33 |
|
$resultCount = \count($results); |
54
|
33 |
|
$dueCount = \count($this->dueTasks()); |
55
|
|
|
|
56
|
33 |
|
if ($resultCount !== $dueCount) { |
57
|
1 |
|
throw new \LogicException(\sprintf('The number of results (%d) does not match the number of due tasks (%d).', $resultCount, $dueCount)); |
58
|
|
|
} |
59
|
|
|
|
60
|
32 |
|
$this->markAsRun(\memory_get_peak_usage(true)); |
61
|
|
|
|
62
|
32 |
|
$this->results = $results; |
63
|
32 |
|
} |
64
|
|
|
|
65
|
3 |
|
public function skip(SkipSchedule $exception): void |
66
|
|
|
{ |
67
|
3 |
|
$this->skipReason = $exception->getMessage(); |
68
|
3 |
|
} |
69
|
|
|
|
70
|
3 |
|
public function skipReason(): ?string |
71
|
|
|
{ |
72
|
3 |
|
return $this->skipReason; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return Result[] |
77
|
|
|
*/ |
78
|
33 |
|
public function getResults(): array |
79
|
|
|
{ |
80
|
33 |
|
$this->ensureHasRun(); |
81
|
|
|
|
82
|
32 |
|
return $this->results; |
83
|
|
|
} |
84
|
|
|
|
85
|
35 |
|
public function isSuccessful(): bool |
86
|
|
|
{ |
87
|
35 |
|
return $this->isSkipped() || 0 === \count($this->getFailures()); |
88
|
|
|
} |
89
|
|
|
|
90
|
32 |
|
public function isFailure(): bool |
91
|
|
|
{ |
92
|
32 |
|
return !$this->isSuccessful(); |
93
|
|
|
} |
94
|
|
|
|
95
|
35 |
|
public function isSkipped(): bool |
96
|
|
|
{ |
97
|
35 |
|
return null !== $this->skipReason; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Result[] |
102
|
|
|
*/ |
103
|
14 |
|
public function getSuccessful(): array |
104
|
|
|
{ |
105
|
14 |
|
if (null !== $this->successful) { |
106
|
|
|
return $this->successful; |
107
|
|
|
} |
108
|
|
|
|
109
|
14 |
|
$this->successful = []; |
110
|
|
|
|
111
|
14 |
|
foreach ($this->getResults() as $result) { |
112
|
12 |
|
if ($result->isSuccessful()) { |
113
|
9 |
|
$this->successful[] = $result; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
14 |
|
return $this->successful; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Result[] |
122
|
|
|
*/ |
123
|
32 |
|
public function getFailures(): array |
124
|
|
|
{ |
125
|
32 |
|
if (null !== $this->failures) { |
126
|
32 |
|
return $this->failures; |
127
|
|
|
} |
128
|
|
|
|
129
|
32 |
|
$this->failures = []; |
130
|
|
|
|
131
|
32 |
|
foreach ($this->getResults() as $result) { |
132
|
28 |
|
if ($result->isFailure()) { |
133
|
17 |
|
$this->failures[] = $result; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
32 |
|
return $this->failures; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return Result[] |
142
|
|
|
*/ |
143
|
14 |
|
public function getSkipped(): array |
144
|
|
|
{ |
145
|
14 |
|
if (null !== $this->skipped) { |
146
|
|
|
return $this->skipped; |
147
|
|
|
} |
148
|
|
|
|
149
|
14 |
|
$this->skipped = []; |
150
|
|
|
|
151
|
14 |
|
foreach ($this->getResults() as $result) { |
152
|
12 |
|
if ($result->isSkipped()) { |
153
|
2 |
|
$this->skipped[] = $result; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
14 |
|
return $this->skipped; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return Result[] |
162
|
|
|
*/ |
163
|
14 |
|
public function getRun(): array |
164
|
|
|
{ |
165
|
14 |
|
if (null !== $this->run) { |
166
|
|
|
return $this->run; |
167
|
|
|
} |
168
|
|
|
|
169
|
14 |
|
$this->run = []; |
170
|
|
|
|
171
|
14 |
|
foreach ($this->getResults() as $result) { |
172
|
12 |
|
if ($result->hasRun()) { |
173
|
11 |
|
$this->run[] = $result; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
14 |
|
return $this->run; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|