5
5
use React \EventLoop \LoopInterface ;
6
6
use React \Promise \PromiseInterface as ReactPromise ;
7
7
use Http \Client \Exception ;
8
- use Http \Promise \Promise ;
8
+ use Http \Promise \Promise as HttpPromise ;
9
9
use Psr \Http \Message \ResponseInterface ;
10
10
11
11
/**
12
- * React promise adapter implementation
12
+ * React promise adapter implementation.
13
+ *
13
14
* @author Stéphane Hulard <stephane@hlrd.me>
14
15
*/
15
- class ReactPromiseAdapter implements Promise
16
+ class Promise implements HttpPromise
16
17
{
17
18
/**
18
- * Promise status
19
+ * Promise status.
20
+ *
19
21
* @var string
20
22
*/
21
- private $ state = Promise ::PENDING ;
23
+ private $ state = HttpPromise ::PENDING ;
22
24
23
25
/**
24
- * Adapted React promise
26
+ * Adapted React promise.
27
+ *
25
28
* @var ReactPromise
26
29
*/
27
30
private $ promise ;
28
31
29
32
/**
30
- * PSR7 received response
33
+ * PSR7 received response.
34
+ *
31
35
* @var ResponseInterface
32
36
*/
33
37
private $ response ;
34
38
35
39
/**
36
- * Execution error
40
+ * Execution error.
41
+ *
37
42
* @var Exception
38
43
*/
39
44
private $ exception ;
40
45
41
46
/**
42
- * React Event Loop used for synchronous processing
47
+ * React Event Loop used for synchronous processing.
48
+ *
43
49
* @var LoopInterface
44
50
*/
45
51
private $ loop ;
46
52
47
53
/**
48
- * Initialize the promise
54
+ * Initialize the promise.
55
+ *
49
56
* @param ReactPromise $promise
50
57
*/
51
58
public function __construct (ReactPromise $ promise )
52
59
{
53
60
$ promise ->then (
54
61
function (ResponseInterface $ response ) {
55
- $ this ->state = Promise ::FULFILLED ;
62
+ $ this ->state = HttpPromise ::FULFILLED ;
56
63
$ this ->response = $ response ;
57
64
},
58
65
function (Exception $ error ) {
59
- $ this ->state = Promise ::REJECTED ;
66
+ $ this ->state = HttpPromise ::REJECTED ;
60
67
$ this ->exception = $ error ;
61
68
}
62
69
);
63
70
$ this ->promise = $ promise ;
64
71
}
65
72
66
73
/**
67
- * Allow to apply callable when the promise resolve
68
- * @param callable|null $onFulfilled
69
- * @param callable|null $onRejected
74
+ * Allow to apply callable when the promise resolve.
75
+ *
76
+ * @param callable|null $onFulfilled
77
+ * @param callable|null $onRejected
78
+ *
70
79
* @return ReactPromiseAdapter
71
80
*/
72
81
public function then (callable $ onFulfilled = null , callable $ onRejected = null )
@@ -80,6 +89,7 @@ public function then(callable $onFulfilled = null, callable $onRejected = null)
80
89
call_user_func ($ onRejected , $ this ->exception );
81
90
}
82
91
});
92
+
83
93
return $ this ;
84
94
}
85
95
@@ -92,13 +102,16 @@ public function getState()
92
102
}
93
103
94
104
/**
95
- * Set EventLoop used for synchronous processing
105
+ * Set EventLoop used for synchronous processing.
106
+ *
96
107
* @param LoopInterface $loop
108
+ *
97
109
* @return ReactPromiseAdapter
98
110
*/
99
111
public function setLoop (LoopInterface $ loop )
100
112
{
101
113
$ this ->loop = $ loop ;
114
+
102
115
return $ this ;
103
116
}
104
117
@@ -108,14 +121,14 @@ public function setLoop(LoopInterface $loop)
108
121
public function wait ($ unwrap = true )
109
122
{
110
123
if (null === $ this ->loop ) {
111
- throw new \LogicException (" You must set the loop before wait! " );
124
+ throw new \LogicException (' You must set the loop before wait! ' );
112
125
}
113
- while (Promise ::PENDING === $ this ->getState ()) {
126
+ while (HttpPromise ::PENDING === $ this ->getState ()) {
114
127
$ this ->loop ->tick ();
115
128
}
116
129
117
130
if ($ unwrap ) {
118
- if (Promise ::REJECTED == $ this ->getState ()) {
131
+ if (HttpPromise ::REJECTED == $ this ->getState ()) {
119
132
throw $ this ->exception ;
120
133
}
121
134
0 commit comments