dmt-software /
eu-vat-validation
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DMT\VatServiceEu\Handler; |
||
| 4 | |||
| 5 | use DMT\Http\Client\RequestHandlerInterface; |
||
| 6 | use DMT\VatServiceEu\Request\CheckVat; |
||
| 7 | use DMT\VatServiceEu\Request\CheckVatApprox; |
||
| 8 | use DMT\VatServiceEu\Request\RequestInterface; |
||
| 9 | use DMT\VatServiceEu\Response\CheckVatApproxResponse; |
||
| 10 | use DMT\VatServiceEu\Response\CheckVatResponse; |
||
| 11 | use DMT\VatServiceEu\Response\ResponseInterface; |
||
| 12 | use GuzzleHttp\Client; |
||
| 13 | use JMS\Serializer\SerializerInterface; |
||
| 14 | use Psr\Http\Message\RequestFactoryInterface; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Class CheckVatHandler |
||
| 18 | * |
||
| 19 | * Manages the request to the VIES VAT validation web service. |
||
| 20 | */ |
||
| 21 | class CheckVatHandler |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var RequestHandlerInterface |
||
| 25 | */ |
||
| 26 | protected $requestHandler; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var SerializerInterface |
||
| 30 | */ |
||
| 31 | protected $serializer; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var RequestFactoryInterface |
||
| 35 | */ |
||
| 36 | protected $requestFactory; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * CheckVatHandler constructor. |
||
| 40 | * |
||
| 41 | * @param RequestHandlerInterface $httpClient |
||
| 42 | * @param SerializerInterface $serializer |
||
| 43 | */ |
||
| 44 | 25 | public function __construct( |
|
| 45 | RequestHandlerInterface $httpClient, |
||
| 46 | SerializerInterface $serializer, |
||
| 47 | RequestFactoryInterface $requestFactory |
||
| 48 | ) |
||
| 49 | { |
||
| 50 | 25 | $this->requestHandler = $httpClient; |
|
| 51 | 25 | $this->serializer = $serializer; |
|
| 52 | 25 | $this->requestFactory = $requestFactory; |
|
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Check a VAT number against the VIES VAT validation service. |
||
| 57 | * |
||
| 58 | * @param CheckVat $checkVat |
||
| 59 | * @return CheckVatResponse |
||
| 60 | */ |
||
| 61 | 12 | public function handleCheckVat(CheckVat $checkVat): CheckVatResponse |
|
| 62 | { |
||
| 63 | 12 | return $this->execute($checkVat, CheckVatResponse::class); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Check a VAT number against the VIES VAT validation service. |
||
| 68 | * |
||
| 69 | * @param CheckVatApprox $checkVatApprox |
||
| 70 | * @return CheckVatApproxResponse |
||
| 71 | */ |
||
| 72 | 12 | public function handleCheckVatApprox(CheckVatApprox $checkVatApprox): CheckVatApproxResponse |
|
| 73 | { |
||
| 74 | 12 | return $this->execute($checkVatApprox, CheckVatApproxResponse::class); |
|
|
0 ignored issues
–
show
|
|||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Execute the request on the VIES VAT web service. |
||
| 79 | * |
||
| 80 | * @param RequestInterface $request |
||
| 81 | * @param string $responseClass |
||
| 82 | * @return CheckVatResponse|CheckVatApproxResponse |
||
| 83 | */ |
||
| 84 | 24 | protected function execute(RequestInterface $request, string $responseClass): ResponseInterface |
|
| 85 | { |
||
| 86 | 24 | $httpRequest = $this->requestFactory |
|
| 87 | 24 | ->createRequest( |
|
| 88 | 24 | 'POST', |
|
| 89 | 24 | 'https://ec.europa.eu/taxation_customs/vies/services/checkVatService' |
|
| 90 | 24 | ) |
|
| 91 | 24 | ->withHeader('Content-Type', 'text/xml; charset=utf-8') |
|
| 92 | 24 | ->withHeader( 'SOAPAction', '""'); |
|
| 93 | 24 | $httpRequest->getBody()->write($this->serializer->serialize($request, 'soap')); |
|
| 94 | |||
| 95 | 24 | $httpResponse = $this->requestHandler->handle($httpRequest); |
|
| 96 | |||
| 97 | 24 | return $this->serializer->deserialize($httpResponse->getBody(), $responseClass, 'soap'); |
|
| 98 | } |
||
| 99 | } |
||
| 100 |