Request::setContentType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace keika299\ConohaAPI\Common\Network;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Handler\MockHandler;
7
use GuzzleHttp\HandlerStack;
8
use GuzzleHttp\Psr7\Response as Psr7Response;
9
use keika299\ConohaAPI\Common\Exceptions\Network\ExceptionFactory;
10
11
/**
12
 * Class Request
13
 *
14
 * Create API request and exec it.
15
 *
16
 * @package keika299\ConohaAPI\Common\Network
17
 */
18
class Request
19
{
20
    private $baseURI;
21
    private $method;
22
    private $uri;
23
    private $headers;
24
    private $options;
25
    private $query;
26
27
    /**
28
     * Request constructor.
29
     */
30 93
    public function __construct()
31
    {
32 93
        $this->baseURI = '';
33 93
        $this->method = 'GET';
34 93
        $this->uri = '';
35 93
        $this->headers = array();
36 93
        $this->options = array();
37 93
        $this->query = '';
38 93
    }
39
40
    /**
41
     * Exec API request.
42
     *
43
     * This function have to run after set all params.
44
     * If $_ENV['IS_TEST'] has value, this function return mock.
45
     *
46
     * @return Response
47
     * @throws \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
48
     */
49 85
    public function exec()
50
    {
51 85
        if (0 < count($this->headers)) {
52 85
            $this->options['headers'] = $this->headers;
53 85
        }
54
55 85
        $client = $this->getClient();
56
57
        try {
58 85
            $response = $client->request($this->method, $this->uri.$this->query, $this->options);
59 85
            return new Response($response);
60
        }
61
        catch (\Exception $e) {
62
            throw ExceptionFactory::build($e);
63
        }
64
    }
65
66
    /**
67
     * Set request method.
68
     * $method allow HTTP/1.1 request methods.
69
     *
70
     * @param string $method
71
     * @return $this
72
     */
73 86
    public function setMethod($method)
74
    {
75 86
        $this->method = strtoupper($method);
76 86
        return $this;
77
    }
78
79
    /**
80
     * Set base URI.
81
     *
82
     * $baseURI usually begin 'https://' or 'http://'.
83
     * $baseURI expect last character is not with '/'.
84
     *
85
     * @param string $baseURI
86
     * @return $this
87
     */
88 86
    public function setBaseURI($baseURI)
89
    {
90 86
        $this->baseURI = $baseURI;
91 86
        return $this;
92
    }
93
94
    /**
95
     * Set URI.
96
     *
97
     * $uri is NOT completely URI.
98
     * This param connect with base URI.
99
     * $uri expect to start with '/'.
100
     *
101
     * @param string $uri
102
     * @return $this
103
     */
104 86
    public function setURI($uri)
105
    {
106 86
        $this->uri = $uri;
107 86
        return $this;
108
    }
109
110
    /**
111
     * Add request header value.
112
     *
113
     * $key is parameter, and $value is value of that parameter.
114
     * You want to add 'X-Auth-Token', you can use setToken func.
115
     * You want to add 'Accept', you can use setAccept func.
116
     *
117
     * @param string $key
118
     * @param string $value
119
     * @return $this
120
     */
121 1
    public function addHeader($key, $value)
122
    {
123 1
        $this->headers[$key] = $value;
124 1
        return $this;
125
    }
126
127
    /**
128
     * Set 'Accept' request header.
129
     *
130
     * @param string $value
131
     * @return $this
132
     */
133 82
    public function setAccept($value)
134
    {
135 82
        $this->headers['Accept'] = $value;
136 82
        return $this;
137
    }
138
139
    /**
140
     * Set 'Content-Type' request header
141
     *
142
     * @param string $value
143
     * @return $this
144
     */
145 6
    public function setContentType($value)
146
    {
147 6
        $this->headers['Content-Type'] = $value;
148 6
        return $this;
149
    }
150
151
    /**
152
     * Set request body.
153
     *
154
     * You can use this function when request 'POST' or 'PUT' method.
155
     *
156
     * @param string $body
157
     * @return $this
158
     */
159 3
    public function setBody($body)
160
    {
161 3
        $this->options['body'] = $body;
162 3
        return $this;
163
    }
164
165
    /**
166
     * Set request Json.
167
     *
168
     * Array transform to json data, and set request body.
169
     *
170
     * @param array $array
171
     * @return $this
172
     */
173 27
    public function setJson(array $array)
174
    {
175 27
        $this->options['json'] = $array;
176 27
        return $this;
177
    }
178
179
    /**
180
     * Set request URI query.
181
     *
182
     * $array have to be associative array.
183
     *
184
     * @param array $array
185
     * @return $this
186
     */
187 14
    public function setQuery(array $array)
188
    {
189 14
        if (sizeof($array) === 0) {
190 13
            return $this;
191
        }
192
193 14
        $query = '?';
194
195 14
        foreach ($array as $item => $value) {
196 14
            $query .= $item . '=' . $value . '&';
197 14
        }
198
199 14
        $this->query = mb_substr($query, 0, -1, "UTF-8");
200
201 14
        return $this;
202
    }
203
204
    /**
205
     * Set 'X-Auth-Token' request header.
206
     *
207
     * @param string $token
208
     * @return $this
209
     */
210 76
    public function setToken($token)
211
    {
212 76
        $this->headers['X-Auth-Token'] = $token;
213 76
        return $this;
214
    }
215
216
    /**
217
     * @return Client
218
     */
219 85
    private function getClient()
220
    {
221 85
        return getenv('IS_TEST') ? $this->createMockClient() : new Client(['base_uri' => $this->baseURI]);
222
    }
223
224
    /**
225
     * @return Client
226
     */
227 85
    private function createMockClient()
228
    {
229 85
        $body = '{"checkKey": "checkValue", "access": {"token": {"id": "sample00d88246078f2bexample788f7"}}}';
230
231
        $mockResponse = [
232 85
            new Psr7Response(200, [], $body)
233 85
        ];
234 85
        $mockHandler = new MockHandler($mockResponse);
235 85
        $handlerStack = HandlerStack::create($mockHandler);
236 85
        return new Client(['handler' => $handlerStack]);
237
    }
238
}
239