Completed
Push — master ( 0ae8dc...1ca3b7 )
by Gallice
02:47
created

GuzzleHttpClient   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A send() 0 15 4
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 4
    public function __construct(ClientInterface $guzzleClient = null)
21
    {
22 4
        $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 4
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32 2
    public function send($method, $uri, $body = null, array $query = [], array $headers = [], array $options = [])
33
    {
34 2
        $options = array_merge($options, [
35 2
            RequestOptions::QUERY => $query,
36 2
            RequestOptions::HEADERS => $headers,
37 2
        ]);
38
39 2
        if (!empty($body) && (is_array($body) || $body instanceof \JsonSerializable)) {
40 1
            $options[RequestOptions::JSON] = $body;
41 1
        } else {
42 1
            $options[RequestOptions::BODY] = $body;
43
        }
44
45 2
        return $this->guzzleClient->request($method, $uri, $options);
46
    }
47
}
48