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 $token; |
47
|
|
|
private $csc; |
48
|
|
|
private $token_ibpt; |
49
|
|
|
private $tempo_limite; |
50
|
|
|
private $sincrono; |
51
|
|
|
private $offline_start; |
52
|
|
|
|
53
|
7 |
|
public function __construct($configuracao = array()) |
54
|
|
|
{ |
55
|
7 |
|
$this->fromArray($configuracao); |
56
|
7 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Banco que fornece informações sobre items da nota como: Códigos e Taxas |
60
|
|
|
*/ |
61
|
32 |
|
public function getBanco() |
62
|
|
|
{ |
63
|
32 |
|
return $this->banco; |
64
|
|
|
} |
65
|
|
|
|
66
|
7 |
|
public function setBanco($banco) |
67
|
|
|
{ |
68
|
7 |
|
$this->banco = $banco; |
69
|
7 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Emitente da nota fiscal |
74
|
|
|
*/ |
75
|
8 |
|
public function getEmitente() |
76
|
|
|
{ |
77
|
8 |
|
return $this->emitente; |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
public function setEmitente($emitente) |
81
|
|
|
{ |
82
|
9 |
|
$this->emitente = $emitente; |
83
|
9 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Informa a instancia que receberá os eventos do processamento das notas |
88
|
|
|
*/ |
89
|
4 |
|
public function getEvento() |
90
|
|
|
{ |
91
|
4 |
|
return $this->evento; |
92
|
|
|
} |
93
|
|
|
|
94
|
7 |
|
public function setEvento($evento) |
95
|
|
|
{ |
96
|
7 |
|
$this->evento = $evento; |
97
|
7 |
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Conteúdo da chave pública ou certificado no formato PEM |
102
|
|
|
*/ |
103
|
3 |
|
public function getChavePublica() |
104
|
|
|
{ |
105
|
3 |
|
return $this->chave_publica; |
106
|
|
|
} |
107
|
|
|
|
108
|
12 |
|
public function setChavePublica($chave_publica) |
109
|
|
|
{ |
110
|
12 |
|
$this->chave_publica = $chave_publica; |
111
|
12 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Conteúdo da chave privada do certificado no formato PEM |
116
|
|
|
*/ |
117
|
3 |
|
public function getChavePrivada() |
118
|
|
|
{ |
119
|
3 |
|
return $this->chave_privada; |
120
|
|
|
} |
121
|
|
|
|
122
|
12 |
|
public function setChavePrivada($chave_privada) |
123
|
|
|
{ |
124
|
12 |
|
$this->chave_privada = $chave_privada; |
125
|
12 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Informa o caminho do arquivo da chave pública ou certificado no formato |
130
|
|
|
* PEM |
131
|
|
|
*/ |
132
|
1 |
|
public function getArquivoChavePublica() |
133
|
|
|
{ |
134
|
1 |
|
return $this->arquivo_chave_publica; |
135
|
|
|
} |
136
|
|
|
|
137
|
12 |
|
public function setArquivoChavePublica($arquivo_chave_publica) |
|
|
|
|
138
|
|
|
{ |
139
|
12 |
|
$this->arquivo_chave_publica = $arquivo_chave_publica; |
140
|
12 |
|
if (file_exists($arquivo_chave_publica)) { |
141
|
5 |
|
$this->setChavePublica(file_get_contents($arquivo_chave_publica)); |
142
|
5 |
|
} |
143
|
12 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Caminho do arquivo da chave privada do certificado no formato PEM |
148
|
|
|
*/ |
149
|
1 |
|
public function getArquivoChavePrivada() |
150
|
|
|
{ |
151
|
1 |
|
return $this->arquivo_chave_privada; |
152
|
|
|
} |
153
|
|
|
|
154
|
12 |
|
public function setArquivoChavePrivada($arquivo_chave_privada) |
|
|
|
|
155
|
|
|
{ |
156
|
12 |
|
$this->arquivo_chave_privada = $arquivo_chave_privada; |
157
|
12 |
|
if (file_exists($arquivo_chave_privada)) { |
158
|
5 |
|
$this->setChavePrivada(file_get_contents($arquivo_chave_privada)); |
159
|
5 |
|
} |
160
|
12 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Token do CSC |
165
|
|
|
*/ |
166
|
4 |
|
public function getToken() |
167
|
|
|
{ |
168
|
4 |
|
return $this->token; |
169
|
|
|
} |
170
|
|
|
|
171
|
7 |
|
public function setToken($token) |
172
|
|
|
{ |
173
|
7 |
|
$this->token = $token; |
174
|
7 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Código do contribuinte para emissão de nota fiscal |
179
|
|
|
*/ |
180
|
4 |
|
public function getCSC() |
181
|
|
|
{ |
182
|
4 |
|
return $this->csc; |
183
|
|
|
} |
184
|
|
|
|
185
|
7 |
|
public function setCSC($csc) |
186
|
|
|
{ |
187
|
7 |
|
$this->csc = $csc; |
188
|
7 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Token IBPT para consulta de impostos online |
193
|
|
|
*/ |
194
|
8 |
|
public function getTokenIBPT() |
195
|
|
|
{ |
196
|
8 |
|
return $this->token_ibpt; |
197
|
|
|
} |
198
|
|
|
|
199
|
7 |
|
public function setTokenIBPT($token_ibpt) |
200
|
|
|
{ |
201
|
7 |
|
$this->token_ibpt = $token_ibpt; |
202
|
7 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Tempo limite em segundos nas conexões com os Web services, 0 para sem tempo limite |
207
|
|
|
*/ |
208
|
1 |
|
public function getTempoLimite() |
209
|
|
|
{ |
210
|
1 |
|
return $this->tempo_limite; |
211
|
|
|
} |
212
|
|
|
|
213
|
7 |
|
public function setTempoLimite($tempo_limite) |
214
|
|
|
{ |
215
|
7 |
|
$this->tempo_limite = intval($tempo_limite); |
216
|
7 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Informa se o processo de autorização da nota é síncrono ou assíncrono |
221
|
|
|
*/ |
222
|
1 |
|
public function getSincrono($normalize = false) |
223
|
|
|
{ |
224
|
1 |
|
if (!$normalize) { |
225
|
1 |
|
return $this->sincrono; |
226
|
|
|
} |
227
|
|
|
return $this->isSincrono()?'1':'0'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Informa se o processo de autorização da nota é síncrono ou assíncrono |
232
|
|
|
*/ |
233
|
|
|
public function isSincrono() |
234
|
|
|
{ |
235
|
|
|
return $this->sincrono == 'Y'; |
236
|
|
|
} |
237
|
|
|
|
238
|
7 |
|
public function setSincrono($sincrono) |
239
|
|
|
{ |
240
|
7 |
|
if (!in_array($sincrono, array('N', 'Y'))) { |
241
|
|
|
$sincrono = $sincrono?'Y':'N'; |
242
|
|
|
} |
243
|
7 |
|
$this->sincrono = $sincrono; |
244
|
7 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Entra no modo offline e sai automaticamente após 3 minutos |
249
|
|
|
*/ |
250
|
|
|
public function setOffline($offline_start) |
251
|
|
|
{ |
252
|
|
|
$this->offline_start = $offline_start; |
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Informa se está operando offline |
258
|
|
|
*/ |
259
|
|
|
public function isOffline() |
260
|
|
|
{ |
261
|
|
|
return $this->offline_start + 180 > time(); |
262
|
|
|
} |
263
|
|
|
|
264
|
1 |
|
public function toArray() |
265
|
|
|
{ |
266
|
1 |
|
$configuracao = array(); |
267
|
1 |
|
$configuracao['banco'] = $this->getBanco(); |
268
|
1 |
|
$configuracao['emitente'] = $this->getEmitente(); |
269
|
1 |
|
$configuracao['evento'] = $this->getEvento(); |
270
|
1 |
|
$configuracao['arquivo_chave_publica'] = $this->getArquivoChavePublica(); |
271
|
1 |
|
$configuracao['arquivo_chave_privada'] = $this->getArquivoChavePrivada(); |
272
|
1 |
|
$configuracao['token'] = $this->getToken(); |
273
|
1 |
|
$configuracao['csc'] = $this->getCSC(); |
274
|
1 |
|
$configuracao['token_ibpt'] = $this->getTokenIBPT(); |
275
|
1 |
|
$configuracao['tempo_limite'] = $this->getTempoLimite(); |
276
|
1 |
|
$configuracao['sincrono'] = $this->getSincrono(); |
277
|
1 |
|
return $configuracao; |
278
|
|
|
} |
279
|
|
|
|
280
|
7 |
|
public function fromArray($configuracao = array()) |
|
|
|
|
281
|
|
|
{ |
282
|
7 |
|
if ($configuracao instanceof Configuracao) { |
283
|
|
|
$configuracao = $configuracao->toArray(); |
284
|
7 |
|
} elseif (!is_array($configuracao)) { |
285
|
|
|
return $this; |
286
|
|
|
} |
287
|
7 |
|
if (!isset($configuracao['banco']) || is_null($configuracao['banco'])) { |
288
|
7 |
|
$this->setBanco(new Estatico()); |
289
|
7 |
|
} else { |
290
|
1 |
|
$this->setBanco($configuracao['banco']); |
291
|
|
|
} |
292
|
7 |
|
if (!isset($configuracao['emitente']) || is_null($configuracao['emitente'])) { |
293
|
7 |
|
$this->setEmitente(new Emitente()); |
294
|
7 |
|
} else { |
295
|
1 |
|
$this->setEmitente($configuracao['emitente']); |
296
|
|
|
} |
297
|
7 |
|
if (isset($configuracao['evento'])) { |
298
|
1 |
|
$this->setEvento($configuracao['evento']); |
299
|
1 |
|
} else { |
300
|
7 |
|
$this->setEvento(null); |
301
|
|
|
} |
302
|
7 |
|
if (isset($configuracao['chave_publica'])) { |
303
|
|
|
$this->setChavePublica($configuracao['chave_publica']); |
304
|
|
|
} else { |
305
|
7 |
|
$this->setChavePublica(null); |
306
|
|
|
} |
307
|
7 |
|
if (isset($configuracao['chave_privada'])) { |
308
|
|
|
$this->setChavePrivada($configuracao['chave_privada']); |
309
|
|
|
} else { |
310
|
7 |
|
$this->setChavePrivada(null); |
311
|
|
|
} |
312
|
7 |
|
if (isset($configuracao['arquivo_chave_publica'])) { |
313
|
1 |
|
$this->setArquivoChavePublica($configuracao['arquivo_chave_publica']); |
314
|
1 |
|
} else { |
315
|
7 |
|
$this->setArquivoChavePublica(null); |
316
|
|
|
} |
317
|
7 |
|
if (isset($configuracao['arquivo_chave_privada'])) { |
318
|
1 |
|
$this->setArquivoChavePrivada($configuracao['arquivo_chave_privada']); |
319
|
1 |
|
} else { |
320
|
7 |
|
$this->setArquivoChavePrivada(null); |
321
|
|
|
} |
322
|
7 |
|
if (isset($configuracao['token'])) { |
323
|
|
|
$this->setToken($configuracao['token']); |
324
|
|
|
} else { |
325
|
7 |
|
$this->setToken(null); |
326
|
|
|
} |
327
|
7 |
|
if (isset($configuracao['csc'])) { |
328
|
|
|
$this->setCSC($configuracao['csc']); |
329
|
|
|
} else { |
330
|
7 |
|
$this->setCSC(null); |
331
|
|
|
} |
332
|
7 |
|
if (isset($configuracao['token_ibpt'])) { |
333
|
|
|
$this->setTokenIBPT($configuracao['token_ibpt']); |
334
|
|
|
} else { |
335
|
7 |
|
$this->setTokenIBPT(null); |
336
|
|
|
} |
337
|
7 |
|
if (!isset($configuracao['tempo_limite']) || is_null($configuracao['tempo_limite'])) { |
338
|
7 |
|
$this->setTempoLimite(4); |
339
|
7 |
|
} else { |
340
|
1 |
|
$this->setTempoLimite($configuracao['tempo_limite']); |
341
|
|
|
} |
342
|
7 |
|
if (!isset($configuracao['sincrono']) || is_null($configuracao['sincrono'])) { |
343
|
7 |
|
$this->setSincrono('Y'); |
344
|
7 |
|
} else { |
345
|
1 |
|
$this->setSincrono($configuracao['sincrono']); |
346
|
|
|
} |
347
|
7 |
|
return $this; |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
|
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.