Passed
Push — master ( 277b3a...5ee2c0 )
by Dani
01:31
created

GuzzleClient::__construct()   A

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 $client;
19
20
    /**
21
     * @param GuzzleHttpClient|null $client The Guzzle client.
22
     */
23 1
    public function __construct(GuzzleHttpClient $client = null)
24
    {
25 1
        $this->client = $client ?: new GuzzleHttpClient();
26 1
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function send(Request $request, $timeout)
32
    {
33
        $options = [
34
            'auth' => $request->getAuth(),
35
            'headers' => $request->getHeaders(),
36
            'http_errors' => false,
37
            'timeout' => $timeout,
38
            'connect_timeout' => 10,
39
            RequestOptions::JSON => $request->json(),
40
        ];
41
        try {
42
            $response = $this->client->request(
43
                $request->getMethod(),
44
                $request->getUrl(),
45
                $options
46
            );
47
        } catch (RequestException $e) {
48
            throw new PostpayException($e->getMessage(), $e->getCode());
49
        }
50
        $headers = $response->getHeaders();
51
        $body = $response->getBody();
52
        $statusCode = $response->getStatusCode();
53
        return new Response($request, $statusCode, $headers, $body);
54
    }
55
}
56