1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\YahooShoppingJp; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as HttpClient; |
6
|
|
|
use GuzzleHttp\Exception\ConnectException as GuzzleConnectException; |
7
|
|
|
use GuzzleHttp\Exception\ServerException as GuzzleServerException; |
8
|
|
|
use GuzzleHttp\Exception\ClientException as GuzzleClientException; |
9
|
|
|
use GuzzleHttp\RequestOptions; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
use Shippinno\YahooShoppingJp\Api\AbstractApi; |
12
|
|
|
use Shippinno\YahooShoppingJp\Exception\ClientException; |
13
|
|
|
use Shippinno\YahooShoppingJp\Exception\ConnectException; |
14
|
|
|
use Shippinno\YahooShoppingJp\Exception\ExpiredAccessTokenException; |
15
|
|
|
use Shippinno\YahooShoppingJp\Exception\ServerException; |
16
|
|
|
use Shippinno\YahooShoppingJp\Request\AbstractRequest; |
17
|
|
|
use SoapBox\Formatter\Formatter; |
18
|
|
|
|
19
|
|
|
class Client |
20
|
|
|
{ |
21
|
|
|
// /** |
22
|
|
|
// * @var string |
23
|
|
|
// */ |
24
|
|
|
// const BASE_URL = 'https://circus.shopping.yahooapis.jp/ShoppingWebService/V1/'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
const BASE_URL = 'https://test.circus.shopping.yahooapis.jp/ShoppingWebService/V1/'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var HttpClient |
33
|
|
|
*/ |
34
|
|
|
private $httpClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var AbstractApi |
38
|
|
|
*/ |
39
|
|
|
private $api; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $accessToken; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $cert; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var |
53
|
|
|
*/ |
54
|
|
|
private $sslKey; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $accessToken |
58
|
|
|
* @param string $cert |
59
|
|
|
* @param string $sslKey |
60
|
|
|
* @param HttpClient|null $httpClient |
61
|
|
|
*/ |
62
|
|
|
public function __construct( |
63
|
|
|
string $accessToken, |
64
|
|
|
string $cert = null, |
65
|
|
|
string $sslKey = null, |
66
|
|
|
HttpClient $httpClient = null |
67
|
|
|
) { |
68
|
|
|
if (null === $httpClient) { |
69
|
|
|
$httpClient = new HttpClient([ |
70
|
|
|
'base_uri' => self::BASE_URL, |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->accessToken = $accessToken; |
75
|
|
|
$this->cert = $cert; |
76
|
|
|
$this->sslKey = $sslKey; |
77
|
|
|
$this->httpClient = $httpClient; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param AbstractRequest $request |
82
|
|
|
* @return array|mixed |
83
|
|
|
* @throws ClientException |
84
|
|
|
* @throws ConnectException |
85
|
|
|
* @throws ExpiredAccessTokenException |
86
|
|
|
* @throws ServerException |
87
|
|
|
*/ |
88
|
|
|
public function execute(AbstractRequest $request): array |
89
|
|
|
{ |
90
|
|
|
$this->setApi($request->api()); |
91
|
|
|
|
92
|
|
|
$options = $this->setAuthorizationHeader($this->setRequestParams($request)); |
93
|
|
|
|
94
|
|
|
$options = $this->setCertAndSslKey($options); |
95
|
|
|
|
96
|
|
|
try { |
97
|
|
|
$rawResponse = $this->request($options); |
98
|
|
|
} catch (GuzzleClientException $e) { |
99
|
|
|
$wwwAuthenticateHeaders = $e->getResponse()->getHeader('WWW-Authenticate'); |
100
|
|
|
|
101
|
|
|
if ([] !== $wwwAuthenticateHeaders) { |
102
|
|
|
if (false !== strpos($wwwAuthenticateHeaders[0], 'error_description="expired token"')) { |
103
|
|
|
throw new ExpiredAccessTokenException; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new ClientException($e->getMessage(), $e->getCode(), $e); |
108
|
|
|
} catch (GuzzleServerException $e) { |
109
|
|
|
throw new ServerException($e->getMessage(), $e->getCode(), $e); |
110
|
|
|
} catch (GuzzleConnectException $e) { |
111
|
|
|
throw new ConnectException($e->getMessage(), $e->getCode(), $e); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->api->distillResponse($this->decodeResponse($rawResponse)); |
115
|
|
|
// $response = $request->response()->setData($distilledResponse); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param AbstractApi $api |
120
|
|
|
*/ |
121
|
|
|
private function setApi(AbstractApi $api) |
122
|
|
|
{ |
123
|
|
|
$this->api = $api; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $options |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
private function setCertAndSslKey(array $options): array |
131
|
|
|
{ |
132
|
|
|
if (null !== $this->cert && null !== $this->sslKey) { |
133
|
|
|
$options[RequestOptions::CERT] = $this->cert, |
|
|
|
|
134
|
|
|
$options[RequestOptions::SSL_KEY] = $this->sslKey; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $options; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param array $options |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
private function setAuthorizationHeader(array $options): array |
145
|
|
|
{ |
146
|
|
|
$options['headers'] = [ |
147
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken, |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
return $options; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param AbstractRequest $request |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
private function setRequestParams(AbstractRequest $request): array |
158
|
|
|
{ |
159
|
|
|
$options = []; |
160
|
|
|
if ($this->api->httpMethod()->equals(HttpMethod::GET())) { |
161
|
|
|
$options = $this->setRequestParamsForGetRequest($options, $request); |
162
|
|
|
} else { |
163
|
|
|
if ($this->api->httpMethod()->equals(HttpMethod::POST())) { |
164
|
|
|
$options = $this->setRequestParamsForPostRequest($options, $request); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $options; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param array $options |
173
|
|
|
* @param AbstractRequest $request |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
|
|
private function setRequestParamsForGetRequest(array $options, AbstractRequest $request): array |
177
|
|
|
{ |
178
|
|
|
$options['query'] = $request->getParams(); |
179
|
|
|
|
180
|
|
|
return $options; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $options |
185
|
|
|
* @param AbstractRequest $request |
186
|
|
|
* @return array |
187
|
|
|
*/ |
188
|
|
|
private function setRequestParamsForPostRequest(array $options, AbstractRequest $request): array |
189
|
|
|
{ |
190
|
|
|
if ($this->api->expectsFormFields()) { |
191
|
|
|
$options['form_params'] = $request->getParams(); |
192
|
|
|
} else { |
193
|
|
|
$options['body'] = $request->getParams(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $options; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param $options |
202
|
|
|
* @return mixed|ResponseInterface |
203
|
|
|
*/ |
204
|
|
|
private function request($options) |
205
|
|
|
{ |
206
|
|
|
return $this->httpClient->request( |
207
|
|
|
$this->api->httpMethod()->getValue(), |
208
|
|
|
$this->api->path(), |
209
|
|
|
$options |
210
|
|
|
); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param ResponseInterface $rawResponse |
215
|
|
|
* @return array |
216
|
|
|
*/ |
217
|
|
|
private function decodeResponse(ResponseInterface $rawResponse): array |
218
|
|
|
{ |
219
|
|
|
$formatter = Formatter::make($rawResponse->getBody()->getContents(), Formatter::XML); |
220
|
|
|
return $formatter->toArray(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.