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
|
|
|
public function __construct($configJson = '') |
80
|
|
|
{ |
81
|
|
|
if (empty($configJson)) { |
82
|
|
|
throw new InvalidArgumentException('A configuração deve ser passada.'); |
83
|
|
|
} |
84
|
|
|
$config = $configJson; |
85
|
|
|
if (is_file($configJson)) { |
86
|
|
|
$config = file_get_contents($configJson); |
87
|
|
|
} |
88
|
|
|
$this->aConfig = json_decode($config, true); |
|
|
|
|
89
|
|
|
|
90
|
|
|
$this->username = $this->aConfig['username']; |
91
|
|
|
$this->password = $this->aConfig['password']; |
92
|
|
|
$this->codigoUnidadeGestora = $this->aConfig['codigoUnidadeGestora']; |
93
|
|
|
$this->aProxy = $this->aConfig['aProxyConf']; |
94
|
|
|
$this->setAmbiente($this->aConfig['tpAmb']); |
95
|
|
|
$this->pathFiles = $this->aConfig['pathFiles']; |
96
|
|
|
$this->loadSoapClass(); |
97
|
|
|
$this->buildSoapHeader(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Seta o ambiente de trabalho |
102
|
|
|
* 1 - Produção |
103
|
|
|
* 2 - Homologação |
104
|
|
|
* @param int $tpAmb |
105
|
|
|
*/ |
106
|
|
|
public function setAmbiente($tpAmb = 2) |
107
|
|
|
{ |
108
|
|
|
if ($tpAmb == 1) { |
109
|
|
|
$this->tpAmb = 1; |
110
|
|
|
$this->ambiente = 'producao'; |
111
|
|
|
} else { |
112
|
|
|
$this->tpAmb = 2; |
113
|
|
|
$this->ambiente = 'homologacao'; |
114
|
|
|
//sobrescreve a senha que é diferente no ambiente de teste |
115
|
|
|
$this->password = '123456'; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* setSoapTimeOut |
121
|
|
|
* Seta um valor para timeout |
122
|
|
|
* |
123
|
|
|
* @param integer $segundos |
124
|
|
|
*/ |
125
|
|
|
public function setSoapTimeOut($segundos = 10) |
126
|
|
|
{ |
127
|
|
|
if (! empty($segundos)) { |
128
|
|
|
$this->soapTimeout = $segundos; |
129
|
|
|
$this->loadSoapClass(); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* getSoapTimeOut |
135
|
|
|
* Retorna o valor de timeout defido |
136
|
|
|
* |
137
|
|
|
* @return integer |
138
|
|
|
*/ |
139
|
|
|
public function getSoapTimeOut() |
140
|
|
|
{ |
141
|
|
|
return $this->soapTimeout; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Monta as tags com base na chave e no valor do array |
146
|
|
|
* @param array $data |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
protected function addTag($data) |
150
|
|
|
{ |
151
|
|
|
$ret = ''; |
152
|
|
|
foreach ($data as $key => $value) { |
153
|
|
|
if (! empty($value)) { |
154
|
|
|
$ret .= "<$key>$value</$key>"; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
return $ret; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Monta o conjunto de Body na função enviar |
162
|
|
|
* @param string $key |
163
|
|
|
* @param array $data |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function buildEnviarB($key, $data) |
167
|
|
|
{ |
168
|
|
|
if (count($data) > 5000) { |
169
|
|
|
throw new InvalidArgumentException('O limite de 5000 dados foi ultrapassado.'); |
170
|
|
|
} |
171
|
|
|
$msg = ''; |
172
|
|
|
foreach ($data as $field) { |
173
|
|
|
$msg .= "<$key>"; |
174
|
|
|
$msg .= $this->addTag($field); |
175
|
|
|
$msg .= "</$key>"; |
176
|
|
|
} |
177
|
|
|
$msg .= '</enviar>'; |
178
|
|
|
return $msg; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Monta o conjunto Body da função Listar |
183
|
|
|
* @param string $pagina |
184
|
|
|
* @param array $filtros |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function buildListarB($pagina = '', $filtros = []) |
188
|
|
|
{ |
189
|
|
|
$msg = '<PAGINA>'.$pagina.'</PAGINA>'; |
190
|
|
|
foreach ($filtros as $filtro) { |
191
|
|
|
$f = '<filtros>'; |
192
|
|
|
$f .= $this->addTag($filtro); |
193
|
|
|
$f .= '</filtros>'; |
194
|
|
|
$msg .= $f; |
195
|
|
|
}; |
196
|
|
|
$msg .= '</listar>'; |
197
|
|
|
return $msg; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Monta a primeira parte de todas mensagens |
202
|
|
|
* @param string $namespace |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
protected function buildMsgH($tipo, $namespace) |
206
|
|
|
{ |
207
|
|
|
$key = 'enviar'; |
208
|
|
|
$codug = ''; |
209
|
|
|
if ($tipo == 'L') { |
210
|
|
|
$key = 'listar'; |
211
|
|
|
$codug = "<codigoUg>$this->codigoUnidadeGestora</codigoUg>"; |
212
|
|
|
} |
213
|
|
|
$msg = "<$key xmlns=\"$namespace\">"; |
214
|
|
|
$msg .= $codug; |
215
|
|
|
$msg .= "<chaveToken>$this->tokenid</chaveToken>"; |
|
|
|
|
216
|
|
|
$msg .= "<competencia>$this->competencia</competencia>"; |
|
|
|
|
217
|
|
|
return $msg; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Monta o corpo de todas as mensagens |
222
|
|
|
* @param string $tipo |
223
|
|
|
* @param array $data |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
protected function buildMsgB($tipo, $data) |
227
|
|
|
{ |
228
|
|
|
if ($tipo == 'L') { |
229
|
|
|
//numerico pagina |
230
|
|
|
$pagina = $data['pagina']; |
231
|
|
|
//array filtros []['','','',''] |
|
|
|
|
232
|
|
|
$filtros = $data['filtros']; |
233
|
|
|
$msg = $this->buildListarB($pagina, $filtros); |
234
|
|
|
} elseif ($tipo == 'E') { |
235
|
|
|
$msg = $this->buildEnviarB($key, $data); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
return $msg; |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Constroi o header da mensagem SOAP |
242
|
|
|
*/ |
243
|
|
|
protected function buildSoapHeader() |
244
|
|
|
{ |
245
|
|
|
$this->header = "<wsse:Security " |
246
|
|
|
. "xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\">" |
247
|
|
|
. "<wsse:UsernameToken " |
248
|
|
|
. "xmlns:wsu=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\">" |
249
|
|
|
. "<wsse:Username>" |
250
|
|
|
. $this->username |
251
|
|
|
. "</wsse:Username><wsse:Password " |
252
|
|
|
. "Type=\"http://docs.oasis-open.org/wss/2004/01/" |
253
|
|
|
. "oasis-200401-wss-username-token-profile-1.0#PasswordText\">" |
254
|
|
|
. $this->password |
255
|
|
|
. "</wsse:Password>" |
256
|
|
|
. "</wsse:UsernameToken>" |
257
|
|
|
. "</wsse:Security>"; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Envia a mensagem para o webservice |
262
|
|
|
* @param string $urlService |
|
|
|
|
263
|
|
|
* @param strting $body |
|
|
|
|
264
|
|
|
* @param string $method |
265
|
|
|
* @return string |
266
|
|
|
*/ |
267
|
|
|
protected function envia($uri, $namespace, $data, $method, $met) |
268
|
|
|
{ |
269
|
|
|
//constroi a mensagem |
270
|
|
|
$body = $this->buildMsgH($method, $namespace); |
271
|
|
|
$body .= $this->buildMsgB($method, $data); |
272
|
|
|
//envia pelo curl |
273
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $method); |
274
|
|
|
//processa o retorno |
275
|
|
|
$resp = Response::readReturn($met, $retorno); |
276
|
|
|
//salvar os arquivos para LOG |
277
|
|
|
return $resp; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Carrega a classe SOAP e os certificados |
282
|
|
|
*/ |
283
|
|
|
protected function loadSoapClass() |
284
|
|
|
{ |
285
|
|
|
$pathlog = $this->pathFiles.DIRECTORY_SEPARATOR.$this->ambiente; |
286
|
|
|
$this->oSoap = null; |
287
|
|
|
$soap = new CurlSoap( |
288
|
|
|
$pathlog, |
289
|
|
|
$this->soapTimeout, |
290
|
|
|
$this->aProxy |
291
|
|
|
); |
292
|
|
|
$this->oSoap = $soap; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
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..