4
4
5
5
namespace Http \Client \Common ;
6
6
7
- use Http \Client \Exception ;
8
7
use Http \Promise \Promise ;
8
+ use Psr \Http \Client \ClientExceptionInterface ;
9
9
use Psr \Http \Message \ResponseInterface ;
10
10
11
11
/**
12
12
* A deferred allow to return a promise which has not been resolved yet.
13
13
*/
14
14
final class Deferred implements Promise
15
15
{
16
+ /**
17
+ * @var ResponseInterface|null
18
+ */
16
19
private $ value ;
17
20
21
+ /**
22
+ * @var ClientExceptionInterface|null
23
+ */
18
24
private $ failure ;
19
25
26
+ /**
27
+ * @var string
28
+ */
20
29
private $ state ;
21
30
31
+ /**
32
+ * @var callable
33
+ */
22
34
private $ waitCallback ;
23
35
36
+ /**
37
+ * @var callable[]
38
+ */
24
39
private $ onFulfilledCallbacks ;
25
40
41
+ /**
42
+ * @var callable[]
43
+ */
26
44
private $ onRejectedCallbacks ;
27
45
28
46
public function __construct (callable $ waitCallback )
@@ -46,12 +64,12 @@ public function then(callable $onFulfilled = null, callable $onRejected = null):
46
64
$ response = $ onFulfilled ($ response );
47
65
}
48
66
$ deferred ->resolve ($ response );
49
- } catch (Exception $ exception ) {
67
+ } catch (ClientExceptionInterface $ exception ) {
50
68
$ deferred ->reject ($ exception );
51
69
}
52
70
};
53
71
54
- $ this ->onRejectedCallbacks [] = function (Exception $ exception ) use ($ onRejected , $ deferred ) {
72
+ $ this ->onRejectedCallbacks [] = function (ClientExceptionInterface $ exception ) use ($ onRejected , $ deferred ) {
55
73
try {
56
74
if (null !== $ onRejected ) {
57
75
$ response = $ onRejected ($ exception );
@@ -60,7 +78,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null):
60
78
return ;
61
79
}
62
80
$ deferred ->reject ($ exception );
63
- } catch (Exception $ newException ) {
81
+ } catch (ClientExceptionInterface $ newException ) {
64
82
$ deferred ->reject ($ newException );
65
83
}
66
84
};
@@ -81,12 +99,12 @@ public function getState(): string
81
99
*/
82
100
public function resolve (ResponseInterface $ response ): void
83
101
{
84
- if (self ::PENDING !== $ this ->state ) {
102
+ if (Promise ::PENDING !== $ this ->state ) {
85
103
return ;
86
104
}
87
105
88
106
$ this ->value = $ response ;
89
- $ this ->state = self ::FULFILLED ;
107
+ $ this ->state = Promise ::FULFILLED ;
90
108
91
109
foreach ($ this ->onFulfilledCallbacks as $ onFulfilledCallback ) {
92
110
$ onFulfilledCallback ($ response );
@@ -96,14 +114,14 @@ public function resolve(ResponseInterface $response): void
96
114
/**
97
115
* Reject this deferred with an Exception.
98
116
*/
99
- public function reject (Exception $ exception ): void
117
+ public function reject (ClientExceptionInterface $ exception ): void
100
118
{
101
- if (self ::PENDING !== $ this ->state ) {
119
+ if (Promise ::PENDING !== $ this ->state ) {
102
120
return ;
103
121
}
104
122
105
123
$ this ->failure = $ exception ;
106
- $ this ->state = self ::REJECTED ;
124
+ $ this ->state = Promise ::REJECTED ;
107
125
108
126
foreach ($ this ->onRejectedCallbacks as $ onRejectedCallback ) {
109
127
$ onRejectedCallback ($ exception );
@@ -115,16 +133,16 @@ public function reject(Exception $exception): void
115
133
*/
116
134
public function wait ($ unwrap = true )
117
135
{
118
- if (self ::PENDING === $ this ->state ) {
136
+ if (Promise ::PENDING === $ this ->state ) {
119
137
$ callback = $ this ->waitCallback ;
120
138
$ callback ();
121
139
}
122
140
123
141
if (!$ unwrap ) {
124
- return ;
142
+ return null ;
125
143
}
126
144
127
- if (self ::FULFILLED === $ this ->state ) {
145
+ if (Promise ::FULFILLED === $ this ->state ) {
128
146
return $ this ->value ;
129
147
}
130
148
0 commit comments