|
1
|
|
|
<?php |
|
2
|
|
|
namespace VatNumberCheck\Model\Datasource; |
|
3
|
|
|
|
|
4
|
|
|
use Cake\Log\Log; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* SOAP DataSource. |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class Soap |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* SoapClient instance. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \SoapClient|null |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $client = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* URI of the WSDL file or NULL if working in non-WSDL mode. |
|
21
|
|
|
* |
|
22
|
|
|
* @var null|string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $wsdl = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Options. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $options = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Connection status. |
|
35
|
|
|
* |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $connected = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Setter for the `wsdl` property. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $wsdl URI of the WSDL file or NULL if working in non-WSDL mode. |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function setWsdl(string $wsdl): bool |
|
47
|
|
|
{ |
|
48
|
|
|
$this->wsdl = $wsdl; |
|
49
|
|
|
|
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Setter for the `options` property. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $options Options |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function setOptions(array $options = []): bool |
|
60
|
|
|
{ |
|
61
|
|
|
$this->options = $options; |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get SoapClient instance. |
|
68
|
|
|
* |
|
69
|
|
|
* @return \SoapClient|null |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getClient() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->client; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Connects to the server using the WSDL in the configuration. |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool True on success, false on failure |
|
80
|
|
|
*/ |
|
81
|
|
|
public function connect(): bool |
|
82
|
|
|
{ |
|
83
|
|
|
if (!empty($this->wsdl)) { |
|
84
|
|
|
try { |
|
85
|
|
|
$this->client = new \SoapClient($this->wsdl, $this->options); |
|
86
|
|
|
$this->connected = (bool)$this->client; |
|
87
|
|
|
|
|
88
|
|
|
return $this->connected; |
|
89
|
|
|
} catch (\SoapFault $e) { |
|
90
|
|
|
Log::error($e); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Disconnects to the server. |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool True |
|
101
|
|
|
*/ |
|
102
|
|
|
public function close(): bool |
|
103
|
|
|
{ |
|
104
|
|
|
$this->client = null; |
|
105
|
|
|
$this->connected = false; |
|
106
|
|
|
|
|
107
|
|
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Query the server with the given method and parameters. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $method Name of method to call |
|
114
|
|
|
* @param array $data A list with parameters to pass |
|
115
|
|
|
* @param array $headers A list of headers to set |
|
116
|
|
|
* @return mixed Returns the result on success, false on failure |
|
117
|
|
|
*/ |
|
118
|
|
|
public function query(string $method, array $data = [], array $headers = []) |
|
119
|
|
|
{ |
|
120
|
|
|
if (!$this->connected) { |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
if (is_null($this->client)) { |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
try { |
|
129
|
|
|
if (!empty($data)) { |
|
130
|
|
|
$data = [$data]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach ($headers as $header) { |
|
134
|
|
|
$this->client |
|
135
|
|
|
->__setSoapHeaders(new \SoapHeader($header['namespace'], $header['name'], $header['data'])); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this->client->__soapCall($method, $data); |
|
139
|
|
|
} catch (\SoapFault $e) { |
|
140
|
|
|
Log::error($e); |
|
141
|
|
|
|
|
142
|
|
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Call methods from the SoapClient class. |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $method A method name |
|
150
|
|
|
* @param array $params Method arguments |
|
151
|
|
|
* @return mixed Returns the result on success, false on failure |
|
152
|
|
|
*/ |
|
153
|
|
|
public function __call(string $method, array $params = []) |
|
154
|
|
|
{ |
|
155
|
|
|
if (is_null($this->client)) { |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$callable = [$this->client, sprintf('__%s', $method)]; |
|
160
|
|
|
if (!is_callable($callable)) { |
|
161
|
|
|
return false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return call_user_func_array($callable, $params); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|