1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\Esfinge; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use NFePHP\Esfinge\Response; |
8
|
|
|
use NFePHP\Esfinge\Base; |
9
|
|
|
|
10
|
|
|
class Tools extends Base |
11
|
|
|
{ |
12
|
|
|
const TK_Obtem = 'O'; |
13
|
|
|
const TK_Inicia = 'I'; |
14
|
|
|
const TK_Finaliza = 'F'; |
15
|
|
|
const TK_Cancela = 'C'; |
16
|
|
|
const TK_Status = 'S'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Endereços principais dos webservices |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $url = [ |
23
|
|
|
'1' => 'https://esfingews.tce.sc.gov.br', |
24
|
|
|
'2' => 'https://desenv2.tce.sc.gov.br:7443', |
25
|
|
|
]; |
26
|
|
|
/** |
27
|
|
|
* Competência bimestral no formato: AAAABB, onde: |
28
|
|
|
* AAAA = ano a ser enviado os dados |
29
|
|
|
* BB = bimestre de 01 até 06 |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $competencia; |
33
|
|
|
/** |
34
|
|
|
* Token de segurança e queue |
35
|
|
|
* hash com 36 caracteres aleatórios |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $tokenid; |
39
|
|
|
/** |
40
|
|
|
* Flag iniciar tranferencia |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $flagIniciar = false; |
44
|
|
|
/** |
45
|
|
|
* Datahora da ultima solicitação da situação do token |
46
|
|
|
* @var timestramp |
47
|
|
|
*/ |
48
|
|
|
protected $tsLastSitToken; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Construtor |
52
|
|
|
* @param string $configJson |
53
|
|
|
*/ |
54
|
6 |
|
public function __construct($configJson = '') |
55
|
|
|
{ |
56
|
6 |
|
parent::__construct($configJson); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Define o período de competência das informações |
61
|
|
|
* formado AAAABB sendo BB o bimestre de 01 até 06 |
62
|
|
|
* @param string $valor |
|
|
|
|
63
|
|
|
*/ |
64
|
9 |
|
public function setCompetencia($aaaabb) |
65
|
|
|
{ |
66
|
9 |
|
if (!is_numeric($aaaabb)) { |
67
|
3 |
|
throw new InvalidArgumentException('O periodo de competência é uma informação APENAS numérica.'); |
68
|
|
|
} |
69
|
6 |
|
$bm = intval(substr($aaaabb, -2)); |
70
|
6 |
|
if ($bm > 6 || $bm <= 0) { |
71
|
3 |
|
throw new InvalidArgumentException('O bimestre pode ser de 01 até 06 APENAS.'); |
72
|
|
|
} |
73
|
3 |
|
$this->competencia = $aaaabb; |
|
|
|
|
74
|
3 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Retorna o período de competência informado |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getCompetencia() |
81
|
|
|
{ |
82
|
3 |
|
return $this->competencia; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Operações com token |
87
|
|
|
* O método pode ser: |
88
|
|
|
* C - cancela a transferencia |
89
|
|
|
* F - finaliza a transferencia |
90
|
|
|
* I - inica a transferencia |
91
|
|
|
* O - Obtem o token para realizar as operações |
92
|
|
|
* S - Verifica a situação do token |
93
|
|
|
* @param string $method |
94
|
|
|
*/ |
95
|
|
|
public function token($method = self::TK_Obtem) |
96
|
|
|
{ |
97
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/tokenWS'; |
98
|
|
|
$namespace = 'http://token.ws.tce.sc.gov.br/'; |
99
|
|
|
|
100
|
|
|
switch ($method) { |
101
|
|
View Code Duplication |
case self::TK_Cancela: |
|
|
|
|
102
|
|
|
//cancela as operações realizadas com um determinado token |
103
|
|
|
//se OK o token é removido e todas as operações com ele |
104
|
|
|
//realizadas são descartadas |
105
|
|
|
if ($this->flagIniciar === false) { |
106
|
|
|
//não está iniciada a tranferencia então não dá para cancelar |
107
|
|
|
throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser cancelada'); |
108
|
|
|
} |
109
|
|
|
$met = 'cancelarTransferencia'; |
110
|
|
|
$body = "<svc:cancelarTransferencia>" |
111
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
112
|
|
|
. "</svc:cancelarTransferencia>"; |
113
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
114
|
|
|
$resp = Response::readReturn($met, $retorno); |
115
|
|
|
if ($resp['bStat']) { |
116
|
|
|
//cancelamento aceito |
117
|
|
|
$this->tokenid = ''; |
118
|
|
|
$this->flagIniciar = false; |
119
|
|
|
} |
120
|
|
|
break; |
121
|
|
View Code Duplication |
case self::TK_Finaliza: |
|
|
|
|
122
|
|
|
//Ao final da transferência caso queria confirmar todos os elementos inseridos |
123
|
|
|
//(que não retornaram erro) nesta sessão, ou seja todos os elementos ligados a |
124
|
|
|
//determinado token passado para o serviço. Uma vez executado este serviço |
125
|
|
|
//o token atual será descartado. |
126
|
|
|
if ($this->flagIniciar === false) { |
127
|
|
|
//não está iniciada a tranferencia então não dá para finalizar |
128
|
|
|
throw new RuntimeException('A tranferencia não foi iniciada, então não pode ser finalizada'); |
129
|
|
|
} |
130
|
|
|
$met = 'finalizarTransferencia'; |
131
|
|
|
$body = "<svc:finalizarTransferencia>" |
132
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
133
|
|
|
. "</svc:finalizarTransferencia>"; |
134
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
135
|
|
|
$resp = Response::readReturn($met, $retorno); |
136
|
|
|
if ($resp['bStat']) { |
137
|
|
|
//finalização aceita |
138
|
|
|
$this->tokenid = ''; |
139
|
|
|
$this->flagIniciar = false; |
140
|
|
|
} |
141
|
|
|
break; |
142
|
|
|
case self::TK_Inicia: |
143
|
|
|
//Antes de iniciar a transferência dos dados propriamente dita, será necessário executar |
144
|
|
|
//o serviço iniciarTransferencia |
145
|
|
|
if ($this->tokenid == '') { |
146
|
|
|
//não é possivel iniciar sem um token valido |
147
|
|
|
throw new RuntimeException('Não é possivel iniciar a tranferência sem um token valido'); |
148
|
|
|
//$this->token(self::TK_O); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
if ($this->flagIniciar === true) { |
151
|
|
|
$resp = [ |
152
|
|
|
'bStat' => true, |
153
|
|
|
'message' => 'Início de transferência liberado', |
154
|
|
|
'status' => 'OK' |
155
|
|
|
]; |
156
|
|
|
break; |
157
|
|
|
} |
158
|
|
|
$met = 'iniciarTransferencia'; |
159
|
|
|
$body = "<svc:iniciarTransferencia>" |
160
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
161
|
|
|
. "</svc:iniciarTransferencia>"; |
162
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
163
|
|
|
$resp = Response::readReturn($met, $retorno); |
164
|
|
|
if ($resp['bStat']) { |
165
|
|
|
$this->flagIniciar = true; |
166
|
|
|
} |
167
|
|
|
break; |
168
|
|
|
case self::TK_Obtem: |
169
|
|
|
//Retorna um token para a unidade gestora poder usar o serviço do TCE. |
170
|
|
|
//Permite somente um token por unidade gestora. |
171
|
|
View Code Duplication |
if ($this->tokenid != '') { |
|
|
|
|
172
|
|
|
$resp = [ |
173
|
|
|
'bStat' => true, |
174
|
|
|
'message' => 'Token criado com sucesso', |
175
|
|
|
'status' => 'OK', |
176
|
|
|
'chaveToken' => $this->tokenid, |
177
|
|
|
'posicao' => 2, |
178
|
|
|
'situacao' => 'Pronto para envio ou consulta' |
179
|
|
|
]; |
180
|
|
|
break; |
181
|
|
|
} |
182
|
|
|
$met = 'obterToken'; |
183
|
|
|
$body = "<svc:obterToken>" |
184
|
|
|
. "<codigoUg>$this->codigoUnidadeGestora</codigoUg>" |
185
|
|
|
. "</svc:obterToken>"; |
186
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
187
|
|
|
$resp = Response::readReturn($met, $retorno); |
188
|
|
|
if ($resp['bStat'] && $resp['chaveToken'] != '') { |
189
|
|
|
$this->tokenid = $resp['chaveToken']; |
190
|
|
|
} |
191
|
|
|
break; |
192
|
|
|
case self::TK_Status: |
193
|
|
|
//Retorna a situação do token passado como parâmetro. Para evitar solicitações |
194
|
|
|
//indefinidas a este serviço o sistema punirá com a remoção do token da fila |
195
|
|
|
//sempre que for feita duas chamadas seguidas do serviço obterSituacaoToken |
196
|
|
|
//em menos de cinco segundos. |
197
|
|
|
if ($this->tokenid == '') { |
198
|
|
|
//não é possivel verificar o token |
199
|
|
|
throw new RuntimeException('Não existe um token aberto.'); |
200
|
|
|
} |
201
|
|
|
//se tentativa de verificação ocorrer em menos de 2 seg |
202
|
|
|
//retorna como OK |
203
|
|
View Code Duplication |
if ((time()-$this->tsLastSitToken) <= 2) { |
|
|
|
|
204
|
|
|
$resp = [ |
205
|
|
|
'bStat' => true, |
206
|
|
|
'message' => 'Situação token obtida com sucesso', |
207
|
|
|
'status' => 'OK', |
208
|
|
|
'posicao' => 1, |
209
|
|
|
'situacao' => 'Pronto para envio ou consulta' |
210
|
|
|
]; |
211
|
|
|
break; |
212
|
|
|
} |
213
|
|
|
$met = 'obterSituacaoToken'; |
214
|
|
|
$body = "<svc:obterSituacaoToken>" |
215
|
|
|
. "<chaveToken>$this->tokenid</chaveToken>" |
216
|
|
|
. "</svc:obterSituacaoToken>"; |
217
|
|
|
$retorno = $this->oSoap->send($uri, $namespace, $this->header, $body, $met); |
218
|
|
|
$resp = Response::readReturn($met, $retorno); |
219
|
|
|
$this->tsLastSitToken = time(); |
|
|
|
|
220
|
|
|
break; |
221
|
|
|
} |
222
|
|
|
return $resp; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Inicia o processo de tranferência de dados |
227
|
|
|
* @param array $data |
228
|
|
|
* @throws InvalidArgumentException |
229
|
|
|
* @throws RuntimeException |
230
|
|
|
*/ |
231
|
|
|
protected function obterTokenIniciarTransferencia($data = array()) |
232
|
|
|
{ |
233
|
|
|
if (empty($data)) { |
234
|
|
|
throw new InvalidArgumentException('Não foram passados dados para o método'); |
235
|
|
|
} |
236
|
|
|
$this->token(self::TK_Obtem); |
237
|
|
|
$this->token(self::TK_Inicia); |
238
|
|
|
if ($this->tokenid == '' || $this->flagIniciar === false) { |
239
|
|
|
throw new RuntimeException("Falha token:$this->tokenid , Iniciar: $this->flagIniciar"); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Servidor |
245
|
|
|
* @param array $data |
246
|
|
|
* @param string $method |
247
|
|
|
* @return array |
248
|
|
|
*/ |
249
|
|
View Code Duplication |
public function servidor($data = array(), $method = 'L') |
|
|
|
|
250
|
|
|
{ |
251
|
|
|
$this->obterTokenIniciarTransferencia($data); |
252
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/servidorWS'; |
253
|
|
|
$namespace = 'http://servidor.ws.tce.sc.gov.br/'; |
254
|
|
|
$met = 'servidor'.$method; |
255
|
|
|
//envia a mensagem via cURL |
256
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
257
|
|
|
return $resp; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Situação Servidor Folha Pagamento |
262
|
|
|
* @param array $data |
263
|
|
|
* @param string $method |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
View Code Duplication |
public function situacaoServidorFolhaPagamento($data = array(), $method = 'L') |
|
|
|
|
267
|
|
|
{ |
268
|
|
|
$this->obterTokenIniciarTransferencia($data); |
269
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/situacaoServidorFolhaPagamentoWS'; |
270
|
|
|
$namespace = 'http://situacaoservidorfolhapagamento.ws.tce.sc.gov.br/'; |
271
|
|
|
$met = 'situacaoServidorFolhaPagamento'.$method; |
272
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
273
|
|
|
return $resp; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Componentes Folha Pagamento |
278
|
|
|
* @param array $data |
279
|
|
|
* @param string $method |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
View Code Duplication |
public function componentesFolhaPagamento($data = array(), $method = 'L') |
|
|
|
|
283
|
|
|
{ |
284
|
|
|
$this->obterTokenIniciarTransferencia($data); |
285
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/componentesFolhaPagamentoWS'; |
286
|
|
|
$namespace = 'http://componentesfolhapagamento.ws.tce.sc.gov.br/'; |
287
|
|
|
$met = 'componentesFolhaPagamento'.$method; |
288
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
289
|
|
|
return $resp; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Folha Pagamento |
294
|
|
|
* @param array $data |
295
|
|
|
* @param string $method |
296
|
|
|
* @return array |
297
|
|
|
*/ |
298
|
|
View Code Duplication |
public function folhaPagamento($data = array(), $method = 'L') |
|
|
|
|
299
|
|
|
{ |
300
|
|
|
$this->obterTokenIniciarTransferencia($data); |
301
|
|
|
$uri = $this->url[$this->tpAmb].'/esfinge/services/folhaPagamentoWS'; |
302
|
|
|
$namespace = 'http://folhapagamento.ws.tce.sc.gov.br/'; |
303
|
|
|
$met = 'folhaPagamento'.$method; |
304
|
|
|
$resp = $this->envia($uri, $namespace, $data, $method, $met); |
305
|
|
|
return $resp; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.