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

KvkClient::fetchSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 10
c 0
b 0
f 0
ccs 4
cts 4
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
     * @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