|
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\Sevensenders\Business\Api\Adapter; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\SevensendersRequestTransfer; |
|
|
|
|
|
|
11
|
|
|
use Generated\Shared\Transfer\SevensendersResponseTransfer; |
|
|
|
|
|
|
12
|
|
|
use GuzzleHttp\Client; |
|
13
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
14
|
|
|
use GuzzleHttp\RequestOptions; |
|
15
|
|
|
use SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiBadCredentialsException; |
|
16
|
|
|
use SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiHttpRequestException; |
|
17
|
|
|
use SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface; |
|
18
|
|
|
use SprykerEco\Zed\Sevensenders\SevensendersConfig; |
|
19
|
|
|
|
|
20
|
|
|
class SevensendersApiAdapter implements AdapterInterface |
|
21
|
|
|
{ |
|
22
|
|
|
protected const DEFAULT_TIMEOUT = 45; |
|
23
|
|
|
|
|
24
|
|
|
public const ORDER_RESOURCE = 'orders'; |
|
25
|
|
|
public const SHIPMENT_RESOURCE = 'shipments'; |
|
26
|
|
|
public const AUTH_RESOURCE = 'token'; |
|
27
|
|
|
|
|
28
|
|
|
protected const RESPONSE_KEY_TOKEN = 'token'; |
|
29
|
|
|
|
|
30
|
|
|
protected const REQUEST_KEY_ACCESS_KEY = 'access_key'; |
|
31
|
|
|
|
|
32
|
|
|
protected const DEFAULT_HEADERS = [ |
|
33
|
|
|
'Content-Type' => 'application/json', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var \GuzzleHttp\Client |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $client; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var \SprykerEco\Zed\Sevensenders\SevensendersConfig |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $config; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $utilEncodingService; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \SprykerEco\Zed\Sevensenders\SevensendersConfig $config |
|
53
|
|
|
* @param \SprykerEco\Zed\Sevensenders\Dependency\Service\SevensendersToUtilEncodingServiceInterface $utilEncodingService |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(SevensendersConfig $config, SevensendersToUtilEncodingServiceInterface $utilEncodingService) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->config = $config; |
|
58
|
|
|
$this->utilEncodingService = $utilEncodingService; |
|
59
|
|
|
$this->client = new Client([ |
|
60
|
|
|
RequestOptions::TIMEOUT => static::DEFAULT_TIMEOUT, |
|
61
|
|
|
]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer |
|
66
|
|
|
* @param string $resource |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiHttpRequestException |
|
69
|
|
|
* |
|
70
|
|
|
* @return \Generated\Shared\Transfer\SevensendersResponseTransfer |
|
71
|
|
|
*/ |
|
72
|
|
|
public function send(SevensendersRequestTransfer $transfer, string $resource): SevensendersResponseTransfer |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
|
|
$options = $this->prepareOptions($transfer); |
|
76
|
|
|
$response = $this->client->post( |
|
77
|
|
|
$this->getUrl($resource), |
|
78
|
|
|
$options |
|
79
|
|
|
); |
|
80
|
|
|
} catch (RequestException $requestException) { |
|
81
|
|
|
throw new SevensendersApiHttpRequestException( |
|
82
|
|
|
$requestException->getMessage(), |
|
83
|
|
|
$requestException->getCode(), |
|
84
|
|
|
$requestException |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$responseTransfer = new SevensendersResponseTransfer(); |
|
89
|
|
|
$responseTransfer->setStatus($response->getStatusCode()); |
|
90
|
|
|
$responseTransfer->setRequestPayload($options[RequestOptions::BODY]); |
|
91
|
|
|
$responseTransfer->setResponsePayload($response->getBody()->getContents()); |
|
92
|
|
|
|
|
93
|
|
|
return $responseTransfer; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @throws \SprykerEco\Zed\Sevensenders\Business\Exception\SevensendersApiBadCredentialsException |
|
98
|
|
|
* |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function auth(): string |
|
102
|
|
|
{ |
|
103
|
|
|
$response = $this->utilEncodingService->decodeJson($this->client->post($this->getUrl(static::AUTH_RESOURCE), [ |
|
104
|
|
|
RequestOptions::BODY => $this->utilEncodingService->encodeJson([ |
|
105
|
|
|
static::REQUEST_KEY_ACCESS_KEY => $this->config->getApiKey(), |
|
106
|
|
|
]), |
|
107
|
|
|
])->getBody()->getContents()); |
|
108
|
|
|
|
|
109
|
|
|
if (!array_key_exists(static::RESPONSE_KEY_TOKEN, $response)) { |
|
110
|
|
|
throw new SevensendersApiBadCredentialsException('Bad credentials', 401); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return 'Bearer ' . $response[static::RESPONSE_KEY_TOKEN]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $resource |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function getUrl(string $resource): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->config->getApiUrl() . $resource; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/**i |
|
127
|
|
|
* |
|
128
|
|
|
* @param \Generated\Shared\Transfer\SevensendersRequestTransfer $transfer |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function prepareOptions(SevensendersRequestTransfer $transfer): array |
|
133
|
|
|
{ |
|
134
|
|
|
$options[RequestOptions::BODY] = $this->utilEncodingService->encodeJson($transfer->getPayload()); |
|
|
|
|
|
|
135
|
|
|
$options[RequestOptions::HEADERS] = $this->prepareHeaders(); |
|
136
|
|
|
|
|
137
|
|
|
return $options; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function prepareHeaders(): array |
|
144
|
|
|
{ |
|
145
|
|
|
$auth = [ |
|
146
|
|
|
'Authorization' => $this->auth(), |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
return array_merge(static::DEFAULT_HEADERS, $auth); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
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