Passed
Push — feature/update-to-layer-contra... ( 101177...e253ec )
by Darío
02:54
created

GuzzleAdapter   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 93.75%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B buildOptions() 0 32 9
A request() 0 21 5
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 17
    public function __construct(ClientInterface $client)
20
    {
21 17
        $this->client = $client;
22
    }
23
24 17
    public function request(HttpClientRequest $request): HttpClientResponse
25
    {
26
        try {
27 17
            $response = $this->client->request(
28 17
                $request->getMethod(),
29 17
                $request->getUri(),
30 17
                $this->buildOptions($request)
31
            );
32 6
        } catch (ConnectException $exception) {
33 1
            throw new HttpConnectionException($exception->getMessage(), (int) $exception->getCode(), $exception);
34 5
        } catch (RequestException $exception) {
35 4
            if (! $exception->hasResponse()) {
36 2
                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 13
        return new GuzzleResponse($response);
45
    }
46
47 17
    private function buildOptions(HttpClientRequest $request): array
48
    {
49
        $options = [
50 17
            'timeout' => $request->getTimeout(),
51 17
            'verify' => $request->isSSL()
52
        ];
53
54 17
        if ($request->hasHeaders()) {
55 2
            $options['headers'] = $request->getHeaders();
56
        }
57
58 17
        if ($request->hasJson()) {
59 1
            $options['json'] = $request->getJson();
60
        }
61
62 17
        if ($request->hasQuery()) {
63 3
            $options['query'] = $request->getQuery();
64
        }
65
66 17
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) {
67
            $options['cert'] = $request->getSecurityContext()->getCertificate();
68
        }
69
70 17
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasPrivateKey()) {
71
            $options['ssl_key'] = $request->getSecurityContext()->getPrivateKey();
72
        }
73
74 17
        if (count($request->getBasicAuth())) {
75 2
            $options['auth'] = $request->getBasicAuth();
76
        }
77
78 17
        return $options;
79
    }
80
}
81