Passed
Push — feature/coverage ( 34ed7c )
by Darío
02:51
created

GuzzleAdapter::request()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.1755

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
rs 9.9666
cc 4
nc 4
nop 1
crap 4.1755
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 GuzzleHttp\ClientInterface;
10
use GuzzleHttp\Exception\RequestException;
11
use GuzzleHttp\Exception\TransferException;
12
13
class GuzzleAdapter implements HttpClientAdapter
14
{
15
    protected ClientInterface $client;
16
17 15
    public function __construct(ClientInterface $client)
18
    {
19 15
        $this->client = $client;
20 15
    }
21
22 15
    public function request(HttpClientRequest $request): HttpClientResponse
23
    {
24
        try {
25 15
            $response = $this->client->request($request->getMethod(), $request->getUri(), $request->options());
26 4
        } catch (RequestException $exception) {
27 4
            if (! $exception->hasResponse()) {
28 2
                throw HttpClientException::fromThrowable($exception);
29
            }
30
31 2
            $response = $exception->getResponse();
32
        } catch (TransferException $exception) {
33
            throw HttpClientException::fromThrowable($exception);
34
        }
35
36 13
        return new GuzzleResponse($response);
37
    }
38
}
39