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; |
|
|
|
|
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\AfterpayToUtilEncodingInterface; |
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
|
|
|
/** |
33
|
|
|
* @var \SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilEncodingInterface |
34
|
|
|
*/ |
35
|
|
|
protected $encodingService; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var \GuzzleHttp\ClientInterface |
39
|
|
|
*/ |
40
|
|
|
protected $client; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \SprykerEco\Zed\Afterpay\AfterpayConfig |
44
|
|
|
*/ |
45
|
|
|
private $config; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param \SprykerEco\Zed\Afterpay\Dependency\Service\AfterpayToUtilEncodingInterface $encodingService |
49
|
|
|
* @param \SprykerEco\Zed\Afterpay\AfterpayConfig $config |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
AfterpayToUtilEncodingInterface $encodingService, |
53
|
|
|
AfterpayConfig $config |
54
|
|
|
) { |
55
|
|
|
$this->encodingService = $encodingService; |
56
|
|
|
$this->config = $config; |
57
|
|
|
$this->client = new Client(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $endPointUrl |
62
|
|
|
* @param string|null $jsonBody |
63
|
|
|
* |
64
|
|
|
* @return \Psr\Http\Message\StreamInterface |
65
|
|
|
*/ |
66
|
|
|
public function sendPost(string $endPointUrl, ?string $jsonBody = null): StreamInterface |
67
|
|
|
{ |
68
|
|
|
$postRequest = $this->buildPostRequest($endPointUrl, $jsonBody); |
69
|
|
|
$response = $this->send($postRequest); |
70
|
|
|
|
71
|
|
|
return $response->getBody(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $endPointUrl |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function sendGet(string $endPointUrl): string |
80
|
|
|
{ |
81
|
|
|
$getRequest = $this->buildGetRequest($endPointUrl); |
82
|
|
|
$response = $this->send($getRequest); |
83
|
|
|
|
84
|
|
|
return $response->getBody()->getContents(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $endPointUrl |
89
|
|
|
* |
90
|
|
|
* @return int |
91
|
|
|
*/ |
92
|
|
|
public function getStatus(string $endPointUrl): int |
93
|
|
|
{ |
94
|
|
|
$getRequest = $this->buildGetRequest($endPointUrl); |
95
|
|
|
$response = $this->send($getRequest); |
96
|
|
|
|
97
|
|
|
return $response->getStatusCode(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Psr\Http\Message\RequestInterface $request |
102
|
|
|
* @param array $options |
103
|
|
|
* |
104
|
|
|
* @throws \SprykerEco\Zed\Afterpay\Business\Exception\ApiHttpRequestException |
105
|
|
|
* |
106
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
107
|
|
|
*/ |
108
|
|
|
protected function send(RequestInterface $request, array $options = []): ResponseInterface |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
|
|
return $this->client->send($request, $options); |
112
|
|
|
} catch (RequestException $requestException) { |
113
|
|
|
$apiHttpRequestException = new ApiHttpRequestException($requestException->getMessage()); |
114
|
|
|
|
115
|
|
|
$content = $requestException->getResponse()->getBody()->getContents(); |
116
|
|
|
if (!empty($content)) { |
117
|
|
|
$errorResponseData = $this->encodingService->decodeJson($content, true); |
118
|
|
|
if (isset($errorResponseData[0])) { |
119
|
|
|
$errorDetails = $errorResponseData[0]; |
120
|
|
|
$apiErrorTransfer = new AfterpayApiResponseErrorTransfer(); |
121
|
|
|
$apiErrorTransfer |
122
|
|
|
->setActionCode($errorDetails['actionCode']) |
123
|
|
|
->setCode($errorDetails['code']) |
124
|
|
|
->setType($errorDetails['type']) |
125
|
|
|
->setMessage($errorDetails['message']) |
126
|
|
|
->setIsSuccess(false); |
127
|
|
|
$apiHttpRequestException->setError($apiErrorTransfer); |
128
|
|
|
$apiHttpRequestException->setDetailedMessage($content); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
throw $apiHttpRequestException; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $endPointUrl |
138
|
|
|
* @param string|null $jsonBody |
139
|
|
|
* |
140
|
|
|
* @return \Psr\Http\Message\RequestInterface |
141
|
|
|
*/ |
142
|
|
|
protected function buildPostRequest(string $endPointUrl, ?string $jsonBody = null): RequestInterface |
143
|
|
|
{ |
144
|
|
|
return new Request( |
145
|
|
|
static::REQUEST_METHOD_POST, |
146
|
|
|
$endPointUrl, |
147
|
|
|
[ |
148
|
|
|
static::REQUEST_HEADER_CONTENT_TYPE => static::HEADER_CONTENT_TYPE_JSON, |
149
|
|
|
static::REQUEST_HEADER_X_AUTH_KEY => $this->config->getApiCredentialsAuthKey(), |
150
|
|
|
], |
151
|
|
|
$jsonBody |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param string $endPointUrl |
157
|
|
|
* |
158
|
|
|
* @return \Psr\Http\Message\RequestInterface |
159
|
|
|
*/ |
160
|
|
|
protected function buildGetRequest(string $endPointUrl): RequestInterface |
161
|
|
|
{ |
162
|
|
|
return new Request( |
163
|
|
|
static::REQUEST_METHOD_GET, |
164
|
|
|
$endPointUrl, |
165
|
|
|
[ |
166
|
|
|
static::REQUEST_HEADER_CONTENT_TYPE => static::HEADER_CONTENT_TYPE_JSON, |
167
|
|
|
static::REQUEST_HEADER_X_AUTH_KEY => $this->config->getApiCredentialsAuthKey(), |
168
|
|
|
] |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths