|
1
|
|
|
<?php |
|
2
|
|
|
namespace VatNumberCheck\Soap\Soap; |
|
3
|
|
|
|
|
4
|
|
|
use SoapClient; |
|
5
|
|
|
use Cake\Core\Configure; |
|
6
|
|
|
use Cake\Core\Exception\Exception; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* SOAP Client Wrapper. |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class Soap { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Description. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
public $description = 'Soap Client Wrapper'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* SoapClient instance. |
|
23
|
|
|
* |
|
24
|
|
|
* @var SoapClient|null |
|
25
|
|
|
*/ |
|
26
|
|
|
public $client = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Connection status. |
|
30
|
|
|
* |
|
31
|
|
|
* @var bool |
|
32
|
|
|
*/ |
|
33
|
|
|
public $connected = false; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Default configuration. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $_baseConfig = [ |
|
41
|
|
|
'wsdl' => null, |
|
42
|
|
|
'location' => '', |
|
43
|
|
|
'uri' => '', |
|
44
|
|
|
'login' => '', |
|
45
|
|
|
'password' => '', |
|
46
|
|
|
'authentication' => 'SOAP_AUTHENTICATION_BASIC' |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $config An array defining the configuration settings |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct($config = []) { |
|
55
|
|
|
parent::__construct($config); |
|
56
|
|
|
|
|
57
|
|
|
$this->connected = $this->connect(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Setup Configuration options. |
|
62
|
|
|
* |
|
63
|
|
|
* @return array|bool Configuration options or false on failure |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function _parseConfig() { |
|
66
|
|
|
if (!class_exists('SoapClient')) { |
|
67
|
|
|
$this->showError('Class SoapClient not found, please enable Soap extensions'); |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$options = ['trace' => Configure::read('debug') > 0]; |
|
72
|
|
|
if (!empty($this->getConfig('location'))) { |
|
|
|
|
|
|
73
|
|
|
$options['location'] = $this->getConfig('location'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if (!empty($this->getConfig('uri'))) { |
|
77
|
|
|
$options['uri'] = $this->getConfig('uri'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (!empty($this->getConfig('login'))) { |
|
81
|
|
|
$options['login'] = $this->getConfig('login'); |
|
82
|
|
|
$options['password'] = $this->getConfig('password'); |
|
83
|
|
|
$options['authentication'] = $this->getConfig('authentication'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $options; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Connects to the SOAP server using the WSDL in the configuration. |
|
91
|
|
|
* |
|
92
|
|
|
* @return bool True on success, false on failure |
|
93
|
|
|
*/ |
|
94
|
|
|
public function connect() { |
|
95
|
|
|
$options = $this->_parseConfig(); |
|
96
|
|
|
|
|
97
|
|
|
if (!empty($this->config['wsdl'])) { |
|
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
$this->client = new SoapClient($this->getConfig('wsdl'), $options); |
|
100
|
|
|
return (bool)$this->client; |
|
101
|
|
|
} catch(SoapFault $fault) { |
|
|
|
|
|
|
102
|
|
|
$this->showError($fault->faultstring); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Sets the SoapClient instance to null. |
|
111
|
|
|
* |
|
112
|
|
|
* @return true |
|
113
|
|
|
*/ |
|
114
|
|
|
public function close() { |
|
115
|
|
|
$this->client = null; |
|
116
|
|
|
$this->connected = false; |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Returns the available SOAP methods. |
|
123
|
|
|
* |
|
124
|
|
|
* @param mixed $data Unused in this class. |
|
125
|
|
|
* @return array List of SOAP methods |
|
126
|
|
|
*/ |
|
127
|
|
|
public function listSources($data = null) { |
|
|
|
|
|
|
128
|
|
|
return $this->client->__getFunctions(); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Query the SOAP server with the given method and parameters. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $method Name of method to call |
|
135
|
|
|
* @param array $queryData A list with parameters to pass |
|
136
|
|
|
* @return mixed Returns the result on success, false on failure |
|
137
|
|
|
*/ |
|
138
|
|
|
public function query($method, $queryData = []) { |
|
139
|
|
|
if (!$this->connected) { |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
if (!empty($queryData)) { |
|
144
|
|
|
$queryData = [$queryData]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
try { |
|
148
|
|
|
return $this->client->__soapCall($method, $queryData); |
|
149
|
|
|
} catch (SoapFault $fault) { |
|
150
|
|
|
$this->showError($fault->faultstring); |
|
151
|
|
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns the last SOAP response. |
|
157
|
|
|
* |
|
158
|
|
|
* @return string The last SOAP response |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getResponse() { |
|
161
|
|
|
return $this->client->__getLastResponse(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns the last SOAP request. |
|
166
|
|
|
* |
|
167
|
|
|
* @return string The last SOAP request |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getRequest() { |
|
170
|
|
|
return $this->client->__getLastRequest(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Writes an error message to log file. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $error Error message |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function showError($error) { |
|
180
|
|
|
$message = __d('vat_number_check', 'SOAP Error: %s', $error); |
|
181
|
|
|
Log::error($message); |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.