KvkClient   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 37.33 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 28
loc 75
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProfile() 7 7 1
A fetchSearch() 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 Werkspot\KvkApi\Client\Factory\KvkPaginatorFactoryInterface;
8
use Werkspot\KvkApi\Client\KvkPaginatorInterface;
9
use Werkspot\KvkApi\Exception\KvkApiException;
10
use Werkspot\KvkApi\Http\ClientInterface;
11
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
12
use Werkspot\KvkApi\Http\Search\QueryInterface;
13
14
final class KvkClient implements KvkClientInterface, KvkPaginatorAwareInterface
15
{
16
    /**
17
     * @var ClientInterface
18
     */
19
    private $httpClient;
20
21
    /**
22
     * @var KvkPaginatorFactoryInterface
23
     */
24
    private $profileResponseFactory;
25
26 39
    public function __construct(ClientInterface $httpClient, KvkPaginatorFactoryInterface $profileResponseFactory)
27
    {
28 39
        $this->httpClient = $httpClient;
29 39
        $this->profileResponseFactory = $profileResponseFactory;
30 39
    }
31
32 21 View Code Duplication
    public function getProfile(QueryInterface $profileQuery): KvkPaginatorInterface
33
    {
34 21
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROFILE, $profileQuery));
35 19
        $data = $this->decodeJsonToArray($json);
36
37 17
        return $this->profileResponseFactory->fromProfileData($data);
38
    }
39
40
    /**
41
     * Execute search query
42
     * @author Patrick Development <[email protected]>
43
     */
44 16 View Code Duplication
    public function fetchSearch(QueryInterface $profileQuery): KvkPaginatorInterface
45
    {
46 16
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::SEARCH, $profileQuery));
47 16
        $data = $this->decodeJsonToArray($json);
48
49 16
        return $this->profileResponseFactory->fromProfileData($data);
50
    }
51
52 1 View Code Duplication
    public function getNextPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
53
    {
54 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getNextUrl()));
55 1
        $data = $this->decodeJsonToArray($json);
56
57 1
        return $this->profileResponseFactory->fromProfileData($data);
58
    }
59
60 1 View Code Duplication
    public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
61
    {
62 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getPreviousUrl()));
63 1
        $data = $this->decodeJsonToArray($json);
64
65 1
        return $this->profileResponseFactory->fromProfileData($data);
66
    }
67
68 37
    private function decodeJsonToArray(string $json): array
69
    {
70 37
        $jsonPayload = json_decode($json, true);
71
72 37
        if (!isset($jsonPayload['data']) && !isset($jsonPayload['error'])) {
73 1
            throw new KvkApiException(
74
                "Unknown payload: \n"
75 1
                . $json
76
            );
77
        }
78
79 36
        if (!isset($jsonPayload['data'])) {
80 1
            throw new KvkApiException(
81 1
                $jsonPayload['error']['message'] . ': ' . $jsonPayload['error']['reason'],
82 1
                $jsonPayload['error']['code']
83
            );
84
        }
85
86 35
        return $jsonPayload['data'];
87
    }
88
}
89