1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Promise; |
4
|
|
|
|
5
|
|
|
use Dazzle\Throwable\Exception\Logic\InvalidArgumentException; |
6
|
|
|
use Dazzle\Throwable\Exception\Runtime\RejectionException; |
7
|
|
|
use Dazzle\Throwable\ThrowableProxy; |
8
|
|
|
use Error; |
9
|
|
|
use Exception; |
10
|
|
|
|
11
|
|
|
class PromiseRejected implements PromiseInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Error|Exception|ThrowableProxy|string|null |
15
|
|
|
*/ |
16
|
|
|
protected $reason; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Error|Exception|string|null |
20
|
|
|
* @throws InvalidArgumentException |
21
|
|
|
*/ |
22
|
179 |
|
public function __construct($reason = null) |
23
|
|
|
{ |
24
|
179 |
|
if ($reason instanceof PromiseInterface) |
25
|
|
|
{ |
26
|
1 |
|
throw new InvalidArgumentException( |
27
|
1 |
|
'You cannot create PromiseRejected with a promise. Use Promise::doReject($promiseOrValue) instead.' |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
179 |
|
$this->reason = $reason; |
32
|
179 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* |
36
|
|
|
*/ |
37
|
111 |
|
public function __destruct() |
38
|
|
|
{ |
39
|
111 |
|
unset($this->reason); |
40
|
111 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @override |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
109 |
View Code Duplication |
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
|
|
|
47
|
|
|
{ |
48
|
109 |
|
if (null === $onRejected) |
49
|
|
|
{ |
50
|
26 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
try |
54
|
|
|
{ |
55
|
106 |
|
return Promise::doResolve($onRejected($this->getReason())); |
56
|
|
|
} |
57
|
8 |
|
catch (Error $ex) |
58
|
|
|
{ |
59
|
|
|
return new PromiseRejected($ex); |
60
|
|
|
} |
61
|
8 |
|
catch (Exception $ex) |
62
|
|
|
{ |
63
|
8 |
|
return new PromiseRejected($ex); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @override |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
76 |
View Code Duplication |
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
|
|
|
|
72
|
|
|
{ |
73
|
76 |
|
if (null === $onRejected) |
74
|
|
|
{ |
75
|
22 |
|
$this->throwError($this->getReason()); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
58 |
|
$result = $onRejected($this->getReason()); |
79
|
|
|
|
80
|
51 |
|
if ($result instanceof self) |
81
|
|
|
{ |
82
|
10 |
|
$this->throwError($result->getReason()); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
41 |
|
if ($result instanceof PromiseInterface) |
86
|
|
|
{ |
87
|
2 |
|
$result->done(); |
88
|
|
|
} |
89
|
41 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @override |
93
|
|
|
* @inheritDoc |
94
|
|
|
*/ |
95
|
2 |
|
public function spread(callable $onFulfilled = null, callable $onRejected = null, callable $onCancel = null) |
96
|
|
|
{ |
97
|
2 |
|
return $this->then( |
98
|
2 |
|
null, |
99
|
|
|
function($rejections) use($onRejected) { |
100
|
2 |
|
return $onRejected(...((array) $rejections)); |
101
|
2 |
|
} |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @override |
107
|
|
|
* @inheritDoc |
108
|
|
|
*/ |
109
|
1 |
|
public function success(callable $onSuccess) |
110
|
|
|
{ |
111
|
1 |
|
return $this->then($onSuccess); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @override |
116
|
|
|
* @inheritDoc |
117
|
|
|
*/ |
118
|
1 |
|
public function failure(callable $onFailure) |
119
|
|
|
{ |
120
|
1 |
|
return $this->then(null, $onFailure); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @override |
125
|
|
|
* @inheritDoc |
126
|
|
|
*/ |
127
|
1 |
|
public function abort(callable $onCancel) |
128
|
|
|
{ |
129
|
1 |
|
return $this->then(null, null, $onCancel); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @override |
134
|
|
|
* @inheritDoc |
135
|
|
|
*/ |
136
|
5 |
View Code Duplication |
public function always(callable $onFulfilledOrRejected) |
|
|
|
|
137
|
|
|
{ |
138
|
5 |
|
return $this->then( |
139
|
5 |
|
null, |
140
|
|
|
function($reason) use($onFulfilledOrRejected) { |
141
|
4 |
|
return Promise::doResolve($onFulfilledOrRejected())->then(function() use($reason) { |
142
|
3 |
|
return new static($reason); |
143
|
4 |
|
}); |
144
|
5 |
|
} |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @override |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
1 |
|
public function isPending() |
153
|
|
|
{ |
154
|
1 |
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @override |
159
|
|
|
* @inheritDoc |
160
|
|
|
*/ |
161
|
3 |
|
public function isFulfilled() |
162
|
|
|
{ |
163
|
3 |
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @override |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
3 |
|
public function isRejected() |
171
|
|
|
{ |
172
|
3 |
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @override |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
3 |
|
public function isCancelled() |
180
|
|
|
{ |
181
|
3 |
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @override |
186
|
|
|
* @inheritDoc |
187
|
|
|
*/ |
188
|
|
|
public function getPromise() |
189
|
|
|
{ |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @override |
195
|
|
|
* @inheritDoc |
196
|
|
|
*/ |
197
|
|
|
public function resolve($value = null) |
198
|
|
|
{ |
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @override |
204
|
|
|
* @inheritDoc |
205
|
|
|
*/ |
206
|
|
|
public function reject($reason = null) |
207
|
|
|
{ |
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @override |
213
|
|
|
* @inheritDoc |
214
|
|
|
*/ |
215
|
5 |
|
public function cancel($reason = null) |
216
|
|
|
{ |
217
|
5 |
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @see Promise::getValue |
222
|
|
|
*/ |
223
|
|
|
protected function getValue() |
224
|
|
|
{ |
225
|
|
|
return null; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @see Promise::getReason |
230
|
|
|
*/ |
231
|
156 |
|
protected function getReason() |
232
|
|
|
{ |
233
|
156 |
|
return ($this->reason instanceof ThrowableProxy) ? $this->reason->toThrowable() : $this->reason; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param Error|Exception|string $reason |
238
|
|
|
* @throws Error|Exception |
239
|
|
|
*/ |
240
|
32 |
|
protected function throwError($reason) |
241
|
|
|
{ |
242
|
32 |
|
if ($reason instanceof Error || $reason instanceof Exception) |
243
|
|
|
{ |
244
|
15 |
|
throw $reason; |
245
|
|
|
} |
246
|
|
|
|
247
|
17 |
|
throw new RejectionException($reason); |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
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.