Test Failed
Pull Request — master (#19)
by Aya
06:22
created

Client::setRequestParamsForGetRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 2
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
        /*
160
         * form_paramsかbodyに入れないとリクエスト内容が空になってしまう。
161
         * `expectsFormFields()`を使っていたのはどこに?
162
         * ↓
163
         */
164
//        $options['form'] = $fluidXml->xml();
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
165
        $options['body'] = $fluidXml->xml();
166
167
168
        return $options;
169
    }
170
171
172
    /**
173
     * @param $options
174
     * @return mixed|ResponseInterface
175
     */
176
    private function request($options)
177
    {
178
        return $this->httpClient->request(
179
            $this->api->httpMethod()->getValue(),
180
            $this->api->path(),
181
            $options
182
        );
183
    }
184
185
    /**
186
     * @param ResponseInterface $rawResponse
187
     * @return array
188
     */
189
    private function decodeResponse(ResponseInterface $rawResponse): array
190
    {
191
        return json_decode(
192
            json_encode(
193
                simplexml_load_string(
194
                    $rawResponse->getBody()->getContents(),
195
                    null,
196
                    LIBXML_NOCDATA
197
                )
198
            ),
199
            true
200
        );
201
    }
202
}
203