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
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunContext; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Kevin Bond <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class ScheduleRunContext extends RunContext |
14
|
|
|
{ |
15
|
|
|
private $schedule; |
16
|
|
|
private $dueTasks; |
17
|
|
|
private $force; |
18
|
|
|
|
19
|
|
|
private $taskRunContexts; |
20
|
|
|
private $skipReason; |
21
|
|
|
|
22
|
|
|
private $results; |
23
|
|
|
private $successful; |
24
|
|
|
private $failures; |
25
|
|
|
private $skipped; |
26
|
|
|
private $run; |
27
|
|
|
|
28
|
78 |
|
public function __construct(Schedule $schedule, Task ...$forcedTasks) |
29
|
|
|
{ |
30
|
78 |
|
$this->schedule = $schedule; |
31
|
78 |
|
$this->dueTasks = empty($forcedTasks) ? $schedule->due() : $forcedTasks; |
32
|
78 |
|
$this->force = !empty($forcedTasks); |
33
|
|
|
|
34
|
78 |
|
parent::__construct(); |
35
|
78 |
|
} |
36
|
|
|
|
37
|
1 |
|
public function __toString(): string |
38
|
|
|
{ |
39
|
1 |
|
return 'The Schedule'; |
40
|
|
|
} |
41
|
|
|
|
42
|
40 |
|
public function getSchedule(): Schedule |
43
|
|
|
{ |
44
|
40 |
|
return $this->schedule; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Task[] |
49
|
|
|
*/ |
50
|
37 |
|
public function dueTasks(): array |
51
|
|
|
{ |
52
|
37 |
|
return $this->dueTasks; |
53
|
|
|
} |
54
|
|
|
|
55
|
25 |
|
public function isForceRun(): bool |
56
|
|
|
{ |
57
|
25 |
|
return $this->force; |
58
|
|
|
} |
59
|
|
|
|
60
|
35 |
|
public function setTaskRunContexts(TaskRunContext ...$contexts): void |
61
|
|
|
{ |
62
|
35 |
|
$contextCount = \count($contexts); |
63
|
35 |
|
$dueCount = \count($this->dueTasks()); |
64
|
|
|
|
65
|
35 |
|
if ($contextCount !== $dueCount) { |
66
|
1 |
|
throw new \LogicException("The number of results ({$contextCount}) does not match the number of due tasks ({$dueCount})."); |
67
|
|
|
} |
68
|
|
|
|
69
|
34 |
|
$this->markAsRun(\memory_get_peak_usage(true)); |
70
|
|
|
|
71
|
34 |
|
$this->taskRunContexts = $contexts; |
72
|
34 |
|
} |
73
|
|
|
|
74
|
3 |
|
public function skip(SkipSchedule $exception): void |
75
|
|
|
{ |
76
|
3 |
|
$this->skipReason = $exception->getMessage(); |
77
|
3 |
|
} |
78
|
|
|
|
79
|
3 |
|
public function getSkipReason(): ?string |
80
|
|
|
{ |
81
|
3 |
|
return $this->skipReason; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return TaskRunContext[] |
86
|
|
|
* |
87
|
|
|
* @throws \LogicException if has not yet run |
88
|
|
|
*/ |
89
|
35 |
|
public function getTaskRunContexts(): array |
90
|
|
|
{ |
91
|
35 |
|
$this->ensureHasRun(); |
92
|
|
|
|
93
|
34 |
|
return $this->taskRunContexts; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return Result[] |
98
|
|
|
* |
99
|
|
|
* @throws \LogicException if has not yet run |
100
|
|
|
*/ |
101
|
35 |
|
public function getResults(): array |
102
|
|
|
{ |
103
|
35 |
|
if (null !== $this->results) { |
104
|
16 |
|
return $this->results; |
105
|
|
|
} |
106
|
|
|
|
107
|
35 |
|
$this->results = []; |
108
|
|
|
|
109
|
35 |
|
foreach ($this->getTaskRunContexts() as $context) { |
110
|
30 |
|
$this->results[] = $context->getResult(); |
111
|
|
|
} |
112
|
|
|
|
113
|
34 |
|
return $this->results; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws \LogicException if has not yet run and has not been marked as skipped |
118
|
|
|
*/ |
119
|
37 |
|
public function isSuccessful(): bool |
120
|
|
|
{ |
121
|
37 |
|
return $this->isSkipped() || 0 === \count($this->getFailures()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws \LogicException if has not yet run |
126
|
|
|
*/ |
127
|
34 |
|
public function isFailure(): bool |
128
|
|
|
{ |
129
|
34 |
|
return !$this->isSuccessful(); |
130
|
|
|
} |
131
|
|
|
|
132
|
37 |
|
public function isSkipped(): bool |
133
|
|
|
{ |
134
|
37 |
|
return null !== $this->skipReason; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return Result[] |
139
|
|
|
* |
140
|
|
|
* @throws \LogicException if has not yet run |
141
|
|
|
*/ |
142
|
15 |
|
public function getSuccessful(): array |
143
|
|
|
{ |
144
|
15 |
|
if (null !== $this->successful) { |
145
|
|
|
return $this->successful; |
146
|
|
|
} |
147
|
|
|
|
148
|
15 |
|
$this->successful = []; |
149
|
|
|
|
150
|
15 |
|
foreach ($this->getResults() as $result) { |
151
|
13 |
|
if ($result->isSuccessful()) { |
152
|
10 |
|
$this->successful[] = $result; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
15 |
|
return $this->successful; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return Result[] |
161
|
|
|
* |
162
|
|
|
* @throws \LogicException if has not yet run |
163
|
|
|
*/ |
164
|
34 |
|
public function getFailures(): array |
165
|
|
|
{ |
166
|
34 |
|
if (null !== $this->failures) { |
167
|
34 |
|
return $this->failures; |
168
|
|
|
} |
169
|
|
|
|
170
|
34 |
|
$this->failures = []; |
171
|
|
|
|
172
|
34 |
|
foreach ($this->getResults() as $result) { |
173
|
30 |
|
if ($result->isFailure()) { |
174
|
17 |
|
$this->failures[] = $result; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
34 |
|
return $this->failures; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @return Result[] |
183
|
|
|
* |
184
|
|
|
* @throws \LogicException if has not yet run |
185
|
|
|
*/ |
186
|
15 |
|
public function getSkipped(): array |
187
|
|
|
{ |
188
|
15 |
|
if (null !== $this->skipped) { |
189
|
|
|
return $this->skipped; |
190
|
|
|
} |
191
|
|
|
|
192
|
15 |
|
$this->skipped = []; |
193
|
|
|
|
194
|
15 |
|
foreach ($this->getResults() as $result) { |
195
|
13 |
|
if ($result->isSkipped()) { |
196
|
2 |
|
$this->skipped[] = $result; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
15 |
|
return $this->skipped; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return Result[] |
205
|
|
|
* |
206
|
|
|
* @throws \LogicException if has not yet run |
207
|
|
|
*/ |
208
|
15 |
|
public function getRun(): array |
209
|
|
|
{ |
210
|
15 |
|
if (null !== $this->run) { |
211
|
|
|
return $this->run; |
212
|
|
|
} |
213
|
|
|
|
214
|
15 |
|
$this->run = []; |
215
|
|
|
|
216
|
15 |
|
foreach ($this->getResults() as $result) { |
217
|
13 |
|
if ($result->hasRun()) { |
218
|
12 |
|
$this->run[] = $result; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
15 |
|
return $this->run; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|