|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Werkspot\KvkApi\Http\Adapter\Guzzle; |
|
6
|
|
|
|
|
7
|
|
|
use GuzzleHttp\ClientInterface as GuzzleClientInterface; |
|
8
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Werkspot\KvkApi\Http\Adapter\Guzzle\Exception\Handler; |
|
11
|
|
|
use Werkspot\KvkApi\Http\ClientInterface; |
|
12
|
|
|
use Werkspot\KvkApi\Http\Endpoint\MapperInterface; |
|
13
|
|
|
use Werkspot\KvkApi\Http\Search\QueryInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class Client implements ClientInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var GuzzleClientInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
private $guzzleClient; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var MapperInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $endpointMapper; |
|
26
|
|
|
|
|
27
|
22 |
|
public function __construct( |
|
28
|
|
|
GuzzleClientInterface $guzzleClient, |
|
29
|
|
|
MapperInterface $urlMapper |
|
30
|
|
|
) { |
|
31
|
22 |
|
$this->guzzleClient = $guzzleClient; |
|
32
|
22 |
|
$this->endpointMapper = $urlMapper; |
|
33
|
22 |
|
} |
|
34
|
|
|
|
|
35
|
19 |
|
public function getEndpoint(string $endpoint, QueryInterface $searchQuery): ResponseInterface |
|
36
|
|
|
{ |
|
37
|
19 |
|
return $this->get( |
|
38
|
19 |
|
$this->endpointMapper->map($endpoint), |
|
39
|
19 |
|
['query' => $searchQuery->get()] |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
public function getUrl(string $url): ResponseInterface |
|
44
|
|
|
{ |
|
45
|
2 |
|
return $this->get($url); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
17 |
|
public function getJson(ResponseInterface $response): string |
|
49
|
|
|
{ |
|
50
|
17 |
|
return $response->getBody()->getContents(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
21 |
|
private function get(string $url, ?array $options = []) |
|
54
|
|
|
{ |
|
55
|
|
|
try { |
|
56
|
21 |
|
return $this->guzzleClient->get($url, $options); |
|
57
|
3 |
|
} catch (RequestException $exception) { |
|
58
|
3 |
|
Handler::handleRequestException($exception); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|