|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\ConsoleTasker; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\ConsoleTasker\Tasks\AbstractTask; |
|
6
|
|
|
use Throwable; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The tasks summary. |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class Summary |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The executed tasks. |
|
16
|
|
|
* |
|
17
|
|
|
* @var AbstractTask[] |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $executedTasks = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The succeeded tasks. |
|
23
|
|
|
* |
|
24
|
|
|
* @var AbstractTask[] |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $succeededTasks = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The failed tasks. |
|
30
|
|
|
* |
|
31
|
|
|
* @var AbstractTask[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $failedTasks = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The rolledback tasks. |
|
37
|
|
|
* |
|
38
|
|
|
* @var AbstractTask[] |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $rolledbackTasks = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The invalid tasks. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $invalidTasks = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* The first exception ever thrown. |
|
51
|
|
|
* |
|
52
|
|
|
* @var Throwable|null |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $firstException; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* The summary instance. |
|
58
|
|
|
* |
|
59
|
|
|
* @var self |
|
60
|
|
|
*/ |
|
61
|
|
|
protected static $instance; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Disable class instantiation in favor of singleton |
|
65
|
|
|
* |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function __construct() |
|
68
|
|
|
{ |
|
69
|
|
|
// call Summary::instance() instead |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Retrieve the summary instance |
|
74
|
|
|
* |
|
75
|
|
|
* @return self |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function instance(): self |
|
78
|
|
|
{ |
|
79
|
|
|
return static::$instance = static::$instance ?: new static(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Retrieve the executed tasks |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getExecutedTasks(): array |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->executedTasks; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Retrieve the succeeded tasks |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getSucceededTasks(): array |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->succeededTasks; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Retrieve the failed tasks |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getFailedTasks(): array |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->failedTasks; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Retrieve the rolledback tasks |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getRolledbackTasks(): array |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->rolledbackTasks; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Retrieve the invalid tasks |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getInvalidTasks(): array |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->invalidTasks; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Retrieve the first exception thrown, if any |
|
134
|
|
|
* |
|
135
|
|
|
* @return Throwable|null |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getFirstException(): ?Throwable |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->firstException; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Add the given task to the successfully executed tasks |
|
144
|
|
|
* |
|
145
|
|
|
* @param AbstractTask $task |
|
146
|
|
|
* @return self |
|
147
|
|
|
*/ |
|
148
|
|
|
public function addExecutedTask(AbstractTask $task): self |
|
149
|
|
|
{ |
|
150
|
|
|
$this->executedTasks[] = $task; |
|
151
|
|
|
|
|
152
|
|
|
if ($task->succeeded()) { |
|
153
|
|
|
$this->succeededTasks[] = $task; |
|
154
|
|
|
} else { |
|
155
|
|
|
$this->failedTasks[] = $task; |
|
156
|
|
|
$this->firstException = $this->firstException ?: $task->getException(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Add the given rolledback task due to the provided failed task |
|
164
|
|
|
* |
|
165
|
|
|
* @param AbstractTask $task |
|
166
|
|
|
* @param AbstractTask $failedTask |
|
167
|
|
|
* @return self |
|
168
|
|
|
*/ |
|
169
|
|
|
public function addRolledbackTask(AbstractTask $task, AbstractTask $failedTask): self |
|
170
|
|
|
{ |
|
171
|
|
|
$this->rolledbackTasks[] = new RollbackScope($task, $failedTask); |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Add the given item to the invalid tasks |
|
178
|
|
|
* |
|
179
|
|
|
* @param AbstractTask $task |
|
180
|
|
|
* @return self |
|
181
|
|
|
*/ |
|
182
|
|
|
public function addInvalidTask(AbstractTask $task): self |
|
183
|
|
|
{ |
|
184
|
|
|
$this->invalidTasks[] = $task; |
|
185
|
|
|
|
|
186
|
|
|
return $this; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Determine whether all tasks succeeded |
|
191
|
|
|
* |
|
192
|
|
|
* @return bool |
|
193
|
|
|
*/ |
|
194
|
|
|
public function succeeded(): bool |
|
195
|
|
|
{ |
|
196
|
|
|
return empty($this->failedTasks) |
|
197
|
|
|
&& empty($this->rolledbackTasks) |
|
198
|
|
|
&& count($this->executedTasks) == count($this->succeededTasks); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|