|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Werkspot\KvkApi; |
|
6
|
|
|
|
|
7
|
|
|
use Werkspot\KvkApi\Api\KvkResponse; |
|
8
|
|
|
use Werkspot\KvkApi\Api\ResponseInterface; |
|
9
|
|
|
use Werkspot\KvkApi\Client\Factory\ProfileResponseFactoryInterface; |
|
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 ProfileResponseFactoryInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $profileResponseFactory; |
|
25
|
|
|
|
|
26
|
21 |
|
public function __construct(ClientInterface $httpClient, ProfileResponseFactoryInterface $profileResponseFactory) |
|
27
|
|
|
{ |
|
28
|
21 |
|
$this->httpClient = $httpClient; |
|
29
|
21 |
|
$this->profileResponseFactory = $profileResponseFactory; |
|
30
|
21 |
|
} |
|
31
|
|
|
|
|
32
|
19 |
|
public function getProfile(ProfileQuery $profileQuery): KvkResponse |
|
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(ResponseInterface $response): ResponseInterface |
|
41
|
|
|
{ |
|
42
|
1 |
|
$json = $this->httpClient->getJson($this->httpClient->getUrl($response->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(ResponseInterface $response): ResponseInterface |
|
49
|
|
|
{ |
|
50
|
1 |
|
$json = $this->httpClient->getJson($this->httpClient->getUrl($response->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
|
|
|
|