Test Failed
Push — develop ( f64a3f...dfbc5e )
by Hirofumi
03:12
created

Client::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
cc 5
eloc 15
nc 5
nop 1
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 Psr\Http\Message\ResponseInterface;
10
use Shippinno\YahooShoppingJp\Api\AbstractApi;
11
use Shippinno\YahooShoppingJp\Exception\ClientException;
12
use Shippinno\YahooShoppingJp\Exception\ConnectException;
13
use Shippinno\YahooShoppingJp\Exception\ServerException;
14
use Shippinno\YahooShoppingJp\Request\AbstractRequest;
15
use SoapBox\Formatter\Formatter;
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
            $wwwAuthenticateHeader = $e->getResponse()->getHeader('WWW-Authenticate');
87
88
            if (false !== strpos($wwwAuthenticateHeader, 'error_description="expired token"')) {
89
                throw new ExpiredAccessTokenException;
90
            }
91
92
            throw new ClientException($e->getMessage(), $e->getCode(), $e);
93
        } catch (GuzzleServerException $e) {
94
            throw new ServerException($e->getMessage(), $e->getCode(), $e);
95
        } catch (GuzzleConnectException $e) {
96
            throw new ConnectException($e->getMessage(), $e->getCode(), $e);
97
        }
98
99
        return $this->api->distillResponse($this->decodeResponse($rawResponse));
100
//        $response = $request->response()->setData($distilledResponse);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
101
    }
102
103
    /**
104
     * @param AbstractApi $api
105
     */
106
    private function setApi(AbstractApi $api)
107
    {
108
        $this->api = $api;
109
    }
110
111
    /**
112
     * @param array $options
113
     * @return array
114
     */
115
    private function setAuthorizationHeader(array $options): array
116
    {
117
        $options['headers'] = [
118
            'Authorization' => 'Bearer ' . $this->accessToken,
119
        ];
120
121
        return $options;
122
    }
123
124
    /**
125
     * @param AbstractRequest $request
126
     * @return array
127
     */
128
    private function setRequestParams(AbstractRequest $request): array
129
    {
130
        $options = [];
131
        if ($this->api->httpMethod()->equals(HttpMethod::GET())) {
132
            $options = $this->setRequestParamsForGetRequest($options, $request);
133
        } else {
134
            if ($this->api->httpMethod()->equals(HttpMethod::POST())) {
135
                $options = $this->setRequestParamsForPostRequest($options, $request);
136
            }
137
        }
138
139
        return $options;
140
    }
141
142
    /**
143
     * @param array $options
144
     * @param AbstractRequest $request
145
     * @return array
146
     */
147
    private function setRequestParamsForGetRequest(array $options, AbstractRequest $request): array
148
    {
149
        $options['query'] = $request->getParams();
150
151
        return $options;
152
    }
153
154
    /**
155
     * @param array $options
156
     * @param AbstractRequest $request
157
     * @return array
158
     */
159
    private function setRequestParamsForPostRequest(array $options, AbstractRequest $request): array
160
    {
161
        if ($this->api->expectsFormFields()) {
162
            $options['form_params'] = $request->getParams();
163
        } else {
164
            $options['body'] = $request->getParams();
165
        }
166
167
        return $options;
168
    }
169
170
171
    /**
172
     * @param $options
173
     * @return mixed|ResponseInterface
174
     */
175
    private function request($options)
176
    {
177
        return $this->httpClient->request(
178
            $this->api->httpMethod()->getValue(),
179
            $this->api->path(),
180
            $options
181
        );
182
    }
183
184
    /**
185
     * @param ResponseInterface $rawResponse
186
     * @return array
187
     */
188
    private function decodeResponse(ResponseInterface $rawResponse): array
189
    {
190
        $formatter = Formatter::make($rawResponse->getBody()->getContents(), Formatter::XML);
191
        return $formatter->toArray();
192
    }
193
}
194