1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Th3Mouk\VatLayer; |
6
|
|
|
|
7
|
|
|
use Fig\Http\Message\RequestMethodInterface; |
8
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
9
|
|
|
use Psr\Http\Client\ClientInterface; |
10
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Th3Mouk\VatLayer\Exception\Api; |
13
|
|
|
use Th3Mouk\VatLayer\Exception\BadResponse; |
14
|
|
|
use Th3Mouk\VatLayer\Response\ValidVatNumber; |
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
|
17
|
|
|
final class Psr18Wrapper implements VatLayer |
18
|
|
|
{ |
19
|
|
|
private const BASE_URL = 'http://apilayer.net/api/'; |
20
|
|
|
|
21
|
|
|
private string $access_key; |
22
|
|
|
private ClientInterface $client; |
23
|
|
|
private RequestFactoryInterface $request_factory; |
24
|
|
|
|
25
|
|
|
public function __construct(string $access_key, ClientInterface $client, RequestFactoryInterface $request_factory) |
26
|
|
|
{ |
27
|
|
|
$this->access_key = $access_key; |
28
|
|
|
$this->client = $client; |
29
|
|
|
$this->request_factory = $request_factory; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritDoc |
34
|
|
|
* @psalm-mutation-free |
35
|
|
|
*/ |
36
|
|
|
public function validate(string $vat_number): ValidVatNumber |
37
|
|
|
{ |
38
|
|
|
$request = $this->request_factory->createRequest(RequestMethodInterface::METHOD_GET, self::BASE_URL . "validate?access_key={$this->access_key}&vat_number={$vat_number}"); |
39
|
|
|
$response = $this->client->sendRequest($request); |
40
|
|
|
|
41
|
|
|
$this->handleResponse($response); |
42
|
|
|
|
43
|
|
|
return ValidVatNumber::createFromResponse($response); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @throws \Th3Mouk\VatLayer\Exception\VatLayer |
48
|
|
|
* |
49
|
|
|
* @psalm-mutation-free |
50
|
|
|
*/ |
51
|
|
|
private function handleResponse(ResponseInterface $response): void |
52
|
|
|
{ |
53
|
|
|
if ($response->getStatusCode() !== StatusCodeInterface::STATUS_OK) { |
54
|
|
|
throw BadResponse::dueToStatusCode($response); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
/** @var array<string, scalar|array{type: string, info: string, code: int}> $data */ |
59
|
|
|
$data = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR); |
60
|
|
|
|
61
|
|
|
if (isset($data['success']) && $data['success'] === false) { |
62
|
|
|
Assert::keyExists($data, 'error'); |
63
|
|
|
Assert::isArray($data['error']); |
64
|
|
|
Assert::keyExists($data['error'], 'type'); |
|
|
|
|
65
|
|
|
Assert::keyExists($data['error'], 'info'); |
66
|
|
|
Assert::keyExists($data['error'], 'code'); |
67
|
|
|
|
68
|
|
|
throw new Api($data['error']['type'] . ': ' . $data['error']['info'], $data['error']['code']); |
69
|
|
|
} |
70
|
|
|
} catch (\JsonException | \InvalidArgumentException $exception) { |
71
|
|
|
throw BadResponse::dueToInvalidResponseFormat($response, $exception); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|