Failed Conditions
Pull Request — master (#8)
by Laurens
05: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 GuzzleHttp\TransferStats;
10
use Psr\Http\Message\ResponseInterface;
11
use Werkspot\KvkApi\Client\Search\QueryInterface;
12
use Werkspot\KvkApi\Http\Adapter\Guzzle\Exception\Handler;
13
use Werkspot\KvkApi\Http\ClientInterface;
14
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
15
16
final class Client implements ClientInterface
17
{
18
    /**
19
     * @var GuzzleClientInterface
20
     */
21
    private $guzzleClient;
22
23
    /**
24
     * @var MapperInterface
25
     */
26
    private $endpointMapper;
27
28 22
    public function __construct(
29
        GuzzleClientInterface $guzzleClient,
30
        MapperInterface $urlMapper
31
    ) {
32 22
        $this->guzzleClient = $guzzleClient;
33 22
        $this->endpointMapper = $urlMapper;
34 22
    }
35
36 19
    public function getEndpoint(string $endpoint, QueryInterface $searchQuery): ResponseInterface
37
    {
38 19
        return $this->get(
39 19
            $this->endpointMapper->map($endpoint),
40 19
            ['query' => $searchQuery->get()]
41
        );
42
    }
43
44 2
    public function getUrl(string $url): ResponseInterface
45
    {
46 2
        return $this->get($url);
47
    }
48
49 17
    public function getJson(ResponseInterface $response): string
50
    {
51 17
        return $response->getBody()->getContents();
52
    }
53
54 21
    private function get(string $url, ?array $options = [])
55
    {
56
        try {
57 21
            return $this->guzzleClient->get($url, $options);
58 3
        } catch (RequestException $exception) {
59 3
            Handler::handleRequestException($exception);
60
        }
61
    }
62
}
63