1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Client\Adapter; |
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Promise\PromiseInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\StreamInterface; |
11
|
|
|
|
12
|
|
|
class Guzzle6Promise implements AdapterPromiseInterface, PromiseGetInterface |
13
|
|
|
{ |
14
|
|
|
protected $response; |
15
|
|
|
protected $promise; |
16
|
|
|
|
17
|
6 |
|
public function __construct(PromiseInterface $promise) |
18
|
|
|
{ |
19
|
6 |
|
$this->response = $promise; |
20
|
6 |
|
$this->promise = $promise; |
21
|
6 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return PromiseInterface |
25
|
|
|
*/ |
26
|
1 |
|
public function getPromise() |
27
|
|
|
{ |
28
|
1 |
|
return $this->promise; |
29
|
|
|
} |
30
|
|
|
|
31
|
5 |
|
public function then(callable $onFulfilled = null, callable $onRejected = null) |
32
|
|
|
{ |
33
|
5 |
|
$this->promise = $this->promise->then($onFulfilled, $onRejected); |
34
|
|
|
|
35
|
5 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return ResponseInterface |
40
|
|
|
*/ |
41
|
5 |
|
public function wait() |
42
|
|
|
{ |
43
|
5 |
|
return $this->response->wait(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Retrieves the HTTP protocol version as a string. |
48
|
|
|
* |
49
|
|
|
* The string MUST contain only the HTTP version number (e.g., "1.1", "1.0"). |
50
|
|
|
* |
51
|
|
|
* @return string HTTP protocol version. |
52
|
|
|
*/ |
53
|
|
|
public function getProtocolVersion() |
54
|
|
|
{ |
55
|
|
|
return $this->wait()->getProtocolVersion(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return an instance with the specified HTTP protocol version. |
60
|
|
|
* |
61
|
|
|
* The version string MUST contain only the HTTP version number (e.g., |
62
|
|
|
* "1.1", "1.0"). |
63
|
|
|
* |
64
|
|
|
* This method MUST be implemented in such a way as to retain the |
65
|
|
|
* immutability of the message, and MUST return an instance that has the |
66
|
|
|
* new protocol version. |
67
|
|
|
* |
68
|
|
|
* @param string $version HTTP protocol version |
69
|
|
|
* @return self |
70
|
|
|
*/ |
71
|
|
|
public function withProtocolVersion($version) |
72
|
|
|
{ |
73
|
|
|
return $this->wait()->withProtocolVersion($version); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retrieves all message header values. |
78
|
|
|
* |
79
|
|
|
* The keys represent the header name as it will be sent over the wire, and |
80
|
|
|
* each value is an array of strings associated with the header. |
81
|
|
|
* |
82
|
|
|
* // Represent the headers as a string |
83
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
84
|
|
|
* echo $name . ": " . implode(", ", $values); |
85
|
|
|
* } |
86
|
|
|
* |
87
|
|
|
* // Emit headers iteratively: |
88
|
|
|
* foreach ($message->getHeaders() as $name => $values) { |
89
|
|
|
* foreach ($values as $value) { |
90
|
|
|
* header(sprintf('%s: %s', $name, $value), false); |
91
|
|
|
* } |
92
|
|
|
* } |
93
|
|
|
* |
94
|
|
|
* While header names are not case-sensitive, getHeaders() will preserve the |
95
|
|
|
* exact case in which headers were originally specified. |
96
|
|
|
* |
97
|
|
|
* @return array Returns an associative array of the message's headers. Each |
98
|
|
|
* key MUST be a header name, and each value MUST be an array of strings |
99
|
|
|
* for that header. |
100
|
|
|
*/ |
101
|
|
|
public function getHeaders() |
102
|
|
|
{ |
103
|
|
|
return $this->wait()->getHeaders(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Checks if a header exists by the given case-insensitive name. |
108
|
|
|
* |
109
|
|
|
* @param string $name Case-insensitive header field name. |
110
|
|
|
* @return bool Returns true if any header names match the given header |
111
|
|
|
* name using a case-insensitive string comparison. Returns false if |
112
|
|
|
* no matching header name is found in the message. |
113
|
|
|
*/ |
114
|
|
|
public function hasHeader($name) |
115
|
|
|
{ |
116
|
|
|
return $this->wait()->hasHeader($name); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retrieves a message header value by the given case-insensitive name. |
121
|
|
|
* |
122
|
|
|
* This method returns an array of all the header values of the given |
123
|
|
|
* case-insensitive header name. |
124
|
|
|
* |
125
|
|
|
* If the header does not appear in the message, this method MUST return an |
126
|
|
|
* empty array. |
127
|
|
|
* |
128
|
|
|
* @param string $name Case-insensitive header field name. |
129
|
|
|
* @return string[] An array of string values as provided for the given |
130
|
|
|
* header. If the header does not appear in the message, this method MUST |
131
|
|
|
* return an empty array. |
132
|
|
|
*/ |
133
|
|
|
public function getHeader($name) |
134
|
|
|
{ |
135
|
|
|
return $this->wait()->getHeader($name); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Retrieves a comma-separated string of the values for a single header. |
140
|
|
|
* |
141
|
|
|
* This method returns all of the header values of the given |
142
|
|
|
* case-insensitive header name as a string concatenated together using |
143
|
|
|
* a comma. |
144
|
|
|
* |
145
|
|
|
* NOTE: Not all header values may be appropriately represented using |
146
|
|
|
* comma concatenation. For such headers, use getHeader() instead |
147
|
|
|
* and supply your own delimiter when concatenating. |
148
|
|
|
* |
149
|
|
|
* If the header does not appear in the message, this method MUST return |
150
|
|
|
* an empty string. |
151
|
|
|
* |
152
|
|
|
* @param string $name Case-insensitive header field name. |
153
|
|
|
* @return string A string of values as provided for the given header |
154
|
|
|
* concatenated together using a comma. If the header does not appear in |
155
|
|
|
* the message, this method MUST return an empty string. |
156
|
|
|
*/ |
157
|
|
|
public function getHeaderLine($name) |
158
|
|
|
{ |
159
|
|
|
return $this->wait()->getHeaderLine($name); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Return an instance with the provided value replacing the specified header. |
164
|
|
|
* |
165
|
|
|
* While header names are case-insensitive, the casing of the header will |
166
|
|
|
* be preserved by this function, and returned from getHeaders(). |
167
|
|
|
* |
168
|
|
|
* This method MUST be implemented in such a way as to retain the |
169
|
|
|
* immutability of the message, and MUST return an instance that has the |
170
|
|
|
* new and/or updated header and value. |
171
|
|
|
* |
172
|
|
|
* @param string $name Case-insensitive header field name. |
173
|
|
|
* @param string|string[] $value Header value(s). |
174
|
|
|
* @return self |
175
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
176
|
|
|
*/ |
177
|
|
|
public function withHeader($name, $value) |
178
|
|
|
{ |
179
|
|
|
return $this->wait()->withHeader($name, $value); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Return an instance with the specified header appended with the given value. |
184
|
|
|
* |
185
|
|
|
* Existing values for the specified header will be maintained. The new |
186
|
|
|
* value(s) will be appended to the existing list. If the header did not |
187
|
|
|
* exist previously, it will be added. |
188
|
|
|
* |
189
|
|
|
* This method MUST be implemented in such a way as to retain the |
190
|
|
|
* immutability of the message, and MUST return an instance that has the |
191
|
|
|
* new header and/or value. |
192
|
|
|
* |
193
|
|
|
* @param string $name Case-insensitive header field name to add. |
194
|
|
|
* @param string|string[] $value Header value(s). |
195
|
|
|
* @return self |
196
|
|
|
* @throws \InvalidArgumentException for invalid header names or values. |
197
|
|
|
*/ |
198
|
|
|
public function withAddedHeader($name, $value) |
199
|
|
|
{ |
200
|
|
|
return $this->wait()->withAddedHeader($name, $value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Return an instance without the specified header. |
205
|
|
|
* |
206
|
|
|
* Header resolution MUST be done without case-sensitivity. |
207
|
|
|
* |
208
|
|
|
* This method MUST be implemented in such a way as to retain the |
209
|
|
|
* immutability of the message, and MUST return an instance that removes |
210
|
|
|
* the named header. |
211
|
|
|
* |
212
|
|
|
* @param string $name Case-insensitive header field name to remove. |
213
|
|
|
* @return self |
214
|
|
|
*/ |
215
|
|
|
public function withoutHeader($name) |
216
|
|
|
{ |
217
|
|
|
return $this->wait()->withoutHeader($name); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Gets the body of the message. |
222
|
|
|
* |
223
|
|
|
* @return StreamInterface Returns the body as a stream. |
224
|
|
|
*/ |
225
|
1 |
|
public function getBody() |
226
|
|
|
{ |
227
|
1 |
|
return $this->wait()->getBody(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Return an instance with the specified message body. |
232
|
|
|
* |
233
|
|
|
* The body MUST be a StreamInterface object. |
234
|
|
|
* |
235
|
|
|
* This method MUST be implemented in such a way as to retain the |
236
|
|
|
* immutability of the message, and MUST return a new instance that has the |
237
|
|
|
* new body stream. |
238
|
|
|
* |
239
|
|
|
* @param StreamInterface $body Body. |
240
|
|
|
* @return self |
241
|
|
|
* @throws \InvalidArgumentException When the body is not valid. |
242
|
|
|
*/ |
243
|
|
|
public function withBody(StreamInterface $body) |
244
|
|
|
{ |
245
|
|
|
return $this->wait()->withBody($body); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Gets the response status code. |
250
|
|
|
* |
251
|
|
|
* The status code is a 3-digit integer result code of the server's attempt |
252
|
|
|
* to understand and satisfy the request. |
253
|
|
|
* |
254
|
|
|
* @return int Status code. |
255
|
|
|
*/ |
256
|
4 |
|
public function getStatusCode() |
257
|
|
|
{ |
258
|
4 |
|
return $this->wait()->getStatusCode(); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Return an instance with the specified status code and, optionally, reason phrase. |
263
|
|
|
* |
264
|
|
|
* If no reason phrase is specified, implementations MAY choose to default |
265
|
|
|
* to the RFC 7231 or IANA recommended reason phrase for the response's |
266
|
|
|
* status code. |
267
|
|
|
* |
268
|
|
|
* This method MUST be implemented in such a way as to retain the |
269
|
|
|
* immutability of the message, and MUST return an instance that has the |
270
|
|
|
* updated status and reason phrase. |
271
|
|
|
* |
272
|
|
|
* @link http://tools.ietf.org/html/rfc7231#section-6 |
273
|
|
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
274
|
|
|
* @param int $code The 3-digit integer result code to set. |
275
|
|
|
* @param string $reasonPhrase The reason phrase to use with the |
276
|
|
|
* provided status code; if none is provided, implementations MAY |
277
|
|
|
* use the defaults as suggested in the HTTP specification. |
278
|
|
|
* @return self |
279
|
|
|
* @throws \InvalidArgumentException For invalid status code arguments. |
280
|
|
|
*/ |
281
|
|
|
public function withStatus($code, $reasonPhrase = '') |
282
|
|
|
{ |
283
|
|
|
return $this->wait()->withStatus($code, $reasonPhrase); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Gets the response reason phrase associated with the status code. |
288
|
|
|
* |
289
|
|
|
* Because a reason phrase is not a required element in a response |
290
|
|
|
* status line, the reason phrase value MAY be null. Implementations MAY |
291
|
|
|
* choose to return the default RFC 7231 recommended reason phrase (or those |
292
|
|
|
* listed in the IANA HTTP Status Code Registry) for the response's |
293
|
|
|
* status code. |
294
|
|
|
* |
295
|
|
|
* @link http://tools.ietf.org/html/rfc7231#section-6 |
296
|
|
|
* @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml |
297
|
|
|
* @return string Reason phrase; must return an empty string if none present. |
298
|
|
|
*/ |
299
|
|
|
public function getReasonPhrase() |
300
|
|
|
{ |
301
|
|
|
return $this->wait()->getReasonPhrase(); |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|