Passed
Push — master ( 7cd6c5...dd963e )
by Laurens
04:05
created

KvkClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 44.68 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 21
loc 47
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

5 Methods

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

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\KvkPaginator;
9
use Werkspot\KvkApi\Client\KvkPaginatorInterface;
10
use Werkspot\KvkApi\Http\ClientInterface;
11
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
12
use Werkspot\KvkApi\Http\Search\ProfileQuery;
13
14
final class KvkClient
15
{
16
    /**
17
     * @var ClientInterface
18
     */
19
    private $httpClient;
20
21
    /**
22
     * @var KvkPaginatorFactoryInterface
23
     */
24
    private $profileResponseFactory;
25
26 21
    public function __construct(ClientInterface $httpClient, KvkPaginatorFactoryInterface $profileResponseFactory)
27
    {
28 21
        $this->httpClient = $httpClient;
29 21
        $this->profileResponseFactory = $profileResponseFactory;
30 21
    }
31
32 19 View Code Duplication
    public function getProfile(ProfileQuery $profileQuery): KvkPaginatorInterface
33
    {
34 19
        $json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROFILE, $profileQuery));
35 17
        $data = $this->decodeJsonToArray($json);
36
37 17
        return $this->profileResponseFactory->fromProfileData($data);
38
    }
39
40 1 View Code Duplication
    public function getNextPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
41
    {
42 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getNextUrl()));
43 1
        $data = $this->decodeJsonToArray($json);
44
45 1
        return $this->profileResponseFactory->fromProfileData($data);
46
    }
47
48 1 View Code Duplication
    public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface
49
    {
50 1
        $json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getPreviousUrl()));
51 1
        $data = $this->decodeJsonToArray($json);
52
53 1
        return $this->profileResponseFactory->fromProfileData($data);
54
    }
55
56 19
    private function decodeJsonToArray(string $json): array
57
    {
58 19
        return json_decode($json, true)['data'];
59
    }
60
}
61