Passed
Pull Request — master (#19)
by Herberto
05:50 queued 01:43
created

KvkClient   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80.77%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 21
loc 63
ccs 21
cts 26
cp 0.8077
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProfile() 7 7 1
A getNextPage() 7 7 1
A getPreviousPage() 7 7 1
A decodeJsonToArray() 0 20 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Werkspot\KvkApi;
6
7
use function json_encode;
8
use Werkspot\KvkApi\Client\Factory\KvkPaginatorFactoryInterface;
9
use Werkspot\KvkApi\Client\KvkPaginator;
10
use Werkspot\KvkApi\Client\KvkPaginatorInterface;
11
use Werkspot\KvkApi\Exception\KvkApiException;
12
use Werkspot\KvkApi\Http\ClientInterface;
13
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
14
use Werkspot\KvkApi\Http\Search\QueryInterface;
15
16
final class KvkClient implements KvkClientInterface, KvkPaginatorAwareInterface
17
{
18
    /**
19
     * @var ClientInterface
20
     */
21
    private $httpClient;
22
23
    /**
24
     * @var KvkPaginatorFactoryInterface
25
     */
26
    private $profileResponseFactory;
27
28 21
    public function __construct(ClientInterface $httpClient, KvkPaginatorFactoryInterface $profileResponseFactory)
29
    {
30 21
        $this->httpClient = $httpClient;
31 21
        $this->profileResponseFactory = $profileResponseFactory;
32 21
    }
33
34 19 View Code Duplication
    public function getProfile(QueryInterface $profileQuery): KvkPaginatorInterface
35
    {
36 19
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROFILE, $profileQuery));
37 17
        $data = $this->decodeJsonToArray($json);
38
39 17
        return $this->profileResponseFactory->fromProfileData($data);
40
    }
41
42 1 View Code Duplication
    public function getNextPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
43
    {
44 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getNextUrl()));
45 1
        $data = $this->decodeJsonToArray($json);
46
47 1
        return $this->profileResponseFactory->fromProfileData($data);
48
    }
49
50 1 View Code Duplication
    public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
51
    {
52 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getPreviousUrl()));
53 1
        $data = $this->decodeJsonToArray($json);
54
55 1
        return $this->profileResponseFactory->fromProfileData($data);
56
    }
57
58 19
    private function decodeJsonToArray(string $json): array
59
    {
60 19
        $jsonPayload = json_decode($json, true);
61
62 19
        if (!isset($jsonPayload['data']) && !isset($jsonPayload['error'])) {
63
            throw new KvkApiException(
64
                "Unknown payload: \n"
65
                . $json
66
            );
67
        }
68
69 19
        if (!isset($jsonPayload['data'])) {
70
            throw new KvkApiException(
71
                $jsonPayload['error']['message'] . ': ' . $jsonPayload['error']['reason'],
72
                $jsonPayload['error']['code']
73
            );
74
        }
75
76 19
        return $jsonPayload['data'];
77
    }
78
}
79