|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Adapter\Guzzle6; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception as GuzzleExceptions; |
|
6
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
|
7
|
|
|
use Http\Client\Exception as HttplugException; |
|
8
|
|
|
use Http\Promise\Promise as HttpPromise; |
|
9
|
|
|
use Psr\Http\Message\RequestInterface; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Wrapper around Guzzle promises. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Joel Wurtz <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Promise implements HttpPromise |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var PromiseInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $promise; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string State of the promise |
|
26
|
|
|
*/ |
|
27
|
|
|
private $state; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var ResponseInterface |
|
31
|
|
|
*/ |
|
32
|
|
|
private $response; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var HttplugException |
|
36
|
|
|
*/ |
|
37
|
|
|
private $exception; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var RequestInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $request; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param PromiseInterface $promise |
|
46
|
|
|
* @param RequestInterface $request |
|
47
|
|
|
*/ |
|
48
|
326 |
|
public function __construct(PromiseInterface $promise, RequestInterface $request) |
|
49
|
|
|
{ |
|
50
|
326 |
|
$this->request = $request; |
|
51
|
326 |
|
$this->state = self::PENDING; |
|
52
|
|
|
$this->promise = $promise->then(function ($response) { |
|
53
|
315 |
|
$this->response = $response; |
|
54
|
315 |
|
$this->state = self::FULFILLED; |
|
55
|
|
|
|
|
56
|
315 |
|
return $response; |
|
57
|
326 |
|
}, function ($reason) use ($request) { |
|
58
|
10 |
|
$this->state = self::REJECTED; |
|
59
|
|
|
|
|
60
|
10 |
|
if ($reason instanceof HttplugException) { |
|
61
|
6 |
|
$this->exception = $reason; |
|
62
|
10 |
|
} elseif ($reason instanceof GuzzleExceptions\GuzzleException) { |
|
63
|
9 |
|
$this->exception = $this->handleException($reason, $request); |
|
64
|
10 |
|
} elseif ($reason instanceof \Exception) { |
|
65
|
1 |
|
$this->exception = new \RuntimeException('Invalid exception returned from Guzzle6', 0, $reason); |
|
|
|
|
|
|
66
|
1 |
|
} else { |
|
67
|
|
|
$this->exception = new \UnexpectedValueException('Reason returned from Guzzle6 must be an Exception', 0, $reason); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
10 |
|
throw $this->exception; |
|
71
|
326 |
|
}); |
|
72
|
326 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritdoc} |
|
76
|
|
|
*/ |
|
77
|
165 |
|
public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
78
|
|
|
{ |
|
79
|
165 |
|
return new static($this->promise->then($onFulfilled, $onRejected), $this->request); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritdoc} |
|
84
|
|
|
*/ |
|
85
|
316 |
|
public function getState() |
|
86
|
|
|
{ |
|
87
|
316 |
|
return $this->state; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
325 |
|
public function wait($unwrap = true) |
|
94
|
|
|
{ |
|
95
|
325 |
|
$this->promise->wait(false); |
|
96
|
|
|
|
|
97
|
325 |
|
if ($unwrap) { |
|
98
|
316 |
|
if ($this->getState() == self::REJECTED) { |
|
99
|
4 |
|
throw $this->exception; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
312 |
|
return $this->response; |
|
103
|
|
|
} |
|
104
|
9 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Converts a Guzzle exception into an Httplug exception. |
|
108
|
|
|
* |
|
109
|
|
|
* @param GuzzleExceptions\GuzzleException $exception |
|
110
|
|
|
* @param RequestInterface $request |
|
111
|
|
|
* |
|
112
|
|
|
* @return HttplugException |
|
113
|
|
|
*/ |
|
114
|
10 |
|
private function handleException(GuzzleExceptions\GuzzleException $exception, RequestInterface $request) |
|
115
|
|
|
{ |
|
116
|
10 |
|
if ($exception instanceof GuzzleExceptions\SeekException) { |
|
117
|
|
|
return new HttplugException\RequestException($exception->getMessage(), $request, $exception); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
10 |
|
if ($exception instanceof GuzzleExceptions\ConnectException) { |
|
121
|
10 |
|
return new HttplugException\NetworkException($exception->getMessage(), $exception->getRequest(), $exception); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
if ($exception instanceof GuzzleExceptions\RequestException) { |
|
125
|
|
|
// Make sure we have a response for the HttpException |
|
126
|
1 |
|
if ($exception->hasResponse()) { |
|
127
|
1 |
|
return new HttplugException\HttpException( |
|
128
|
1 |
|
$exception->getMessage(), |
|
129
|
1 |
|
$exception->getRequest(), |
|
130
|
1 |
|
$exception->getResponse(), |
|
131
|
|
|
$exception |
|
132
|
1 |
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
return new HttplugException\RequestException($exception->getMessage(), $exception->getRequest(), $exception); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
1 |
|
return new HttplugException\TransferException($exception->getMessage(), 0, $exception); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..