Completed
Push — master ( c731dd...0ae8dc )
by Gallice
42s
created

GuzzleHttpClient::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Tgallice\Wit\HttpClient;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use GuzzleHttp\ClientInterface;
7
use GuzzleHttp\RequestOptions;
8
use Tgallice\Wit\Client;
9
10
class GuzzleHttpClient implements HttpClient
11
{
12
    /**
13
     * @var ClientInterface
14
     */
15
    private $guzzleClient;
16
17
    /**
18
     * @param ClientInterface|null $guzzleClient
19
     */
20 3
    public function __construct(ClientInterface $guzzleClient = null)
21
    {
22 3
        $this->guzzleClient = $guzzleClient ?: new GuzzleClient([
23 1
            'base_uri' => Client::API_BASE_URI,
24 1
            'timeout' => Client::DEFAULT_TIMEOUT,
25 1
            'connect_timeout' => Client::DEFAULT_TIMEOUT,
26 1
        ]);
27 3
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 1
    public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
33
    {
34 1
        $options = array_merge($options, [
35 1
            RequestOptions::QUERY => $query,
36 1
            RequestOptions::HEADERS => $headers,
37 1
        ]);
38
39 1
        if (!empty($body) && (is_array($body) || $body instanceof \JsonSerializable)) {
40
            $options[RequestOptions::JSON] = $body;
41
        } else {
42 1
            $options[RequestOptions::BODY] = $body;
43
        }
44
45 1
        return $this->guzzleClient->request($method, $uri, $options);
46
    }
47
}
48