Passed
Pull Request — master (#45)
by Darío
05:30 queued 02:45
created

GuzzleAdapter::buildOptions()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.1582

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 32
ccs 14
cts 16
cp 0.875
rs 8.0555
cc 9
nc 64
nop 1
crap 9.1582
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