1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thruster\Component\Promise; |
4
|
|
|
|
5
|
|
|
use Thruster\Component\Promise\Exception\UnhandledRejectionException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class RejectedPromise |
9
|
|
|
* |
10
|
|
|
* @package Thruster\Component\Promise |
11
|
|
|
* @author Aurimas Niekis <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class RejectedPromise implements ExtendedPromiseInterface, CancellablePromiseInterface |
14
|
|
|
{ |
15
|
|
|
private $reason; |
16
|
|
|
|
17
|
231 |
|
public function __construct($reason = null) |
18
|
|
|
{ |
19
|
231 |
|
if ($reason instanceof PromiseInterface) { |
20
|
1 |
|
throw new \InvalidArgumentException( |
21
|
|
|
'You cannot create Thruster\Component\Promise\RejectedPromise with a promise.' . |
22
|
1 |
|
' Use Thruster\Component\Promise\reject($promiseOrValue) instead.' |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
231 |
|
$this->reason = $reason; |
27
|
231 |
|
} |
28
|
|
|
|
29
|
146 |
View Code Duplication |
public function then( |
|
|
|
|
30
|
|
|
callable $onFulfilled = null, |
31
|
|
|
callable $onRejected = null, |
32
|
|
|
callable $onProgress = null |
33
|
|
|
) : PromiseInterface { |
34
|
146 |
|
if (null === $onRejected) { |
35
|
27 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
try { |
39
|
144 |
|
return resolve($onRejected($this->reason)); |
40
|
11 |
|
} catch (\Exception $exception) { |
41
|
11 |
|
return new RejectedPromise($exception); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
101 |
|
public function done(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) |
46
|
|
|
{ |
47
|
101 |
|
if (null === $onRejected) { |
48
|
31 |
|
throw UnhandledRejectionException::resolve($this->reason); |
49
|
|
|
} |
50
|
|
|
|
51
|
76 |
|
$result = $onRejected($this->reason); |
52
|
|
|
|
53
|
66 |
|
if ($result instanceof self) { |
54
|
14 |
|
throw UnhandledRejectionException::resolve($result->reason); |
55
|
|
|
} |
56
|
|
|
|
57
|
52 |
|
if ($result instanceof ExtendedPromiseInterface) { |
58
|
3 |
|
$result->done(); |
59
|
|
|
} |
60
|
52 |
|
} |
61
|
|
|
|
62
|
4 |
|
public function otherwise(callable $onRejected) : ExtendedPromiseInterface |
63
|
|
|
{ |
64
|
4 |
|
if (!_checkTypehint($onRejected, $this->reason)) { |
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return $this->then(null, $onRejected); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function always(callable $onFulfilledOrRejected) : ExtendedPromiseInterface |
72
|
|
|
{ |
73
|
|
|
return $this->then(null, function ($reason) use ($onFulfilledOrRejected) { |
74
|
5 |
|
return resolve($onFulfilledOrRejected())->then(function () use ($reason) { |
75
|
4 |
|
return new RejectedPromise($reason); |
76
|
5 |
|
}); |
77
|
6 |
|
}); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function progress(callable $onProgress) : ExtendedPromiseInterface |
81
|
|
|
{ |
82
|
1 |
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
public function cancel() |
86
|
|
|
{ |
87
|
4 |
|
} |
88
|
|
|
} |
89
|
|
|
|
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.