Passed
Pull Request — master (#50)
by Darío
12:22 queued 08:51
created

GuzzleAdapter::request()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.4888
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 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 22
    public function __construct(ClientInterface $client)
20
    {
21 22
        $this->client = $client;
22
    }
23
24 22
    public function request(HttpClientRequest $request): HttpClientResponse
25
    {
26
        try {
27 22
            $response = $this->client->request(
28 22
                $request->getMethod(),
29 22
                $request->getUri(),
30 22
                $this->buildOptions($request)
31 22
            );
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 15
        return new GuzzleResponse($response);
45
    }
46
47 22
    private function buildOptions(HttpClientRequest $request): array
48
    {
49 22
        $options = [
50 22
            'timeout' => $request->getTimeout(),
51 22
            'verify' => $request->isSSL()
52 22
        ];
53
54 22
        $this
55 22
            ->setHeaders($request, $options)
56 22
            ->setJson($request, $options)
57 22
            ->setQuery($request, $options)
58 22
            ->setUrlEncodedData($request, $options)
59 22
            ->setSecurityContext($request, $options)
60 22
            ->setBasicAuth($request, $options);
61
62 22
        return $options;
63
    }
64
65 22
    private function setHeaders(HttpClientRequest $request, &$options): self
66
    {
67 22
        if ($request->hasHeaders()) {
68 2
            $options['headers'] = $request->getHeaders();
69
        }
70
71 22
        return $this;
72
    }
73
74 22
    private function setJson(HttpClientRequest $request, &$options): self
75
    {
76 22
        if ($request->hasJson()) {
77 3
            $options['json'] = $request->getJson();
78
        }
79
80 22
        return $this;
81
    }
82
83 22
    private function setQuery(HttpClientRequest $request, &$options): self
84
    {
85 22
        if ($request->hasQuery()) {
86 3
            $options['query'] = $request->getQuery();
87
        }
88
89 22
        return $this;
90
    }
91
92 22
    private function setSecurityContext(HttpClientRequest $request, &$options): self
93
    {
94 22
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasCertificate()) {
95 3
            $options['cert'] = $request->getSecurityContext()->getCertificate();
96
        }
97
98 22
        if ($request->hasSecurityContext() && $request->getSecurityContext()->hasPrivateKey()) {
99 2
            $options['ssl_key'] = $request->getSecurityContext()->getPrivateKey();
100
        }
101
102 22
        return $this;
103
    }
104
105 22
    private function setBasicAuth(HttpClientRequest $request, &$options): self
106
    {
107 22
        if (count($request->getBasicAuth())) {
108 2
            $options['auth'] = $request->getBasicAuth();
109
        }
110
111 22
        return $this;
112
    }
113
114 22
    private function setUrlEncodedData(HttpClientRequest $request, array &$options): self
115
    {
116 22
        if ($request->hasUrlEncodedData()) {
0 ignored issues
show
Bug introduced by
The method hasUrlEncodedData() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest. It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        if ($request->/** @scrutinizer ignore-call */ hasUrlEncodedData()) {
Loading history...
117 1
            $options['form_params'] = $request->getUrlEncodedData();
0 ignored issues
show
Bug introduced by
The method getUrlEncodedData() does not exist on EasyHttp\LayerContracts\...racts\HttpClientRequest. It seems like you code against a sub-type of EasyHttp\LayerContracts\...racts\HttpClientRequest such as EasyHttp\GuzzleLayer\GuzzleRequest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
            /** @scrutinizer ignore-call */ 
118
            $options['form_params'] = $request->getUrlEncodedData();
Loading history...
118
        }
119
120 22
        return $this;
121
    }
122
}
123