GuzzleClient::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 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