1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Shippinno\YahooShoppingJp; |
4
|
|
|
|
5
|
|
|
use FluidXml\FluidXml; |
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
7
|
|
|
use GuzzleHttp\Exception\ConnectException as GuzzleConnectException; |
8
|
|
|
use GuzzleHttp\Exception\ServerException as GuzzleServerException; |
9
|
|
|
use GuzzleHttp\Exception\ClientException as GuzzleClientException; |
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\ServerException; |
15
|
|
|
use Shippinno\YahooShoppingJp\Request\AbstractRequest; |
16
|
|
|
|
17
|
|
|
class Client |
18
|
|
|
{ |
19
|
|
|
// /** |
20
|
|
|
// * @var string |
21
|
|
|
// */ |
22
|
|
|
// const BASE_URL = 'https://circus.shopping.yahooapis.jp/ShoppingWebService/V1/'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
const BASE_URL = 'https://test.circus.shopping.yahooapis.jp/ShoppingWebService/V1/'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var HttpClient |
31
|
|
|
*/ |
32
|
|
|
private $httpClient; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AbstractApi |
36
|
|
|
*/ |
37
|
|
|
private $api; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $accessToken; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $refreshToken; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $accessToken |
51
|
|
|
* @param string $refreshToken |
52
|
|
|
* @param HttpClient|null $httpClient |
53
|
|
|
*/ |
54
|
|
|
public function __construct( |
55
|
|
|
string $accessToken, |
56
|
|
|
string $refreshToken, |
57
|
|
|
HttpClient $httpClient = null |
58
|
|
|
) { |
59
|
|
|
if (null === $httpClient) { |
60
|
|
|
$httpClient = new HttpClient([ |
61
|
|
|
'base_uri' => self::BASE_URL, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->accessToken = $accessToken; |
66
|
|
|
$this->refreshToken = $refreshToken; |
67
|
|
|
$this->httpClient = $httpClient; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param AbstractRequest $request |
72
|
|
|
* @return mixed |
73
|
|
|
* @throws ClientException |
74
|
|
|
* @throws ConnectException |
75
|
|
|
* @throws ServerException |
76
|
|
|
*/ |
77
|
|
|
public function execute(AbstractRequest $request): array |
78
|
|
|
{ |
79
|
|
|
$this->setApi($request->api()); |
80
|
|
|
|
81
|
|
|
$options = $this->setAuthorizationHeader($this->setRequestParams($request)); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$rawResponse = $this->request($options); |
85
|
|
|
} catch (GuzzleClientException $e) { |
86
|
|
|
throw new ClientException($e->getMessage(), $e->getCode(), $e); |
87
|
|
|
} catch (GuzzleServerException $e) { |
88
|
|
|
throw new ServerException($e->getMessage(), $e->getCode(), $e); |
89
|
|
|
} catch (GuzzleConnectException $e) { |
90
|
|
|
throw new ConnectException($e->getMessage(), $e->getCode(), $e); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$distilledResponse = $this->api->distillResponse($this->decodeResponse($rawResponse)); |
94
|
|
|
$response = $request->response()->setData($distilledResponse); |
95
|
|
|
|
96
|
|
|
return $response; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param AbstractApi $api |
101
|
|
|
*/ |
102
|
|
|
private function setApi(AbstractApi $api) |
103
|
|
|
{ |
104
|
|
|
$this->api = $api; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param array $options |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
private function setAuthorizationHeader(array $options): array |
112
|
|
|
{ |
113
|
|
|
$options['headers'] = [ |
114
|
|
|
'Authorization' => 'Bearer ' . $this->accessToken, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
return $options; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param AbstractRequest $request |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
private function setRequestParams(AbstractRequest $request): array |
125
|
|
|
{ |
126
|
|
|
$options = []; |
127
|
|
|
if ($this->api->httpMethod()->equals(HttpMethod::GET())) { |
128
|
|
|
$options = $this->setRequestParamsForGetRequest($options, $request); |
129
|
|
|
} else { |
130
|
|
|
if ($this->api->httpMethod()->equals(HttpMethod::POST())) { |
131
|
|
|
$options = $this->setRequestParamsForPostRequest($options, $request); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $options; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param array $options |
140
|
|
|
* @param AbstractRequest $request |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
private function setRequestParamsForGetRequest(array $options, AbstractRequest $request): array |
144
|
|
|
{ |
145
|
|
|
$options['query'] = $request->getParams(); |
146
|
|
|
|
147
|
|
|
return $options; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array $options |
152
|
|
|
* @param AbstractRequest $request |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
private function setRequestParamsForPostRequest(array $options, AbstractRequest $request): array |
156
|
|
|
{ |
157
|
|
|
$fluidXml = new FluidXml('Req'); |
158
|
|
|
$fluidXml->add($request->getParams()); |
159
|
|
|
$options['form'] = $fluidXml->xml(); |
160
|
|
|
|
161
|
|
|
return $options; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param $options |
167
|
|
|
* @return mixed|ResponseInterface |
168
|
|
|
*/ |
169
|
|
|
private function request($options) |
170
|
|
|
{ |
171
|
|
|
return $this->httpClient->request( |
172
|
|
|
$this->api->httpMethod()->getValue(), |
173
|
|
|
$this->api->path(), |
174
|
|
|
$options |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param ResponseInterface $rawResponse |
180
|
|
|
* @return array |
181
|
|
|
*/ |
182
|
|
|
private function decodeResponse(ResponseInterface $rawResponse): array |
183
|
|
|
{ |
184
|
|
|
return json_decode( |
185
|
|
|
json_encode( |
186
|
|
|
simplexml_load_string( |
187
|
|
|
$rawResponse->getBody()->getContents(), |
188
|
|
|
null, |
189
|
|
|
LIBXML_NOCDATA |
190
|
|
|
) |
191
|
|
|
), |
192
|
|
|
true |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|