Completed
Branch master (9c37c7)
by Jens
07:29
created

Guzzle5Promise::getHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
namespace Commercetools\Core\Client\Adapter;
7
8
use GuzzleHttp\Message\FutureResponse;
9
use GuzzleHttp\Psr7\Response;
10
use GuzzleHttp\Ring\Future\FutureInterface;
11
use Psr\Http\Message\ResponseInterface;
12
use Psr\Http\Message\StreamInterface;
13
14
class Guzzle5Promise implements AdapterPromiseInterface
15
{
16
    /**
17
     * @var FutureResponse
18
     */
19
    protected $response;
20
21
    /**
22
     * @var FutureInterface
23
     */
24
    protected $promise;
25
26
    public function __construct(FutureResponse $promise)
27
    {
28
        $this->response = $promise;
29
        $this->promise = $promise;
0 ignored issues
show
Documentation Bug introduced by
It seems like $promise of type object<GuzzleHttp\Message\FutureResponse> is incompatible with the declared type object<GuzzleHttp\Ring\Future\FutureInterface> of property $promise.

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..

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