Passed
Push — master ( 7cd6c5...dd963e )
by Laurens
04:05
created

Client   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.12%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 47
ccs 16
cts 17
cp 0.9412
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getEndpoint() 0 7 1
A getUrl() 0 4 1
A getJson() 0 4 1
A get() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\KvkApi\Http\Adapter\Guzzle;
6
7
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
8
use GuzzleHttp\Exception\RequestException;
9
use Psr\Http\Message\ResponseInterface;
10
use Werkspot\KvkApi\Http\Adapter\Guzzle\Exception\Handler;
11
use Werkspot\KvkApi\Http\ClientInterface;
12
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
13
use Werkspot\KvkApi\Http\Search\QueryInterface;
14
15
final class Client implements ClientInterface
16
{
17
    /**
18
     * @var GuzzleClientInterface
19
     */
20
    private $guzzleClient;
21
22
    /**
23
     * @var MapperInterface
24
     */
25
    private $endpointMapper;
26
27 22
    public function __construct(
28
        GuzzleClientInterface $guzzleClient,
29
        MapperInterface $urlMapper
30
    ) {
31 22
        $this->guzzleClient = $guzzleClient;
32 22
        $this->endpointMapper = $urlMapper;
33 22
    }
34
35 19
    public function getEndpoint(string $endpoint, QueryInterface $searchQuery): ResponseInterface
36
    {
37 19
        return $this->get(
38 19
            $this->endpointMapper->map($endpoint),
39 19
            ['query' => $searchQuery->get()]
40
        );
41
    }
42
43 2
    public function getUrl(string $url): ResponseInterface
44
    {
45 2
        return $this->get($url);
46
    }
47
48 17
    public function getJson(ResponseInterface $response): string
49
    {
50 17
        return $response->getBody()->getContents();
51
    }
52
53 21
    private function get(string $url, ?array $options = [])
54
    {
55
        try {
56 21
            return $this->guzzleClient->get($url, $options);
57 3
        } catch (RequestException $exception) {
58 3
            Handler::handleRequestException($exception);
59
        }
60
    }
61
}
62