Passed
Push — master ( 5319ee...e6b641 )
by Dani
02:13
created

GuzzleClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 45
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A send() 0 26 2
1
<?php
2
3
namespace Postpay\HttpClients;
4
5
use GuzzleHttp\Client as GuzzleHttpClient;
6
use GuzzleHttp\Exception\RequestException;
7
use GuzzleHttp\RequestOptions;
8
9
use Postpay\Exceptions\PostpayException;
10
use Postpay\Http\Request;
11
use Postpay\Http\Response;
12
13
class GuzzleClient implements ClientInterface
14
{
15
    /**
16
     * @var GuzzleHttpClient The Guzzle client.
17
     */
18
    protected $guzzleClient;
19
20
    /**
21
     * @param GuzzleHttpClient|null $guzzleClient The Guzzle client.
22
     */
23 4
    public function __construct(GuzzleHttpClient $guzzleClient = null)
24
    {
25 4
        $this->guzzleClient = $guzzleClient ?: new GuzzleHttpClient();
26 4
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 1
    public function send(Request $request, $timeout = null)
32
    {
33
        $options = [
34 1
            'auth' => $request->getAuth(),
35 1
            'headers' => $request->getHeaders(),
36
            'http_errors' => false,
37 1
            'timeout' => $timeout,
38 1
            'connect_timeout' => 10,
39 1
            RequestOptions::JSON => $request->json(),
40
        ];
41
        try {
42 1
            $response = $this->guzzleClient->request(
43 1
                $request->getMethod(),
44 1
                $request->getUrl(),
45
                $options
46
            );
47 1
        } catch (RequestException $e) {
48 1
            throw new PostpayException($e->getMessage(), $e->getCode());
49
        }
50 1
        return new Response(
51 1
            $request,
52 1
            $response->getStatusCode(),
53 1
            $response->getHeaders(),
54 1
            $response->getBody()
55
        );
56
    }
57
}
58