From 0c4973f3a1bdd9d850768075f63a30b2b4f38825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Tue, 26 Jul 2022 02:22:22 +0200 Subject: [PATCH] Add SoapClient timeout using a stream context option Close #37 --- src/VatCalculator.php | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/VatCalculator.php b/src/VatCalculator.php index 4f986b3..dbf6e5b 100644 --- a/src/VatCalculator.php +++ b/src/VatCalculator.php @@ -33,8 +33,11 @@ class VatCalculator /** @var string */ private $businessVatNumber; + /** @var float */ + private $timeout; - public function __construct(VatRates $vatRates, ?string $businessCountryCode = null, ?string $businessVatNumber = null) + + public function __construct(VatRates $vatRates, ?string $businessCountryCode = null, ?string $businessVatNumber = null, ?float $timeout = null) { $this->vatRates = $vatRates; if ($businessCountryCode) { @@ -43,6 +46,7 @@ public function __construct(VatRates $vatRates, ?string $businessCountryCode = n if ($businessVatNumber) { $this->setBusinessVatNumber($businessVatNumber); } + $this->timeout = $timeout ?? (float)ini_get('default_socket_timeout'); } @@ -172,7 +176,16 @@ public function getVatDetails(string $vatNumber, ?string $requesterVatNumber = n try { if ($this->soapClient === null) { - $this->soapClient = new SoapClient(self::VAT_SERVICE_URL); + $this->soapClient = new SoapClient( + self::VAT_SERVICE_URL, + [ + 'stream_context' => stream_context_create([ + 'http' => [ + 'timeout' => $this->timeout, + ], + ]), + ], + ); } if ($requesterVatNumber === null) { $requesterVatNumber = $this->businessVatNumber;