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