1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Werkspot\KvkApi; |
6
|
|
|
|
7
|
|
|
use function json_encode; |
8
|
|
|
use Werkspot\KvkApi\Client\Factory\KvkPaginatorFactoryInterface; |
9
|
|
|
use Werkspot\KvkApi\Client\KvkPaginator; |
10
|
|
|
use Werkspot\KvkApi\Client\KvkPaginatorInterface; |
11
|
|
|
use Werkspot\KvkApi\Exception\KvkApiException; |
12
|
|
|
use Werkspot\KvkApi\Http\ClientInterface; |
13
|
|
|
use Werkspot\KvkApi\Http\Endpoint\MapperInterface; |
14
|
|
|
use Werkspot\KvkApi\Http\Search\QueryInterface; |
15
|
|
|
|
16
|
|
|
final class KvkClient implements KvkClientInterface, KvkPaginatorAwareInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ClientInterface |
20
|
|
|
*/ |
21
|
|
|
private $httpClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var KvkPaginatorFactoryInterface |
25
|
|
|
*/ |
26
|
|
|
private $profileResponseFactory; |
27
|
|
|
|
28
|
21 |
|
public function __construct(ClientInterface $httpClient, KvkPaginatorFactoryInterface $profileResponseFactory) |
29
|
|
|
{ |
30
|
21 |
|
$this->httpClient = $httpClient; |
31
|
21 |
|
$this->profileResponseFactory = $profileResponseFactory; |
32
|
21 |
|
} |
33
|
|
|
|
34
|
19 |
View Code Duplication |
public function getProfile(QueryInterface $profileQuery): KvkPaginatorInterface |
35
|
|
|
{ |
36
|
19 |
|
$json = $this->httpClient->getJson($this->httpClient->getEndpoint(MapperInterface::PROFILE, $profileQuery)); |
37
|
17 |
|
$data = $this->decodeJsonToArray($json); |
38
|
|
|
|
39
|
17 |
|
return $this->profileResponseFactory->fromProfileData($data); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
View Code Duplication |
public function getNextPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface |
43
|
|
|
{ |
44
|
1 |
|
$json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getNextUrl())); |
45
|
1 |
|
$data = $this->decodeJsonToArray($json); |
46
|
|
|
|
47
|
1 |
|
return $this->profileResponseFactory->fromProfileData($data); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
View Code Duplication |
public function getPreviousPage(KvkPaginatorInterface $kvkPaginator): KvkPaginatorInterface |
51
|
|
|
{ |
52
|
1 |
|
$json = $this->httpClient->getJson($this->httpClient->getUrl($kvkPaginator->getPreviousUrl())); |
53
|
1 |
|
$data = $this->decodeJsonToArray($json); |
54
|
|
|
|
55
|
1 |
|
return $this->profileResponseFactory->fromProfileData($data); |
56
|
|
|
} |
57
|
|
|
|
58
|
19 |
|
private function decodeJsonToArray(string $json): array |
59
|
|
|
{ |
60
|
19 |
|
$jsonPayload = json_decode($json, true); |
61
|
|
|
|
62
|
19 |
|
if (!isset($jsonPayload['data']) && !isset($jsonPayload['error'])) { |
63
|
|
|
throw new KvkApiException( |
64
|
|
|
"Unknown payload: \n" |
65
|
|
|
. $json |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
19 |
|
if (!isset($jsonPayload['data'])) { |
70
|
|
|
throw new KvkApiException( |
71
|
|
|
$jsonPayload['error']['message'] . ': ' . $jsonPayload['error']['reason'], |
72
|
|
|
$jsonPayload['error']['code'] |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
19 |
|
return $jsonPayload['data']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|