Passed
Pull Request — master (#23)
by Anton
14:41 queued 08:23
created

Promise::then()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Service\AkeneoPim\Dependencies\External\Api\Adapter\HttpClient;
9
10
use GuzzleHttp\Exception\ConnectException;
11
use GuzzleHttp\Exception\GuzzleException;
12
use GuzzleHttp\Exception\RequestException;
13
use GuzzleHttp\Promise\PromiseInterface;
14
use GuzzleHttp\Stream\Exception\SeekException;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Stream\Exception\SeekException 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...
15
use Http\Client\Exception as HttpExceptionInterface;
16
use Http\Client\Exception\HttpException;
17
use Http\Client\Exception\NetworkException as HttpNetworkException;
18
use Http\Client\Exception\RequestException as HttpRequestException;
19
use Http\Client\Exception\TransferException as HttpTransferException;
20
use Http\Promise\Promise as HttpPromise;
21
use Psr\Http\Message\RequestInterface;
22
use Psr\Http\Message\ResponseInterface;
23
use SprykerEco\Service\AkeneoPim\Dependencies\External\Api\Adapter\HttpClient\Exception\UnexpectedValueException;
24
use Throwable;
25
26
class Promise implements HttpPromise
27
{
28
    /**
29
     * @var \GuzzleHttp\Promise\PromiseInterface
30
     */
31
    protected $promise;
32
33
    /**
34
     * @var string
35
     */
36
    protected $state;
37
38
    /**
39
     * @var \Psr\Http\Message\ResponseInterface
40
     */
41
    protected $response;
42
43
    /**
44
     * @var \Http\Client\Exception
45
     */
46
    protected $exception;
47
48
    /**
49
     * @var \Psr\Http\Message\RequestInterface
50
     */
51
    protected $request;
52
53
    /**
54
     * @param \GuzzleHttp\Promise\PromiseInterface $promise
55
     * @param \Psr\Http\Message\RequestInterface $request
56
     */
57
    public function __construct(PromiseInterface $promise, RequestInterface $request)
58
    {
59
        $this->request = $request;
60
        $this->state = static::PENDING;
61
        $this->promise = $promise->then(function ($response) {
62
            return $this->handleResponse($response);
63
        }, function ($reason) use ($request) {
64
            $this->exception = $this->handleRejectCallback($reason, $request);
65
66
            throw $this->exception;
67
        });
68
    }
69
70
    /**
71
     * @param callable|null $onFulfilled
72
     * @param callable|null $onRejected
73
     *
74
     * @return \Http\Promise\Promise
75
     */
76
    public function then(?callable $onFulfilled = null, ?callable $onRejected = null): HttpPromise
77
    {
78
        return new static($this->promise->then($onFulfilled, $onRejected), $this->request);
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getState(): string
85
    {
86
        return $this->state;
87
    }
88
89
    /**
90
     * @param bool $unwrap
91
     *
92
     * @throws \Http\Client\Exception
93
     *
94
     * @return \Psr\Http\Message\ResponseInterface|null
95
     */
96
    public function wait($unwrap = true): ?ResponseInterface
97
    {
98
        $this->promise->wait(false);
99
100
        if (!$unwrap) {
101
            return null;
102
        }
103
104
        if ($this->getState() == static::REJECTED && $this->exception !== null) {
105
            throw $this->exception;
106
        }
107
108
        return $this->response;
109
    }
110
111
    /**
112
     * @param \Psr\Http\Message\ResponseInterface $response
113
     *
114
     * @return \Psr\Http\Message\ResponseInterface
115
     */
116
    protected function handleResponse(ResponseInterface $response): ResponseInterface
117
    {
118
        $this->response = $response;
119
        $this->state = static::FULFILLED;
120
121
        return $response;
122
    }
123
124
    /**
125
     * @param callable $rejectCallback
126
     * @param \Psr\Http\Message\RequestInterface $request
127
     *
128
     * @return \Http\Client\Exception
129
     */
130
    protected function handleRejectCallback(callable $rejectCallback, RequestInterface $request): HttpExceptionInterface
131
    {
132
        if ($rejectCallback instanceof HttpExceptionInterface) {
133
            return $rejectCallback;
134
        }
135
136
        if ($rejectCallback instanceof GuzzleException) {
137
            return $this->handleGuzzleException($rejectCallback, $request);
138
        }
139
140
        if ($rejectCallback instanceof Throwable) {
141
            return new HttpTransferException('Invalid exception returned from Guzzle', 0, $rejectCallback);
142
        }
143
144
        return new UnexpectedValueException('Reason returned from Guzzle must be an Exception');
145
    }
146
147
    /**
148
     * @param \GuzzleHttp\Exception\GuzzleException $exception
149
     * @param \Psr\Http\Message\RequestInterface $request
150
     *
151
     * @return \Http\Client\Exception
152
     */
153
    protected function handleGuzzleException(GuzzleException $exception, RequestInterface $request): HttpExceptionInterface
154
    {
155
        if ($exception instanceof SeekException) {
156
            return new HttpRequestException($exception->getMessage(), $request, $exception);
157
        }
158
159
        if ($exception instanceof ConnectException) {
160
            return new HttpNetworkException($exception->getMessage(), $exception->getRequest(), $exception);
161
        }
162
163
        if ($exception instanceof RequestException && $exception->hasResponse()) {
164
            return new HttpException(
165
                $exception->getMessage(),
166
                $exception->getRequest(),
167
                $exception->getResponse(),
168
                $exception
169
            );
170
        }
171
172
        if ($exception instanceof RequestException) {
173
            return new HttpRequestException($exception->getMessage(), $exception->getRequest(), $exception);
174
        }
175
176
        return new HttpTransferException($exception->getMessage(), 0, $exception);
177
    }
178
}
179