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