1 | <?php |
||
2 | |||
3 | namespace Cerbero\ConsoleTasker\Tasks; |
||
4 | |||
5 | use Cerbero\ConsoleTasker\Traits\IOAware; |
||
6 | use Illuminate\Support\Str; |
||
7 | use Throwable; |
||
8 | |||
9 | /** |
||
10 | * The abstract task. |
||
11 | * |
||
12 | */ |
||
13 | abstract class AbstractTask |
||
14 | { |
||
15 | use IOAware; |
||
16 | |||
17 | /** |
||
18 | * Whether the task succeeded. |
||
19 | * |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $succeeded = false; |
||
23 | |||
24 | /** |
||
25 | * The error that caused this task to fail. |
||
26 | * |
||
27 | * @var string|null |
||
28 | */ |
||
29 | protected $error = null; |
||
30 | |||
31 | /** |
||
32 | * The exception that caused this task to fail. |
||
33 | * |
||
34 | * @var Throwable|null |
||
35 | */ |
||
36 | protected $exception = null; |
||
37 | |||
38 | /** |
||
39 | * The exception that caused this task rollback to fail. |
||
40 | * |
||
41 | * @var Throwable|null |
||
42 | */ |
||
43 | protected $rollbackException = null; |
||
44 | |||
45 | /** |
||
46 | * Whether this task executed the rollback logic. |
||
47 | * |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $ranRollback = false; |
||
51 | |||
52 | /** |
||
53 | * Whether this task was successfully rolled back. |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $rolledback = false; |
||
58 | |||
59 | /** |
||
60 | * Run the task |
||
61 | * |
||
62 | * @return mixed |
||
63 | */ |
||
64 | abstract public function run(); |
||
65 | |||
66 | /** |
||
67 | * Retrieve this task purpose |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | public function getPurpose(): string |
||
72 | { |
||
73 | $class = class_basename(static::class); |
||
74 | |||
75 | return Str::snake($class, ' '); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Determine whether this task succeeded |
||
80 | * |
||
81 | * @return bool |
||
82 | */ |
||
83 | public function succeeded(): bool |
||
84 | { |
||
85 | return $this->succeeded; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Set the result of this step |
||
90 | * |
||
91 | * @param bool $result |
||
92 | * @return self |
||
93 | */ |
||
94 | public function setResult(bool $result): self |
||
95 | { |
||
96 | $this->succeeded = $result; |
||
97 | |||
98 | return $this; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Retrieve the error that caused this task to fail |
||
103 | * |
||
104 | * @return string|null |
||
105 | */ |
||
106 | public function getError(): ?string |
||
107 | { |
||
108 | return $this->error; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Set the error that caused this task to fail |
||
113 | * |
||
114 | * @param string $error |
||
115 | * @return self |
||
116 | */ |
||
117 | public function setError(string $error): self |
||
118 | { |
||
119 | $this->error = $error; |
||
120 | |||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Retrieve the exception that caused this task to fail |
||
126 | * |
||
127 | * @return Throwable|null |
||
128 | */ |
||
129 | public function getException(): ?Throwable |
||
130 | { |
||
131 | return $this->exception; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Set the exception that caused this task to fail |
||
136 | * |
||
137 | * @param Throwable $exception |
||
138 | * @return self |
||
139 | */ |
||
140 | public function setException(Throwable $exception): self |
||
141 | { |
||
142 | $this->exception = $exception; |
||
143 | |||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Retrieve the exception that caused this task rollback to fail |
||
149 | * |
||
150 | * @return Throwable|null |
||
151 | */ |
||
152 | public function getRollbackException(): ?Throwable |
||
153 | { |
||
154 | return $this->rollbackException; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Set the exception that caused this task rollback to fail |
||
159 | * |
||
160 | * @param Throwable $rollbackException |
||
161 | * @return self |
||
162 | */ |
||
163 | public function setRollbackException(Throwable $rollbackException): self |
||
164 | { |
||
165 | $this->rollbackException = $rollbackException; |
||
166 | |||
167 | return $this; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Determine whether this task should run |
||
172 | * |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function shouldRun(): bool |
||
176 | { |
||
177 | return true; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Retrieve the reason why this task should not run |
||
182 | * |
||
183 | * @return string|null |
||
184 | */ |
||
185 | public function getSkippingReason(): ?string |
||
186 | { |
||
187 | return null; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Determine whether this task should rollback if the given task fails |
||
192 | * |
||
193 | * @param AbstractTask|null $task |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function shouldRollbackDueTo(?AbstractTask $task): bool |
||
0 ignored issues
–
show
|
|||
197 | { |
||
198 | return true; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Rollback this task |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | public function rollback(): void |
||
207 | { |
||
208 | if ($this->ranRollback()) { |
||
209 | return; |
||
210 | } |
||
211 | |||
212 | $this->ranRollback = true; |
||
213 | $this->rolledback = $this->revert() !== false; |
||
0 ignored issues
–
show
Are you sure the usage of
$this->revert() targeting Cerbero\ConsoleTasker\Tasks\AbstractTask::revert() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Determine whether this task executed the rollback logic |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function ranRollback(): bool |
||
222 | { |
||
223 | return $this->ranRollback; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Determine whether this task was successfully rolled back |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function rolledback(): bool |
||
232 | { |
||
233 | return $this->rolledback; |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Revert this task |
||
238 | * |
||
239 | * @return mixed |
||
240 | */ |
||
241 | protected function revert() |
||
242 | { |
||
243 | return; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Determine whether the successive tasks should be stopped if this task fails |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function stopsSuccessiveTasksOnFailure(): bool |
||
252 | { |
||
253 | return true; |
||
254 | } |
||
255 | } |
||
256 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.