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