Passed
Push — master ( a8eb2e...e64db9 )
by Darío
01:33 queued 13s
created

GuzzleAdapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 15
eloc 33
c 1
b 0
f 1
dl 0
loc 64
ccs 32
cts 32
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A request() 0 21 5
B buildOptions() 0 32 9
1
<?php
2
3
namespace EasyHttp\GuzzleLayer;
4
5
use EasyHttp\LayerContracts\Contracts\HttpClientAdapter;
6
use EasyHttp\LayerContracts\Contracts\HttpClientRequest;
7
use EasyHttp\LayerContracts\Contracts\HttpClientResponse;
8
use EasyHttp\LayerContracts\Exceptions\HttpClientException;
9
use EasyHttp\LayerContracts\Exceptions\HttpConnectionException;
10
use GuzzleHttp\ClientInterface;
11
use GuzzleHttp\Exception\ConnectException;
12
use GuzzleHttp\Exception\RequestException;
13
use GuzzleHttp\Exception\TransferException;
14
15
class GuzzleAdapter implements HttpClientAdapter
16
{
17
    protected ClientInterface $client;
18
19 21
    public function __construct(ClientInterface $client)
20
    {
21 21
        $this->client = $client;
22
    }
23
24 21
    public function request(HttpClientRequest $request): HttpClientResponse
25
    {
26
        try {
27 21
            $response = $this->client->request(
28 21
                $request->getMethod(),
29 21
                $request->getUri(),
30 21
                $this->buildOptions($request)
31
            );
32 9
        } catch (ConnectException $exception) {
33 1
            throw new HttpConnectionException($exception->getMessage(), (int) $exception->getCode(), $exception);
34 8
        } catch (RequestException $exception) {
35 7
            if (! $exception->hasResponse()) {
36 5
                throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception);
37
            }
38
39 2
            $response = $exception->getResponse();
40 1
        } catch (TransferException $exception) {
41 1
            throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception);
42
        }
43
44 14
        return new GuzzleResponse($response);
45
    }
46
47 21
    private function buildOptions(HttpClientRequest $request): array
48
    {
49
        $options = [
50 21
            'timeout' => $request->getTimeout(),
51 21
            'verify' => $request->isSSL()
52
        ];
53
54 21
        if ($request->hasHeaders()) {
55 2
            $options['headers'] = $request->getHeaders();
56
        }
57
58 21
        if ($request->hasJson()) {
59 3
            $options['json'] = $request->getJson();
60
        }
61
62 21
        if ($request->hasQuery()) {
63 3
            $options['query'] = $request->getQuery();
64
        }
65
66 21
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) {
67 3
            $options['cert'] = $request->getSecurityContext()->getCertificate();
68
        }
69
70 21
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasPrivateKey()) {
71 2
            $options['ssl_key'] = $request->getSecurityContext()->getPrivateKey();
72
        }
73
74 21
        if (count($request->getBasicAuth())) {
75 2
            $options['auth'] = $request->getBasicAuth();
76
        }
77
78 21
        return $options;
79
    }
80
}
81