Test Failed
Pull Request — master (#19)
by
unknown
02:23
created

Client::request()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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