1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Adapter\Artax\Internal; |
4
|
|
|
|
5
|
|
|
use Amp\Promise as AmpPromise; |
6
|
|
|
use Http\Client\Exception; |
7
|
|
|
use Http\Promise\Promise as HttpPromise; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Promise adapter between artax and php-http, which allow to use the sendAsyncRequest and also the coroutine system by still respecting the Amp Promise. |
12
|
|
|
* |
13
|
|
|
* @internal |
14
|
|
|
*/ |
15
|
|
|
class Promise implements HttpPromise, AmpPromise |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
private $state = HttpPromise::PENDING; |
19
|
|
|
|
20
|
|
|
/** @var ResponseInterface */ |
21
|
|
|
private $response; |
22
|
|
|
|
23
|
|
|
/** @var Exception */ |
24
|
|
|
private $exception; |
25
|
|
|
|
26
|
|
|
/** @var AmpPromise */ |
27
|
|
|
private $promise; |
28
|
|
|
|
29
|
|
|
/** @var callable|null */ |
30
|
|
|
private $onFulfilled; |
31
|
|
|
|
32
|
|
|
/** @var callable|null */ |
33
|
|
|
private $onRejected; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param AmpPromise $promise Underlying amp promise which MUST resolve with a ResponseInterface or MUST fail with a Http\Client\Exception |
37
|
|
|
*/ |
38
|
|
|
public function __construct(AmpPromise $promise) |
39
|
|
|
{ |
40
|
|
|
$this->promise = $promise; |
41
|
|
|
$this->promise->onResolve(function ($error, $result) { |
42
|
|
|
if (null !== $error) { |
43
|
|
|
if (!$error instanceof Exception) { |
44
|
|
|
$error = new Exception\TransferException($error->getMessage(), 0, $error); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->reject($error); |
48
|
|
|
} else { |
49
|
|
|
if (!$result instanceof ResponseInterface) { |
50
|
|
|
$this->reject(new Exception\TransferException('Bad reponse returned')); |
51
|
|
|
} else { |
52
|
|
|
$this->resolve($result); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function then(callable $onFulfilled = null, callable $onRejected = null) |
62
|
|
|
{ |
63
|
|
|
$deferred = new \Amp\Deferred(); |
64
|
|
|
$newPromise = new self($deferred->promise()); |
65
|
|
|
|
66
|
|
|
$onFulfilled = $onFulfilled ?? function (ResponseInterface $response) { |
67
|
|
|
return $response; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$onRejected = $onRejected ?? function (\Throwable $exception) { |
71
|
|
|
if (!$exception instanceof Exception) { |
72
|
|
|
$exception = new Exception\TransferException($exception->getMessage(), 0, $exception); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw $exception; |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
View Code Duplication |
$this->onFulfilled = function (ResponseInterface $response) use ($onFulfilled, $deferred) { |
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$deferred->resolve($onFulfilled($response)); |
81
|
|
|
} catch (Exception $exception) { |
82
|
|
|
$deferred->fail($exception); |
|
|
|
|
83
|
|
|
} catch (\Throwable $error) { |
84
|
|
|
$deferred->fail(new Exception\TransferException($error->getMessage(), 0, $error)); |
85
|
|
|
} |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
View Code Duplication |
$this->onRejected = function (Exception $exception) use ($onRejected, $deferred) { |
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$deferred->resolve($onRejected($exception)); |
91
|
|
|
} catch (Exception $exception) { |
92
|
|
|
$deferred->fail($exception); |
|
|
|
|
93
|
|
|
} catch (\Throwable $error) { |
94
|
|
|
$deferred->fail(new Exception\TransferException($error->getMessage(), 0, $error)); |
95
|
|
|
} |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
if (HttpPromise::FULFILLED === $this->state) { |
99
|
|
|
$this->resolve($this->response); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (HttpPromise::REJECTED === $this->state) { |
103
|
|
|
$this->reject($this->exception); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $newPromise; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getState() |
113
|
|
|
{ |
114
|
|
|
return $this->state; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function wait($unwrap = true) |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
AmpPromise\wait($this->promise); |
124
|
|
|
} catch (Exception $exception) { |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($unwrap) { |
128
|
|
|
if (HttpPromise::REJECTED === $this->getState()) { |
129
|
|
|
throw $this->exception; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->response; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritdoc} |
138
|
|
|
*/ |
139
|
|
|
public function onResolve(callable $onResolved) |
140
|
|
|
{ |
141
|
|
|
$this->promise->onResolve($onResolved); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
private function resolve(ResponseInterface $response) |
145
|
|
|
{ |
146
|
|
|
$this->state = HttpPromise::FULFILLED; |
147
|
|
|
$this->response = $response; |
148
|
|
|
$onFulfilled = $this->onFulfilled; |
149
|
|
|
|
150
|
|
|
if (null !== $onFulfilled) { |
151
|
|
|
$onFulfilled($response); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function reject(Exception $exception) |
156
|
|
|
{ |
157
|
|
|
$this->state = HttpPromise::REJECTED; |
158
|
|
|
$this->exception = $exception; |
159
|
|
|
$onRejected = $this->onRejected; |
160
|
|
|
|
161
|
|
|
if (null !== $onRejected) { |
162
|
|
|
$onRejected($exception); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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.