|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Evacuator package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Serafim\Evacuator; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class Evacuator |
|
12
|
|
|
* @package Serafim\Evacuator |
|
13
|
|
|
*/ |
|
14
|
|
|
class Evacuator |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
* @internal |
|
19
|
|
|
*/ |
|
20
|
|
|
const CATCH_ALL_EXCEPTIONS = '*'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
const INFINITY_RETRIES = -100; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var \Closure |
|
29
|
|
|
*/ |
|
30
|
|
|
private $context; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $retries = 0; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array|\Closure[] |
|
39
|
|
|
*/ |
|
40
|
|
|
private $catches = []; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var null|\Closure |
|
44
|
|
|
*/ |
|
45
|
|
|
private $then; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var array|\Closure |
|
49
|
|
|
*/ |
|
50
|
|
|
private $everyError = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Evacuator constructor. |
|
54
|
|
|
* @param \Closure $context |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct(\Closure $context) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->context = $context; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array ...$args |
|
63
|
|
|
* @return mixed |
|
64
|
|
|
* @throws \Throwable |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __invoke(...$args) |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->invoke(...$args); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param int $count |
|
73
|
|
|
* @return Evacuator |
|
74
|
|
|
* @throws \InvalidArgumentException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function retries(int $count): Evacuator |
|
77
|
|
|
{ |
|
78
|
|
|
if ($count < 0 && $count !== static::INFINITY_RETRIES) { |
|
79
|
|
|
throw new \InvalidArgumentException('Retries count must be greater than 0'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$this->retries = $count; |
|
83
|
|
|
|
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param \Closure $then |
|
89
|
|
|
* @return Evacuator|$this |
|
90
|
|
|
* @throws \InvalidArgumentException |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function catch(\Closure $then): Evacuator |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$exceptionClass = $this->resolveTypeHint($then, \Throwable::class); |
|
95
|
|
|
|
|
96
|
|
|
$this->catches[$exceptionClass] = $then; |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param \Closure $then |
|
103
|
|
|
* @return Evacuator |
|
104
|
|
|
* @throws \InvalidArgumentException |
|
105
|
|
|
*/ |
|
106
|
|
View Code Duplication |
public function onError(\Closure $then): Evacuator |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
$exceptionClass = $this->resolveTypeHint($then, \Throwable::class); |
|
109
|
|
|
|
|
110
|
|
|
$this->everyError[$exceptionClass] = $then; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param \Closure $closure |
|
117
|
|
|
* @param string $instanceOf |
|
118
|
|
|
* @return string |
|
119
|
|
|
* @throws \InvalidArgumentException |
|
120
|
|
|
*/ |
|
121
|
|
|
private function resolveTypeHint(\Closure $closure, string $instanceOf): string |
|
122
|
|
|
{ |
|
123
|
|
|
$parameters = (new \ReflectionFunction($closure))->getParameters(); |
|
124
|
|
|
|
|
125
|
|
|
// Callback has ony one argument |
|
126
|
|
|
if (1 !== count($parameters)) { |
|
127
|
|
|
throw new \InvalidArgumentException( |
|
128
|
|
|
'Closure argument of catch(...) method required ' . (count($parameters) > 1 ? 'only ' :'') . |
|
129
|
|
|
'1 parameter ' . count($parameters) . ' given' |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Callback has no type hints |
|
134
|
|
|
if (null === reset($parameters)->getType()) { |
|
135
|
|
|
return static::CATCH_ALL_EXCEPTIONS; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$typeHintClass = reset($parameters)->getClass(); |
|
139
|
|
|
|
|
140
|
|
|
// Callback has primitive type hint |
|
141
|
|
|
if (null === $typeHintClass) { |
|
142
|
|
|
throw new \InvalidArgumentException( |
|
143
|
|
|
'Closure argument of catch(...) method type hint can not be a primitive' |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (!($typeHintClass->newInstanceWithoutConstructor() instanceof $instanceOf)) { |
|
148
|
|
|
throw new \InvalidArgumentException( |
|
149
|
|
|
'Closure argument of catch(...) method type hint must be instance of ' . $instanceOf . |
|
150
|
|
|
', ' . $typeHintClass->name . ' given' |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
return $typeHintClass->name; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param array ...$args |
|
159
|
|
|
* @return mixed |
|
160
|
|
|
* @throws \Throwable |
|
161
|
|
|
*/ |
|
162
|
|
|
public function invoke(...$args) |
|
163
|
|
|
{ |
|
164
|
|
|
$result = null; |
|
165
|
|
|
$error = null; |
|
166
|
|
|
|
|
167
|
|
|
try { |
|
168
|
|
|
$result = $this->callClosure(...$args); |
|
169
|
|
|
} catch (\Throwable $e) { |
|
170
|
|
|
$error = $e; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if ($this->then) { |
|
174
|
|
|
return ($this->then)($result ?? $error); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
if ($error !== null) { |
|
178
|
|
|
throw $error; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $result; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param array ...$args |
|
186
|
|
|
* @return mixed |
|
187
|
|
|
* @throws \Throwable |
|
188
|
|
|
*/ |
|
189
|
|
|
private function callClosure(...$args) |
|
190
|
|
|
{ |
|
191
|
|
|
while ( |
|
192
|
|
|
$this->retries === static::INFINITY_RETRIES || |
|
193
|
|
|
($this->retries-- + 1) > 0 |
|
194
|
|
|
) { |
|
195
|
|
|
try { |
|
196
|
|
|
return ($this->context)(...$args); |
|
197
|
|
|
} catch (\Throwable $e) { |
|
198
|
|
|
$this->throw($e, $this->everyError); |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
if ($this->willBeThrows()) { |
|
201
|
|
|
return $this->throw($e, $this->catches, true); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return null; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return bool |
|
211
|
|
|
* @throws \Throwable |
|
212
|
|
|
*/ |
|
213
|
|
|
private function willBeThrows(): bool |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->retries !== static::INFINITY_RETRIES && $this->retries < 0; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param \Throwable $e |
|
220
|
|
|
* @param array $callbacks |
|
221
|
|
|
* @param bool $throwAfter |
|
222
|
|
|
* @return mixed |
|
223
|
|
|
* @throws \Throwable |
|
224
|
|
|
*/ |
|
225
|
|
|
private function throw(\Throwable $e, array $callbacks, bool $throwAfter = false) |
|
226
|
|
|
{ |
|
227
|
|
|
foreach ($callbacks as $name => $callback) { |
|
228
|
|
|
if ($e instanceof $name || $name === static::CATCH_ALL_EXCEPTIONS) { |
|
229
|
|
|
return $callback($e); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if ($throwAfter) { |
|
234
|
|
|
throw $e; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return null; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @param \Closure $then |
|
242
|
|
|
* @return Evacuator|$this |
|
243
|
|
|
*/ |
|
244
|
|
|
public function finally(\Closure $then): Evacuator |
|
|
|
|
|
|
245
|
|
|
{ |
|
246
|
|
|
$this->then = $then; |
|
247
|
|
|
|
|
248
|
|
|
return $this; |
|
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.