Failed Conditions
Pull Request — master (#8)
by Laurens
05:05
created

KvkClient   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 27.45 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getProfile() 0 7 1
A getNextPage() 7 7 1
A getPreviousPage() 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\KvkPaginator;
8
use Werkspot\KvkApi\Client\KvkPaginatorInterface;
9
use Werkspot\KvkApi\Client\Factory\KvkPaginatorFactoryInterface;
10
use Werkspot\KvkApi\Client\Search\ProfileQuery;
11
use Werkspot\KvkApi\Http\ClientInterface;
12
use Werkspot\KvkApi\Http\Endpoint\MapperInterface;
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
    public function getProfile(ProfileQuery $profileQuery): KvkPaginator
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
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    /**
57
     * @param $json
58
     * @return mixed
59
     */
60 19
    private function decodeJsonToArray($json)
61
    {
62 19
        return json_decode($json, true)['data'];
63
    }
64
}
65