44
55namespace Http \Client \Common ;
66
7- use Http \Client \Exception ;
87use Http \Promise \Promise ;
8+ use Psr \Http \Client \ClientExceptionInterface ;
99use Psr \Http \Message \ResponseInterface ;
1010
1111/**
1212 * A deferred allow to return a promise which has not been resolved yet.
1313 */
1414final class Deferred implements Promise
1515{
16+ /**
17+ * @var ResponseInterface|null
18+ */
1619 private $ value ;
1720
21+ /**
22+ * @var ClientExceptionInterface|null
23+ */
1824 private $ failure ;
1925
26+ /**
27+ * @var string
28+ */
2029 private $ state ;
2130
31+ /**
32+ * @var callable
33+ */
2234 private $ waitCallback ;
2335
36+ /**
37+ * @var callable[]
38+ */
2439 private $ onFulfilledCallbacks ;
2540
41+ /**
42+ * @var callable[]
43+ */
2644 private $ onRejectedCallbacks ;
2745
2846 public function __construct (callable $ waitCallback )
@@ -46,12 +64,12 @@ public function then(callable $onFulfilled = null, callable $onRejected = null):
4664 $ response = $ onFulfilled ($ response );
4765 }
4866 $ deferred ->resolve ($ response );
49- } catch (Exception $ exception ) {
67+ } catch (ClientExceptionInterface $ exception ) {
5068 $ deferred ->reject ($ exception );
5169 }
5270 };
5371
54- $ this ->onRejectedCallbacks [] = function (Exception $ exception ) use ($ onRejected , $ deferred ) {
72+ $ this ->onRejectedCallbacks [] = function (ClientExceptionInterface $ exception ) use ($ onRejected , $ deferred ) {
5573 try {
5674 if (null !== $ onRejected ) {
5775 $ response = $ onRejected ($ exception );
@@ -60,7 +78,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null):
6078 return ;
6179 }
6280 $ deferred ->reject ($ exception );
63- } catch (Exception $ newException ) {
81+ } catch (ClientExceptionInterface $ newException ) {
6482 $ deferred ->reject ($ newException );
6583 }
6684 };
@@ -81,12 +99,12 @@ public function getState(): string
8199 */
82100 public function resolve (ResponseInterface $ response ): void
83101 {
84- if (self ::PENDING !== $ this ->state ) {
102+ if (Promise ::PENDING !== $ this ->state ) {
85103 return ;
86104 }
87105
88106 $ this ->value = $ response ;
89- $ this ->state = self ::FULFILLED ;
107+ $ this ->state = Promise ::FULFILLED ;
90108
91109 foreach ($ this ->onFulfilledCallbacks as $ onFulfilledCallback ) {
92110 $ onFulfilledCallback ($ response );
@@ -96,14 +114,14 @@ public function resolve(ResponseInterface $response): void
96114 /**
97115 * Reject this deferred with an Exception.
98116 */
99- public function reject (Exception $ exception ): void
117+ public function reject (ClientExceptionInterface $ exception ): void
100118 {
101- if (self ::PENDING !== $ this ->state ) {
119+ if (Promise ::PENDING !== $ this ->state ) {
102120 return ;
103121 }
104122
105123 $ this ->failure = $ exception ;
106- $ this ->state = self ::REJECTED ;
124+ $ this ->state = Promise ::REJECTED ;
107125
108126 foreach ($ this ->onRejectedCallbacks as $ onRejectedCallback ) {
109127 $ onRejectedCallback ($ exception );
@@ -115,16 +133,16 @@ public function reject(Exception $exception): void
115133 */
116134 public function wait ($ unwrap = true )
117135 {
118- if (self ::PENDING === $ this ->state ) {
136+ if (Promise ::PENDING === $ this ->state ) {
119137 $ callback = $ this ->waitCallback ;
120138 $ callback ();
121139 }
122140
123141 if (!$ unwrap ) {
124- return ;
142+ return null ;
125143 }
126144
127- if (self ::FULFILLED === $ this ->state ) {
145+ if (Promise ::FULFILLED === $ this ->state ) {
128146 return $ this ->value ;
129147 }
130148
0 commit comments