1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bludata\Soap; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use SoapClient as NativeSoapClient; |
7
|
|
|
|
8
|
|
|
class SoapClient |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $host; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $options; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SoapClient |
22
|
|
|
*/ |
23
|
|
|
protected $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var mixed |
27
|
|
|
*/ |
28
|
|
|
protected $service; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var mixed |
32
|
|
|
*/ |
33
|
|
|
protected $request; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Cria um instancia para comunicação com WSDL utilizando SoapClient. |
37
|
|
|
* |
38
|
|
|
* @param string $host Parametro obrigatório, endereço do WSDL |
39
|
|
|
* @param array $options Parametro opcional, referente aos options que serão utilizados |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function __construct($host, array $options = []) |
42
|
|
|
{ |
43
|
|
|
$this->setHost($host) |
44
|
|
|
->setOptions($options); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Efetua a conexão inicial com o WSDL. |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
|
|
public function connect() |
53
|
|
|
{ |
54
|
|
|
if (!$host = $this->getHost()) { |
55
|
|
|
throw new InvalidArgumentException('Host não informado'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!$this->client) { |
59
|
|
|
$this->client = new NativeSoapClient($host, $this->getOptions()); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Faz a chamada para o WSDL, de acordo com os parametros pré-configurados para o serviço. |
|
|
|
|
67
|
|
|
* |
68
|
|
|
* @return SoapClient::__soapCall |
|
|
|
|
69
|
|
|
*/ |
70
|
|
|
public function call() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$host = $this->getHost(); |
73
|
|
|
if (!$host || empty($host)) { |
74
|
|
|
throw new InvalidArgumentException('Host não informado'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$service = $this->getService(); |
78
|
|
|
if (!$service || empty($service)) { |
79
|
|
|
throw new InvalidArgumentException('Serviço não informado'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$request = $this->getRequest(); |
83
|
|
|
if (!$request || empty($request)) { |
84
|
|
|
throw new InvalidArgumentException('Request não informada'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->connect(); |
88
|
|
|
$client = $this->getClient(); |
89
|
|
|
|
90
|
|
|
return $client->__soapCall($service, $request); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Retorna o host do WSDL. |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getHost() |
99
|
|
|
{ |
100
|
|
|
return $this->host; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Recebe o endereço do WSDL. |
105
|
|
|
* |
106
|
|
|
* @param string $host |
107
|
|
|
* |
108
|
|
|
* @return self |
109
|
|
|
*/ |
110
|
|
|
public function setHost($host) |
111
|
|
|
{ |
112
|
|
|
$this->host = $host; |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Retorna os options utilizados no SoapClient. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getOptions() |
123
|
|
|
{ |
124
|
|
|
return $this->options; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Recebe os options que serão utilizados no SoapClient. |
129
|
|
|
* |
130
|
|
|
* @param array $options |
131
|
|
|
* |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
|
|
public function setOptions(array $options) |
135
|
|
|
{ |
136
|
|
|
$this->options = $options; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Retorna dados da requisição. |
143
|
|
|
* |
144
|
|
|
* @return mixed |
145
|
|
|
*/ |
146
|
|
|
public function getRequest() |
147
|
|
|
{ |
148
|
|
|
return $this->request; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Recebe dados para montar o request. |
153
|
|
|
* |
154
|
|
|
* @param array |
155
|
|
|
* |
156
|
|
|
* @return self |
157
|
|
|
*/ |
158
|
|
|
public function setRequest($request) |
159
|
|
|
{ |
160
|
|
|
$this->request = $request; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Retorna o serviço utilizado. |
167
|
|
|
* |
168
|
|
|
* @return mixed |
169
|
|
|
*/ |
170
|
|
|
public function getService() |
171
|
|
|
{ |
172
|
|
|
return $this->service; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Recebe o serviço que sera utilizado no WSDL. |
177
|
|
|
* |
178
|
|
|
* @param string $service |
179
|
|
|
* |
180
|
|
|
* @return self |
181
|
|
|
*/ |
182
|
|
|
public function setService($service) |
183
|
|
|
{ |
184
|
|
|
$this->service = $service; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Retorna a instancia do SoapClient que foi incializada. |
191
|
|
|
* |
192
|
|
|
* @return mixed |
193
|
|
|
*/ |
194
|
|
|
public function getClient() |
195
|
|
|
{ |
196
|
|
|
return $this->client; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Recebe o SoapClient que sera utilizado. |
201
|
|
|
* |
202
|
|
|
* @param SoapClient $client |
|
|
|
|
203
|
|
|
* |
204
|
|
|
* @return self |
205
|
|
|
*/ |
206
|
|
|
public function setClient(NativeSoapClient $client) |
207
|
|
|
{ |
208
|
|
|
$this->client = $client; |
|
|
|
|
209
|
|
|
|
210
|
|
|
return $this; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.