Test Failed
Pull Request — master (#20)
by
unknown
05:08
created

KvkClient::fetchSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
nc 1
nop 1
crap 1
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