|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* MIT License |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA |
|
6
|
|
|
* |
|
7
|
|
|
* @author Francimar Alves <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
11
|
|
|
* in the Software without restriction, including without limitation the rights |
|
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
14
|
|
|
* furnished to do so, subject to the following conditions: |
|
15
|
|
|
* |
|
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
17
|
|
|
* copies or substantial portions of the Software. |
|
18
|
|
|
* |
|
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
25
|
|
|
* SOFTWARE. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
namespace NFe\Common; |
|
29
|
|
|
|
|
30
|
|
|
use NFe\Entity\Emitente; |
|
31
|
|
|
use NFe\Database\Estatico; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Fornece informações importante para a geração e envio das notas fiscais |
|
35
|
|
|
*/ |
|
36
|
|
|
class Configuracao |
|
|
|
|
|
|
37
|
|
|
{ |
|
38
|
|
|
|
|
39
|
|
|
private $banco; |
|
40
|
|
|
private $emitente; |
|
41
|
|
|
private $evento; |
|
42
|
|
|
private $chave_publica; |
|
43
|
|
|
private $chave_privada; |
|
44
|
|
|
private $arquivo_chave_publica; |
|
45
|
|
|
private $arquivo_chave_privada; |
|
46
|
|
|
private $expiracao; |
|
47
|
|
|
private $token; |
|
48
|
|
|
private $csc; |
|
49
|
|
|
private $token_ibpt; |
|
50
|
|
|
private $tempo_limite; |
|
51
|
|
|
private $sincrono; |
|
52
|
|
|
private $offline; |
|
53
|
|
|
|
|
54
|
87 |
|
public function __construct($configuracao = array()) |
|
55
|
|
|
{ |
|
56
|
87 |
|
$this->fromArray($configuracao); |
|
57
|
87 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Banco que fornece informações sobre items da nota como: Códigos e Taxas |
|
61
|
|
|
*/ |
|
62
|
62 |
|
public function getBanco() |
|
63
|
|
|
{ |
|
64
|
62 |
|
return $this->banco; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
87 |
|
public function setBanco($banco) |
|
68
|
|
|
{ |
|
69
|
87 |
|
$this->banco = $banco; |
|
70
|
87 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Emitente da nota fiscal |
|
75
|
|
|
*/ |
|
76
|
38 |
|
public function getEmitente() |
|
77
|
|
|
{ |
|
78
|
38 |
|
return $this->emitente; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
87 |
|
public function setEmitente($emitente) |
|
82
|
|
|
{ |
|
83
|
87 |
|
$this->emitente = $emitente; |
|
84
|
87 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Informa a instancia que receberá os eventos do processamento das notas |
|
89
|
|
|
*/ |
|
90
|
4 |
|
public function getEvento() |
|
91
|
|
|
{ |
|
92
|
4 |
|
return $this->evento; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
87 |
|
public function setEvento($evento) |
|
96
|
|
|
{ |
|
97
|
87 |
|
$this->evento = $evento; |
|
98
|
87 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Conteúdo da chave pública ou certificado no formato PEM |
|
103
|
|
|
*/ |
|
104
|
87 |
|
public function getChavePublica() |
|
105
|
|
|
{ |
|
106
|
87 |
|
return $this->chave_publica; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
87 |
|
public function setChavePublica($chave_publica) |
|
110
|
|
|
{ |
|
111
|
87 |
|
$this->chave_publica = $chave_publica; |
|
112
|
87 |
|
$this->carregaChavePublica(); |
|
113
|
87 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Conteúdo da chave privada do certificado no formato PEM |
|
118
|
|
|
*/ |
|
119
|
28 |
|
public function getChavePrivada() |
|
120
|
|
|
{ |
|
121
|
28 |
|
return $this->chave_privada; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
87 |
|
public function setChavePrivada($chave_privada) |
|
125
|
|
|
{ |
|
126
|
87 |
|
$this->chave_privada = $chave_privada; |
|
127
|
87 |
|
return $this; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Informa o caminho do arquivo da chave pública ou certificado no formato |
|
132
|
|
|
* PEM |
|
133
|
|
|
*/ |
|
134
|
20 |
|
public function getArquivoChavePublica() |
|
135
|
|
|
{ |
|
136
|
20 |
|
return $this->arquivo_chave_publica; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
87 |
|
public function setArquivoChavePublica($arquivo_chave_publica) |
|
|
|
|
|
|
140
|
|
|
{ |
|
141
|
87 |
|
$this->arquivo_chave_publica = $arquivo_chave_publica; |
|
142
|
87 |
|
if (file_exists($arquivo_chave_publica)) { |
|
143
|
42 |
|
$this->setChavePublica(file_get_contents($arquivo_chave_publica)); |
|
144
|
42 |
|
} |
|
145
|
87 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Caminho do arquivo da chave privada do certificado no formato PEM |
|
150
|
|
|
*/ |
|
151
|
20 |
|
public function getArquivoChavePrivada() |
|
152
|
|
|
{ |
|
153
|
20 |
|
return $this->arquivo_chave_privada; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
87 |
|
public function setArquivoChavePrivada($arquivo_chave_privada) |
|
|
|
|
|
|
157
|
|
|
{ |
|
158
|
87 |
|
$this->arquivo_chave_privada = $arquivo_chave_privada; |
|
159
|
87 |
|
if (file_exists($arquivo_chave_privada)) { |
|
160
|
42 |
|
$this->setChavePrivada(file_get_contents($arquivo_chave_privada)); |
|
161
|
42 |
|
} |
|
162
|
87 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Data de expiração do certificado em timestamp |
|
167
|
|
|
*/ |
|
168
|
2 |
|
public function getExpiracao() |
|
169
|
|
|
{ |
|
170
|
2 |
|
return $this->expiracao; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
87 |
|
private function setExpiracao($expiracao) |
|
174
|
|
|
{ |
|
175
|
87 |
|
$this->expiracao = $expiracao; |
|
176
|
87 |
|
return $this; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Token do CSC |
|
181
|
|
|
*/ |
|
182
|
26 |
|
public function getToken() |
|
183
|
|
|
{ |
|
184
|
26 |
|
return $this->token; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
87 |
|
public function setToken($token) |
|
188
|
|
|
{ |
|
189
|
87 |
|
$this->token = $token; |
|
190
|
87 |
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Código do contribuinte para emissão de nota fiscal |
|
195
|
|
|
*/ |
|
196
|
26 |
|
public function getCSC() |
|
197
|
|
|
{ |
|
198
|
26 |
|
return $this->csc; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
87 |
|
public function setCSC($csc) |
|
202
|
|
|
{ |
|
203
|
87 |
|
$this->csc = $csc; |
|
204
|
87 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Token IBPT para consulta de impostos online |
|
209
|
|
|
*/ |
|
210
|
31 |
|
public function getTokenIBPT() |
|
211
|
|
|
{ |
|
212
|
31 |
|
return $this->token_ibpt; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
87 |
|
public function setTokenIBPT($token_ibpt) |
|
216
|
|
|
{ |
|
217
|
87 |
|
$this->token_ibpt = $token_ibpt; |
|
218
|
87 |
|
return $this; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Tempo limite em segundos nas conexões com os Web services, 0 para sem tempo limite |
|
223
|
|
|
*/ |
|
224
|
20 |
|
public function getTempoLimite() |
|
225
|
|
|
{ |
|
226
|
20 |
|
return $this->tempo_limite; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
87 |
|
public function setTempoLimite($tempo_limite) |
|
230
|
|
|
{ |
|
231
|
87 |
|
$this->tempo_limite = intval($tempo_limite); |
|
232
|
87 |
|
return $this; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Informa se o processo de autorização da nota é síncrono ou assíncrono |
|
237
|
|
|
*/ |
|
238
|
4 |
|
public function getSincrono($normalize = false) |
|
239
|
|
|
{ |
|
240
|
4 |
|
if (!$normalize) { |
|
241
|
1 |
|
return $this->sincrono; |
|
242
|
|
|
} |
|
243
|
3 |
|
return $this->isSincrono()?'1':'0'; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Informa se o processo de autorização da nota é síncrono ou assíncrono |
|
248
|
|
|
*/ |
|
249
|
3 |
|
public function isSincrono() |
|
250
|
|
|
{ |
|
251
|
3 |
|
return $this->sincrono == 'Y'; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
87 |
|
public function setSincrono($sincrono) |
|
255
|
|
|
{ |
|
256
|
87 |
|
if (!in_array($sincrono, array('N', 'Y'))) { |
|
257
|
|
|
$sincrono = $sincrono?'Y':'N'; |
|
258
|
|
|
} |
|
259
|
87 |
|
$this->sincrono = $sincrono; |
|
260
|
87 |
|
return $this; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Informa se está operando offline |
|
265
|
|
|
* @return mixed offline da Configuracao |
|
266
|
|
|
*/ |
|
267
|
1 |
|
public function getOffline($normalize = false) |
|
268
|
|
|
{ |
|
269
|
1 |
|
if (!$normalize || is_null($this->offline)) { |
|
270
|
1 |
|
return $this->offline; |
|
271
|
|
|
} |
|
272
|
|
|
return Util::toDateTime($this->getOffline()); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Informa se está operando offline |
|
277
|
|
|
*/ |
|
278
|
20 |
|
public function isOffline() |
|
279
|
|
|
{ |
|
280
|
20 |
|
return $this->offline + 180 > time(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Entra no modo offline e sai automaticamente após 3 minutos |
|
285
|
|
|
*/ |
|
286
|
2 |
|
public function setOffline($offline) |
|
287
|
|
|
{ |
|
288
|
2 |
|
if (!is_null($offline) && !is_numeric($offline)) { |
|
289
|
|
|
$offline = strtotime($offline); |
|
290
|
|
|
} |
|
291
|
2 |
|
$this->offline = $offline; |
|
292
|
2 |
|
return $this; |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
1 |
|
public function toArray($recursive = false) |
|
296
|
|
|
{ |
|
297
|
1 |
|
$configuracao = array(); |
|
298
|
1 |
|
$configuracao['banco'] = $this->getBanco(); |
|
299
|
1 |
|
$configuracao['emitente'] = $this->getEmitente(); |
|
300
|
1 |
|
$configuracao['evento'] = $this->getEvento(); |
|
301
|
1 |
|
$configuracao['arquivo_chave_publica'] = $this->getArquivoChavePublica(); |
|
302
|
1 |
|
$configuracao['arquivo_chave_privada'] = $this->getArquivoChavePrivada(); |
|
303
|
1 |
|
$configuracao['expiracao'] = $this->getExpiracao(); |
|
304
|
1 |
|
$configuracao['token'] = $this->getToken(); |
|
305
|
1 |
|
$configuracao['csc'] = $this->getCSC(); |
|
306
|
1 |
|
$configuracao['token_ibpt'] = $this->getTokenIBPT(); |
|
307
|
1 |
|
$configuracao['tempo_limite'] = $this->getTempoLimite(); |
|
308
|
1 |
|
$configuracao['sincrono'] = $this->getSincrono(); |
|
309
|
1 |
|
$configuracao['offline'] = $this->getOffline($recursive); |
|
310
|
1 |
|
return $configuracao; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
87 |
|
public function fromArray($configuracao = array()) |
|
|
|
|
|
|
314
|
|
|
{ |
|
315
|
87 |
|
if ($configuracao instanceof Configuracao) { |
|
316
|
|
|
$configuracao = $configuracao->toArray(); |
|
317
|
87 |
|
} elseif (!is_array($configuracao)) { |
|
318
|
|
|
return $this; |
|
319
|
|
|
} |
|
320
|
87 |
|
if (!isset($configuracao['banco']) || is_null($configuracao['banco'])) { |
|
321
|
87 |
|
$this->setBanco(new Estatico()); |
|
322
|
87 |
|
} else { |
|
323
|
1 |
|
$this->setBanco($configuracao['banco']); |
|
324
|
|
|
} |
|
325
|
87 |
|
if (!isset($configuracao['emitente']) || is_null($configuracao['emitente'])) { |
|
326
|
87 |
|
$this->setEmitente(new Emitente()); |
|
327
|
87 |
|
} else { |
|
328
|
1 |
|
$this->setEmitente($configuracao['emitente']); |
|
329
|
|
|
} |
|
330
|
87 |
|
if (isset($configuracao['evento'])) { |
|
331
|
1 |
|
$this->setEvento($configuracao['evento']); |
|
332
|
1 |
|
} else { |
|
333
|
87 |
|
$this->setEvento(null); |
|
334
|
|
|
} |
|
335
|
87 |
|
if (isset($configuracao['chave_publica'])) { |
|
336
|
|
|
$this->setChavePublica($configuracao['chave_publica']); |
|
337
|
|
|
} else { |
|
338
|
87 |
|
$this->setChavePublica(null); |
|
339
|
|
|
} |
|
340
|
87 |
|
if (isset($configuracao['chave_privada'])) { |
|
341
|
|
|
$this->setChavePrivada($configuracao['chave_privada']); |
|
342
|
|
|
} else { |
|
343
|
87 |
|
$this->setChavePrivada(null); |
|
344
|
|
|
} |
|
345
|
87 |
|
if (isset($configuracao['arquivo_chave_publica'])) { |
|
346
|
1 |
|
$this->setArquivoChavePublica($configuracao['arquivo_chave_publica']); |
|
347
|
1 |
|
} else { |
|
348
|
87 |
|
$this->setArquivoChavePublica(null); |
|
349
|
|
|
} |
|
350
|
87 |
|
if (isset($configuracao['arquivo_chave_privada'])) { |
|
351
|
1 |
|
$this->setArquivoChavePrivada($configuracao['arquivo_chave_privada']); |
|
352
|
1 |
|
} else { |
|
353
|
87 |
|
$this->setArquivoChavePrivada(null); |
|
354
|
|
|
} |
|
355
|
87 |
|
if (isset($configuracao['token'])) { |
|
356
|
|
|
$this->setToken($configuracao['token']); |
|
357
|
|
|
} else { |
|
358
|
87 |
|
$this->setToken(null); |
|
359
|
|
|
} |
|
360
|
87 |
|
if (isset($configuracao['csc'])) { |
|
361
|
|
|
$this->setCSC($configuracao['csc']); |
|
362
|
|
|
} else { |
|
363
|
87 |
|
$this->setCSC(null); |
|
364
|
|
|
} |
|
365
|
87 |
|
if (isset($configuracao['token_ibpt'])) { |
|
366
|
|
|
$this->setTokenIBPT($configuracao['token_ibpt']); |
|
367
|
|
|
} else { |
|
368
|
87 |
|
$this->setTokenIBPT(null); |
|
369
|
|
|
} |
|
370
|
87 |
|
if (!isset($configuracao['tempo_limite']) || is_null($configuracao['tempo_limite'])) { |
|
371
|
87 |
|
$this->setTempoLimite(4); |
|
372
|
87 |
|
} else { |
|
373
|
1 |
|
$this->setTempoLimite($configuracao['tempo_limite']); |
|
374
|
|
|
} |
|
375
|
87 |
|
if (!isset($configuracao['sincrono']) || is_null($configuracao['sincrono'])) { |
|
376
|
87 |
|
$this->setSincrono('Y'); |
|
377
|
87 |
|
} else { |
|
378
|
1 |
|
$this->setSincrono($configuracao['sincrono']); |
|
379
|
|
|
} |
|
380
|
87 |
|
return $this; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
87 |
|
private function carregaChavePublica() |
|
384
|
|
|
{ |
|
385
|
87 |
|
if (is_null($this->getChavePublica())) { |
|
386
|
87 |
|
$this->setExpiracao(null); |
|
387
|
87 |
|
} else { |
|
388
|
42 |
|
$cert = openssl_x509_read($this->getChavePublica()); |
|
389
|
42 |
|
$cert_data = openssl_x509_parse($cert); |
|
390
|
42 |
|
$this->setExpiracao($cert_data['validTo_time_t']); |
|
391
|
|
|
} |
|
392
|
87 |
|
} |
|
393
|
|
|
|
|
394
|
29 |
|
public function verificaValidadeCertificado() |
|
395
|
|
|
{ |
|
396
|
29 |
|
if (getenv('APP_ENV') == 'testing') { |
|
397
|
29 |
|
return; |
|
398
|
|
|
} |
|
399
|
|
|
if (is_null($this->getExpiracao())) { |
|
400
|
|
|
throw new \Exception('A data de expiração do certificado não foi informada', 401); |
|
401
|
|
|
} else if ($this->getExpiracao() < time()) { |
|
402
|
|
|
throw new \Exception('O certificado digital expirou', 500); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
|
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.