GuzzleHttpClient::send()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 4
crap 12
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
declare(strict_types=1);
13
14
namespace PhpMob\Omise\Client;
15
16
use GuzzleHttp\Psr7\Request;
17
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
18
use Http\Client\HttpAsyncClient;
19
use Http\Client\HttpClient;
20
use PhpMob\Omise\OmiseApi;
21
use Psr\Http\Message\ResponseInterface;
22
23
/**
24
 * @author Ishmael Doss <[email protected]>
25
 */
26
final class GuzzleHttpClient implements HttpClientInterface
27
{
28
    /**
29
     * @var HttpClient
30
     */
31
    private $httpClient;
32
33
    /**
34
     * @param HttpClient $client
35
     * @param array $config
36
     */
37
    public function __construct(?HttpClient $client = null, array $config = [])
38
    {
39
        $this->httpClient = $client ?: GuzzleAdapter::createWithConfig(
40
            array_replace_recursive(
41
                [
42
                    'verify' => true,
43
                    'headers' => [
44
                        'User-Agent' => 'PHPMOB-OMISE/' . OmiseApi::VERSION,
45
                    ],
46
                ],
47
                $config
48
            )
49
        );
50
    }
51
52
    /**
53
     * @param string $method
54
     * @param string $uri
55
     * @param array $data
56
     * @param array $headers
57
     *
58
     * @return ResponseInterface
59
     */
60
    public function send($method, $uri, array $data = [], array $headers = [])
61
    {
62
        if ('GET' === strtoupper($method) && !empty($data)) {
63
            $uri = $uri . '?' . http_build_query($data);
64
            $data = [];
65
        }
66
67
        return $this->httpClient->sendRequest(
68
            new Request($method, $uri, $headers, \GuzzleHttp\json_encode($data))
69
        );
70
    }
71
}
72