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

KvkClient   A

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