Passed
Push — master ( b846fe...52bca8 )
by
unknown
07:07
created

Guzzle::createAfterPayApiResponseErrorTransfer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\Http;
9
10
use Generated\Shared\Transfer\AfterPayApiResponseErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...piResponseErrorTransfer 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 GuzzleHttp\Client;
12
use GuzzleHttp\Exception\RequestException;
13
use GuzzleHttp\Psr7\Request;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface;
16
use Psr\Http\Message\StreamInterface;
17
use SprykerEco\Zed\AfterPay\AfterPayConfig;
18
use SprykerEco\Zed\AfterPay\Business\Api\Adapter\Client\ClientInterface;
19
use SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException;
20
use SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface;
21
22
class Guzzle implements ClientInterface
23
{
24
    public const REQUEST_METHOD_POST = 'POST';
25
    public const REQUEST_METHOD_GET = 'GET';
26
27
    public const REQUEST_HEADER_X_AUTH_KEY = 'X-Auth-Key';
28
    public const REQUEST_HEADER_CONTENT_TYPE = 'Content-Type';
29
30
    public const HEADER_CONTENT_TYPE_JSON = 'application/json';
31
32
    protected const ERROR_KEY_ACTION_CODE = 'actionCode';
33
    protected const ERROR_KEY_CODE = 'code';
34
    protected const ERROR_KEY_TYPE = 'type';
35
    protected const ERROR_KEY_MESSAGE = 'message';
36
37
    /**
38
     * @var \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface
39
     */
40
    protected $encodingService;
41
42
    /**
43
     * @var \GuzzleHttp\ClientInterface
44
     */
45
    protected $client;
46
47
    /**
48
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
49
     */
50
    protected $config;
51
52
    /**
53
     * @param \SprykerEco\Zed\AfterPay\Dependency\Service\AfterPayToUtilEncodingServiceInterface $encodingService
54
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
55
     */
56
    public function __construct(
57
        AfterPayToUtilEncodingServiceInterface $encodingService,
58
        AfterPayConfig $config
59
    ) {
60
        $this->encodingService = $encodingService;
61
        $this->config = $config;
62
        $this->client = new Client();
63
    }
64
65
    /**
66
     * @param string $endPointUrl
67
     * @param string|null $jsonBody
68
     *
69
     * @return \Psr\Http\Message\StreamInterface
70
     */
71
    public function sendPost(string $endPointUrl, ?string $jsonBody = null): StreamInterface
72
    {
73
        $postRequest = $this->buildPostRequest($endPointUrl, $jsonBody);
74
        $response = $this->send($postRequest);
75
76
        return $response->getBody();
77
    }
78
79
    /**
80
     * @param string $endPointUrl
81
     *
82
     * @return string
83
     */
84
    public function sendGet(string $endPointUrl): string
85
    {
86
        $getRequest = $this->buildGetRequest($endPointUrl);
87
        $response = $this->send($getRequest);
88
89
        return $response->getBody()->getContents();
90
    }
91
92
    /**
93
     * @param string $endPointUrl
94
     *
95
     * @return int
96
     */
97
    public function getStatus(string $endPointUrl): int
98
    {
99
        $getRequest = $this->buildGetRequest($endPointUrl);
100
        $response = $this->send($getRequest);
101
102
        return $response->getStatusCode();
103
    }
104
105
    /**
106
     * @param \Psr\Http\Message\RequestInterface $request
107
     * @param array $options
108
     *
109
     * @throws \SprykerEco\Zed\AfterPay\Business\Exception\ApiHttpRequestException
110
     *
111
     * @return \Psr\Http\Message\ResponseInterface
112
     */
113
    protected function send(RequestInterface $request, array $options = []): ResponseInterface
114
    {
115
        try {
116
            return $this->client->send($request, $options);
117
        } catch (RequestException $requestException) {
118
            $apiHttpRequestException = new ApiHttpRequestException($requestException->getMessage());
119
120
            $responseContent = $this->getExceptionResponseContent($requestException);
121
            $errorsResponseData = $this->encodingService->decodeJson($responseContent, true);
122
123
            if (isset($errorsResponseData[0])) {
124
                $afterPayApiResponseErrorTransfer = $this->createAfterPayApiResponseErrorTransfer($errorsResponseData[0]);
125
126
                $apiHttpRequestException->setError($afterPayApiResponseErrorTransfer);
127
                $apiHttpRequestException->setDetailedMessage($responseContent);
128
            }
129
130
            throw $apiHttpRequestException;
131
        }
132
    }
133
134
    /**
135
     * @param array $errorDetails
136
     *
137
     * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer\AfterPayApiResponseErrorTransfer
0 ignored issues
show
Bug introduced by
The type Spryker\Shared\Kernel\Tr...piResponseErrorTransfer 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...
138
     */
139
    protected function createAfterPayApiResponseErrorTransfer(array $errorDetails): AfterPayApiResponseErrorTransfer
140
    {
141
        return (new AfterPayApiResponseErrorTransfer())
142
            ->setActionCode($errorDetails[static::ERROR_KEY_ACTION_CODE])
143
            ->setCode($errorDetails[static::ERROR_KEY_CODE])
144
            ->setType($errorDetails[static::ERROR_KEY_TYPE])
145
            ->setMessage($errorDetails[static::ERROR_KEY_MESSAGE])
146
            ->setIsSuccess(false);
147
    }
148
149
    /**
150
     * @param \GuzzleHttp\Exception\RequestException $requestException
151
     *
152
     * @return string
153
     */
154
    protected function getExceptionResponseContent(RequestException $requestException): string
155
    {
156
        $response = $requestException->getResponse();
157
158
        return $response instanceof ResponseInterface ? $response->getBody()->getContents() : '';
159
    }
160
161
    /**
162
     * @param string $endPointUrl
163
     * @param string|null $jsonBody
164
     *
165
     * @return \Psr\Http\Message\RequestInterface
166
     */
167
    protected function buildPostRequest(string $endPointUrl, ?string $jsonBody = null): RequestInterface
168
    {
169
        return new Request(
170
            static::REQUEST_METHOD_POST,
171
            $endPointUrl,
172
            [
173
                (string)static::REQUEST_HEADER_CONTENT_TYPE => (string)static::HEADER_CONTENT_TYPE_JSON,
174
                (string)static::REQUEST_HEADER_X_AUTH_KEY => (string)$this->config->getApiCredentialsAuthKey(),
175
            ],
176
            $jsonBody
177
        );
178
    }
179
180
    /**
181
     * @param string $endPointUrl
182
     *
183
     * @return \Psr\Http\Message\RequestInterface
184
     */
185
    protected function buildGetRequest(string $endPointUrl): RequestInterface
186
    {
187
        return new Request(
188
            static::REQUEST_METHOD_GET,
189
            $endPointUrl,
190
            [
191
                (string)static::REQUEST_HEADER_CONTENT_TYPE => (string)static::HEADER_CONTENT_TYPE_JSON,
192
                (string)static::REQUEST_HEADER_X_AUTH_KEY => (string)$this->config->getApiCredentialsAuthKey(),
193
            ]
194
        );
195
    }
196
}
197