1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\Esfinge; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use NFePHP\Esfinge\Soap\CurlSoap; |
7
|
|
|
use NFePHP\Esfinge\Files\FileFolders; |
8
|
|
|
|
9
|
|
|
class Base |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $errors; |
13
|
|
|
/** |
14
|
|
|
* tpAmb |
15
|
|
|
* @var int |
16
|
|
|
*/ |
17
|
|
|
protected $tpAmb = 2; |
18
|
|
|
/** |
19
|
|
|
* ambiente |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $ambiente = 'homologacao'; |
23
|
|
|
/** |
24
|
|
|
* Diretorio para gravar arquivos de LOG |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $pathFiles = ''; |
28
|
|
|
/** |
29
|
|
|
* aConfig |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $aConfig = array(); |
33
|
|
|
/** |
34
|
|
|
* aProxy |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $aProxy = array(); |
38
|
|
|
/** |
39
|
|
|
* soapTimeout |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $soapTimeout = 10; |
43
|
|
|
/** |
44
|
|
|
* oSoap |
45
|
|
|
* @var Object Class |
46
|
|
|
*/ |
47
|
|
|
protected $oSoap; |
48
|
|
|
/** |
49
|
|
|
* soapDebug |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $soapDebug = ''; |
53
|
|
|
/** |
54
|
|
|
* Header da mensagem SOAP |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $header; |
58
|
|
|
/** |
59
|
|
|
* Nome do usuário do sistema |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected $username; |
63
|
|
|
/** |
64
|
|
|
* Password do usuário do sistema |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
protected $password; |
68
|
|
|
/** |
69
|
|
|
* Código da Unidade Gestora conforme informado pelo serviço listar |
70
|
|
|
* da tabela unidades gestoras |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected $codigoUnidadeGestora; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Contrutor |
77
|
|
|
* @param string $configJson |
78
|
|
|
*/ |
79
|
6 |
|
public function __construct($configJson = '') |
80
|
|
|
{ |
81
|
6 |
|
if (empty($configJson)) { |
82
|
3 |
|
throw new InvalidArgumentException('A configuração deve ser passada.'); |
83
|
|
|
} |
84
|
3 |
|
$config = $configJson; |
85
|
3 |
|
if (is_file($configJson)) { |
86
|
|
|
$config = file_get_contents($configJson); |
87
|
|
|
} |
88
|
3 |
|
$this->aConfig = json_decode($config, true); |
|
|
|
|
89
|
|
|
|
90
|
3 |
|
$this->username = $this->aConfig['username']; |
91
|
3 |
|
$this->password = $this->aConfig['password']; |
92
|
3 |
|
$this->codigoUnidadeGestora = $this->aConfig['codigoUnidadeGestora']; |
93
|
3 |
|
$this->aProxy = $this->aConfig['aProxyConf']; |
94
|
3 |
|
$this->setAmbiente($this->aConfig['tpAmb']); |
95
|
3 |
|
$this->pathFiles = $this->aConfig['pathFiles']; |
96
|
3 |
|
$this->loadSoapClass(); |
97
|
3 |
|
$this->buildSoapHeader(); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Seta o ambiente de trabalho |
102
|
|
|
* 1 - Produção |
103
|
|
|
* 2 - Homologação |
104
|
|
|
* @param int $tpAmb |
105
|
|
|
*/ |
106
|
6 |
|
public function setAmbiente($tpAmb = 2) |
107
|
|
|
{ |
108
|
6 |
|
if ($tpAmb == 1) { |
109
|
3 |
|
$this->tpAmb = 1; |
110
|
3 |
|
$this->ambiente = 'producao'; |
111
|
2 |
|
} else { |
112
|
6 |
|
$this->tpAmb = 2; |
113
|
6 |
|
$this->ambiente = 'homologacao'; |
114
|
|
|
//sobrescreve a senha que é diferente no ambiente de teste |
115
|
6 |
|
$this->password = '123456'; |
116
|
|
|
} |
117
|
6 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Retorna o tpAmb |
121
|
|
|
* @return int |
122
|
|
|
*/ |
123
|
9 |
|
public function getAmbiente() |
124
|
|
|
{ |
125
|
9 |
|
return $this->tpAmb; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* setSoapTimeOut |
130
|
|
|
* Seta um valor para timeout |
131
|
|
|
* |
132
|
|
|
* @param integer $segundos |
133
|
|
|
*/ |
134
|
6 |
|
public function setSoapTimeOut($segundos = 10) |
135
|
|
|
{ |
136
|
6 |
|
if (! empty($segundos) && is_numeric($segundos)) { |
137
|
3 |
|
$this->soapTimeout = $segundos; |
|
|
|
|
138
|
3 |
|
$this->loadSoapClass(); |
139
|
2 |
|
} |
140
|
6 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* getSoapTimeOut |
144
|
|
|
* Retorna o valor de timeout defido |
145
|
|
|
* |
146
|
|
|
* @return integer |
147
|
|
|
*/ |
148
|
6 |
|
public function getSoapTimeOut() |
149
|
|
|
{ |
150
|
6 |
|
return $this->soapTimeout; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Monta as tags com base na chave e no valor do array |
155
|
|
|
* @param array $data |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
protected function addTag($data) |
159
|
|
|
{ |
160
|
|
|
$ret = ''; |
161
|
|
|
foreach ($data as $key => $value) { |
162
|
|
|
if (! empty($value)) { |
163
|
|
|
$ret .= "<$key>$value</$key>"; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
return $ret; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Monta o conjunto de Body na função enviar |
171
|
|
|
* @param string $key |
172
|
|
|
* @param array $data |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function buildEnviarB($key, $data) |
176
|
|
|
{ |
177
|
|
|
if (count($data) > 5000) { |
178
|
|
|
throw new InvalidArgumentException('O limite de 5000 dados foi ultrapassado.'); |
179
|
|
|
} |
180
|
|
|
$msg = ""; |
181
|
|
|
foreach ($data as $field) { |
182
|
|
|
$msg .= "<$key>"; |
183
|
|
|
$msg .= $this->addTag($field); |
184
|
|
|
$msg .= "</$key>"; |
185
|
|
|
} |
186
|
|
|
$msg .= '</enviar>'; |
187
|
|
|
return $msg; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Monta o conjunto Body da função Listar |
192
|
|
|
* @param string $pagina |
193
|
|
|
* @param array $filtros |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
protected function buildListarB($pagina = '', $filtros = []) |
197
|
|
|
{ |
198
|
|
|
$msg = '<PAGINA>'.$pagina.'</PAGINA>'; |
199
|
|
|
foreach ($filtros as $filtro) { |
200
|
|
|
$f = '<filtros>'; |
201
|
|
|
$f .= $this->addTag($filtro); |
202
|
|
|
$f .= '</filtros>'; |
203
|
|
|
$msg .= $f; |
204
|
|
|
}; |
205
|
|
|
$msg .= '</listar>'; |
206
|
|
|
return $msg; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Monta a primeira parte de todas mensagens |
211
|
|
|
* @param string $namespace |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
protected function buildMsgH($tipo, $namespace) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$key = 'enviar'; |
217
|
|
|
$codug = ''; |
218
|
|
|
if ($tipo == 'L') { |
219
|
|
|
$key = 'listar'; |
220
|
|
|
$codug = "<codigoUg>$this->codigoUnidadeGestora</codigoUg>"; |
221
|
|
|
} |
222
|
|
|
$msg = "<$key>"; |
223
|
|
|
$msg .= $codug; |
224
|
|
|
$msg .= "<chaveToken>$this->tokenid</chaveToken>"; |
|
|
|
|
225
|
|
|
$msg .= "<competencia>$this->competencia</competencia>"; |
|
|
|
|
226
|
|
|
return $msg; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Monta o corpo de todas as mensagens |
231
|
|
|
* @param string $tipo |
232
|
|
|
* @param array $data |
233
|
|
|
* @param string $key |
234
|
|
|
* @return string |
235
|
|
|
*/ |
236
|
|
|
protected function buildMsgB($tipo, $data, $key = '') |
237
|
|
|
{ |
238
|
|
|
if ($tipo == 'L') { |
239
|
|
|
//numerico pagina |
240
|
|
|
$pagina = $data['pagina']; |
241
|
|
|
//array filtros []['','','',''] |
|
|
|
|
242
|
|
|
$filtros = $data['filtros']; |
243
|
|
|
$msg = $this->buildListarB($pagina, $filtros); |
244
|
|
|
} elseif ($tipo == 'E') { |
245
|
|
|
$msg = $this->buildEnviarB($key, $data); |
246
|
|
|
} |
247
|
|
|
return $msg; |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Constroi o header da mensagem SOAP |
252
|
|
|
*/ |
253
|
|
|
protected function buildSoapHeader() |
254
|
|
|
{ |
255
|
|
|
$this->header = "<wsse:Security " |
256
|
|
|
. "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" |
257
|
|
|
. "<wsse:UsernameToken " |
258
|
|
|
. "xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" |
259
|
|
|
. "<wsse:Username>" |
260
|
|
|
. $this->username |
261
|
|
|
. "</wsse:Username><wsse:Password " |
262
|
|
|
. "Type=\"http://docs.oasis-open.org/wss/2004/01/" |
263
|
|
|
. "oasis-200401-wss-username-token-profile-1.0#PasswordText\">" |
264
|
|
|
. $this->password |
265
|
|
|
. "</wsse:Password>" |
266
|
|
|
. "</wsse:UsernameToken>" |
267
|
|
|
. "</wsse:Security>"; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Envia a mensagem para o webservice |
272
|
|
|
* @param string $urlService |
|
|
|
|
273
|
|
|
* @param strting $body |
|
|
|
|
274
|
|
|
* @param string $method |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
protected function envia($uri, $namespace, $data, $method, $met) |
278
|
|
|
{ |
279
|
|
|
if ($namespace !== 'http://token.ws.tce.sc.gov.br/') { |
280
|
|
|
//constroi a mensagem |
281
|
|
|
$body = $this->buildMsgH($method, $namespace); |
282
|
|
|
$body .= $this->buildMsgB($method, $data, substr($met, 0, strlen($met)-1)); |
283
|
|
|
} else { |
284
|
|
|
$body = $data; |
285
|
|
|
} |
286
|
|
|
//envia pelo curl |
287
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
288
|
|
|
//processa o retorno |
289
|
|
|
$resp = Response::readReturn($met, $retorno); |
290
|
|
|
//salvar os arquivos para LOG |
291
|
|
|
return $resp; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Carrega a classe SOAP e os certificados |
296
|
|
|
*/ |
297
|
9 |
|
protected function loadSoapClass() |
298
|
|
|
{ |
299
|
9 |
|
$pathlog = $this->pathFiles.DIRECTORY_SEPARATOR.$this->ambiente; |
300
|
9 |
|
$this->oSoap = null; |
301
|
9 |
|
$soap = new CurlSoap( |
302
|
6 |
|
$pathlog, |
303
|
9 |
|
$this->soapTimeout, |
304
|
9 |
|
$this->aProxy |
305
|
6 |
|
); |
306
|
9 |
|
$this->oSoap = $soap; |
307
|
9 |
|
} |
308
|
|
|
} |
309
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..