Failed Conditions
Pull Request — master (#20)
by
unknown
01:47
created

KvkClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 35.9 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 28
loc 78
ccs 26
cts 30
cp 0.8667
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 getSearch() 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 23
    public function __construct(ClientInterface $httpClient, KvkPaginatorFactoryInterface $profileResponseFactory)
29
    {
30 23
        $this->httpClient = $httpClient;
31 23
        $this->profileResponseFactory = $profileResponseFactory;
32 23
    }
33
34 21 View Code Duplication
    public function getProfile(QueryInterface $profileQuery): KvkPaginatorInterface
35
    {
36 21
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROFILE, $profileQuery));
37 19
        $data = $this->decodeJsonToArray($json);
38
39 17
        return $this->profileResponseFactory->fromProfileData($data);
40
    }
41
42
    /**
43
     * Execute search query
44
     * @param QueryInterface $profileQuery
45
     * @return KvkPaginatorInterface
46
     * @throws KvkApiException
47
     * @author Patrick Development <[email protected]>
48
     */
49 View Code Duplication
    public function getSearch(QueryInterface $profileQuery): KvkPaginatorInterface
50
    {
51
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::SEARCH, $profileQuery));
52
        $data = $this->decodeJsonToArray($json);
53
54
        return $this->profileResponseFactory->fromProfileData($data);
55
    }
56
57 1 View Code Duplication
    public function getNextPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
58
    {
59 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getNextUrl()));
60 1
        $data = $this->decodeJsonToArray($json);
61
62 1
        return $this->profileResponseFactory->fromProfileData($data);
63
    }
64
65 1 View Code Duplication
    public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
66
    {
67 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getPreviousUrl()));
68 1
        $data = $this->decodeJsonToArray($json);
69
70 1
        return $this->profileResponseFactory->fromProfileData($data);
71
    }
72
73 21
    private function decodeJsonToArray(string $json): array
74
    {
75 21
        $jsonPayload = json_decode($json, true);
76
77 21
        if (!isset($jsonPayload['data']) && !isset($jsonPayload['error'])) {
78 1
            throw new KvkApiException(
79
                "Unknown payload: \n"
80 1
                . $json
81
            );
82
        }
83
84 20
        if (!isset($jsonPayload['data'])) {
85 1
            throw new KvkApiException(
86 1
                $jsonPayload['error']['message'] . ': ' . $jsonPayload['error']['reason'],
87 1
                $jsonPayload['error']['code']
88
            );
89
        }
90
91 19
        return $jsonPayload['data'];
92
    }
93
}
94