Passed
Pull Request — master (#45)
by Darío
10:54 queued 07:25
created

GuzzleAdapter::buildOptions()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.0178

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 28
ccs 13
cts 14
cp 0.9286
rs 8.8333
cc 7
nc 32
nop 1
crap 7.0178
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 15
    public function __construct(ClientInterface $client)
20
    {
21 15
        $this->client = $client;
22
    }
23
24 15
    public function request(HttpClientRequest $request): HttpClientResponse
25
    {
26
        try {
27 15
            $response = $this->client->request(
28 15
                $request->getMethod(),
29 15
                $request->getUri(),
30 15
                $this->buildOptions($request)
31
            );
32 4
        } catch (ConnectException $exception) {
33
            throw new HttpConnectionException($exception->getMessage(), (int) $exception->getCode(), $exception);
34 4
        } 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
        } catch (TransferException $exception) {
41
            throw new HttpClientException($exception->getMessage(), (int) $exception->getCode(), $exception);
42
        }
43
44 13
        return new GuzzleResponse($response);
45
    }
46
47 15
    private function buildOptions(HttpClientRequest $request): array
48
    {
49
        $options = [
50 15
            'timeout' => $request->getTimeout(),
51 15
            'verify' => $request->isSSL()
52
        ];
53
54 15
        if ($request->hasHeaders()) {
55 2
            $options['headers'] = $request->getHeaders();
56
        }
57
58 15
        if ($request->hasJson()) {
59 1
            $options['json'] = $request->getJson();
60
        }
61
62 15
        if ($request->hasQuery()) {
63 3
            $options['query'] = $request->getQuery();
64
        }
65
66 15
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) {
67
            $options['cert'] = $request->getSecurityContext()->getCertificate();
68
        }
69
70 15
        if (count($request->getBasicAuth())) {
71 2
            $options['auth'] = $request->getBasicAuth();
72
        }
73
74 15
        return $options;
75
    }
76
}
77