Client   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 81
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A checkVat() 0 13 2
A getSoapClient() 0 15 2
1
<?php
2
3
namespace Ddeboer\Vatin\Vies;
4
5
use SoapFault;
6
use Ddeboer\Vatin\Exception\ViesException;
7
8
/**
9
 * A client for the VIES SOAP web service
10
 */
11
class Client
12
{
13
    /**
14
     * URL to WSDL
15
     *
16
     * @var string
17
     */
18
    private $wsdl = 'https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
19
20
    /**
21
     * SOAP client
22
     *
23
     * @var \SoapClient
24
     */
25
    private $soapClient;
26
27
    /**
28
     * SOAP classmap
29
     *
30
     * @var array
31
     */
32
    private $classmap = [
33
        'checkVatResponse' => 'Ddeboer\Vatin\Vies\Response\CheckVatResponse'
34
    ];
35
36
    /**
37
     * Constructor
38
     *
39
     * @param string|null $wsdl URL to WSDL
40
     */
41 2
    public function __construct($wsdl = null)
42
    {
43 2
        if ($wsdl) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $wsdl of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
44 1
            $this->wsdl = $wsdl;
45
        }
46 2
    }
47
48
    /**
49
     * Check VAT
50
     *
51
     * @param string $countryCode Country code
52
     * @param string $vatNumber   VAT number
53
     *
54
     * @return Response\CheckVatResponse
55
     * @throws ViesException
56
     */
57 2
    public function checkVat($countryCode, $vatNumber)
58
    {
59
        try {
60 2
            return $this->getSoapClient()->checkVat(
61
                [
62 1
                    'countryCode' => $countryCode,
63 1
                    'vatNumber' => $vatNumber
64
                ]
65
            );
66 1
        } catch (SoapFault $e) {
67 1
            throw new ViesException('Error communicating with VIES service', 0, $e);
68
        }
69
    }
70
71
    /**
72
     * Get SOAP client
73
     *
74
     * @return \SoapClient
75
     */
76 2
    private function getSoapClient()
77
    {
78 2
        if (null === $this->soapClient) {
79 2
            $this->soapClient = new \SoapClient(
80 2
                $this->wsdl,
81
                [
82 2
                    'classmap' => $this->classmap,
83 2
                    'user_agent' => 'Mozilla', // the request fails unless a (dummy) user agent is specified
84
                    'exceptions' => true,
85
                ]
86
            );
87
        }
88
89 1
        return $this->soapClient;
90
    }
91
}
92