Test Failed
Pull Request — master (#20)
by
unknown
02:18
created

KvkClient   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 36.84 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

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

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 23
    public function __construct(ClientInterface $httpClient, KvkPaginatorFactoryInterface $profileResponseFactory)
27
    {
28 23
        $this->httpClient = $httpClient;
29 23
        $this->profileResponseFactory = $profileResponseFactory;
30 23
    }
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 1
    /**
41
     * Execute search query
42 1
     * @throws KvkApiException
43 1
     * @author Patrick Development <[email protected]>
44
     */
45 1 View Code Duplication
    public function fetchSearch(QueryInterface $profileQuery): KvkPaginatorInterface
46
    {
47
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::SEARCH, $profileQuery));
48 1
        $data = $this->decodeJsonToArray($json);
49
50 1
        return $this->profileResponseFactory->fromProfileData($data);
51 1
    }
52
53 1 View Code Duplication
    public function getNextPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
54
    {
55
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getNextUrl()));
56 21
        $data = $this->decodeJsonToArray($json);
57
58 21
        return $this->profileResponseFactory->fromProfileData($data);
59
    }
60 21
61 1 View Code Duplication
    public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
62
    {
63 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getPreviousUrl()));
64
        $data = $this->decodeJsonToArray($json);
65
66
        return $this->profileResponseFactory->fromProfileData($data);
67 20
    }
68 1
69 1
    private function decodeJsonToArray(string $json): array
70 1
    {
71
        $jsonPayload = json_decode($json, true);
72
73
        if (!isset($jsonPayload['data']) && !isset($jsonPayload['error'])) {
74 19
            throw new KvkApiException(
75
                "Unknown payload: \n"
76
                . $json
77
            );
78
        }
79
80
        if (!isset($jsonPayload['data'])) {
81
            throw new KvkApiException(
82
                $jsonPayload['error']['message'] . ': ' . $jsonPayload['error']['reason'],
83
                $jsonPayload['error']['code']
84
            );
85
        }
86
87
        return $jsonPayload['data'];
88
    }
89
}
90