Passed
Push — master ( da0795...78880b )
by Jens
14:45 queued 18s
created

Guzzle5Promise::getPromise()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * @author @jenschude <[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;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Ring\Future\FutureInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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