Skip to content

Commit 1d1d533

Browse files
Merge pull request #12 from roelvanduijnhoven/lazy-initialization
Lazy instantiate VIES SOAP client. Thanks @roelvanduijnhoven
2 parents ffc5cd5 + 37cbe08 commit 1d1d533

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/Validator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ public function validateExistence($vatNumber) {
9999
* @param string $vatNumber Either the full VAT number (incl. country) or just the part after the country code.
100100
*
101101
* @return boolean
102+
*
103+
* @throws Vies\ViesException
102104
*/
103105
public function validate( $vatNumber ) {
104106
return $this->validateFormat( $vatNumber ) && $this->validateExistence( $vatNumber );

src/Vies/Client.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ class Client {
1515
/**
1616
* @var SoapClient
1717
*/
18-
protected $client;
18+
private $client;
19+
20+
/**
21+
* @var int
22+
*/
23+
protected $timeout;
1924

2025
/**
2126
* Client constructor.
2227
*
2328
* @param int $timeout How long should we wait before aborting the request to VIES?
2429
*/
2530
public function __construct($timeout = 10) {
26-
$this->client = new SoapClient( self::URL, [ 'connection_timeout' => $timeout ]);
31+
$this->timeout = $timeout;
2732
}
2833

2934
/**
@@ -36,7 +41,7 @@ public function __construct($timeout = 10) {
3641
*/
3742
public function checkVat( $countryCode, $vatNumber ) {
3843
try {
39-
$response = $this->client->checkVat(
44+
$response = $this->getClient()->checkVat(
4045
array(
4146
'countryCode' => $countryCode,
4247
'vatNumber' => $vatNumber
@@ -48,4 +53,16 @@ public function checkVat( $countryCode, $vatNumber ) {
4853

4954
return (bool) $response->valid;
5055
}
56+
57+
/**
58+
* @return SoapClient
59+
*/
60+
protected function getClient()
61+
{
62+
if ($this->client === null) {
63+
$this->client = new SoapClient(self::URL, ['connection_timeout' => $this->timeout]);
64+
}
65+
66+
return $this->client;
67+
}
5168
}

0 commit comments

Comments
 (0)