Completed
Push — master ( 8e0025...04518a )
by
05:55
created

GuzzleHttpClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A send() 0 11 3
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace PhpMob\Omise\Client;
13
14
use GuzzleHttp\Psr7\Request;
15
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
16
use Http\Client\HttpClient;
17
use PhpMob\Omise\OmiseApi;
18
use Psr\Http\Message\ResponseInterface;
19
20
/**
21
 * @author Ishmael Doss <[email protected]>
22
 */
23
final class GuzzleHttpClient implements HttpClientInterface
24
{
25
    /**
26
     * @var HttpClient
27
     */
28
    private $httpClient;
29
30
    /**
31
     * @param array $config
32
     */
33
    public function __construct(array $config = [])
34
    {
35
        $this->httpClient = GuzzleAdapter::createWithConfig(
36
            array_replace_recursive(
37
                [
38
                    'verify' => true,
39
                    'headers' => [
40
                        'User-Agent' => 'PHPMOB-OMISE/'.OmiseApi::VERSION,
41
                    ],
42
                ],
43
                $config
44
            )
45
        );
46
    }
47
48
    /**
49
     * @param string $method
50
     * @param string $uri
51
     * @param array $data
52
     * @param array $headers
53
     *
54
     * @return ResponseInterface
55
     */
56
    public function send($method, $uri, array $data = [], array $headers = [])
57
    {
58
        if ('GET' === strtoupper($method) && !empty($data)) {
59
            $uri = $uri.'?'.http_build_query($data);
60
            $data = [];
61
        }
62
63
        return $this->httpClient->sendRequest(
64
            new Request($method, $uri, $headers, \GuzzleHttp\json_encode($data))
65
        );
66
    }
67
}
68