Passed
Push — master ( 159b0a...127407 )
by
unknown
52s queued 11s
created

Tools::sefazEvento()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 0
cts 50
cp 0
rs 9.0472
c 0
b 0
f 0
cc 2
nc 2
nop 5
crap 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace NFePHP\MDFe;
4
5
use NFePHP\Common\Signer;
6
use NFePHP\Common\Strings;
7
use NFePHP\Common\UFList;
8
use NFePHP\MDFe\Common\Tools as ToolsCommon;
9
10
/**
11
 * Classe principal para a comunicação com a SEFAZ
12
 *
13
 * @author    Cleiton Perin <cperin20 at gmail dot com>
14
 * @package   nfephp-org/sped-mdfe
15
 * @copyright 2008-2019 NFePHP
16
 * @license   http://www.gnu.org/licenses/lesser.html LGPL v3
17
 * @link      http://github.com/nfephp-org/sped-mdfe for the canonical source repository
18
 * @category  Library
19
 */
20
class Tools extends ToolsCommon
21
{
22
23
    /**
24
     * @author Cleiton Perin
25
     *
26
     * @param array $aXml
27
     * @param string $idLote
28
     * @param int $indSinc flag to use synchronous communication
29
     * @return   string
30
     * @throws   Exception\InvalidArgumentException
31
     */
32
    public function sefazEnviaLote(
33
        $aXml,
34
        $idLote = '',
35
        $indSinc = 0
36
    ) {
37
        if (!is_array($aXml)) {
38
            throw new \InvalidArgumentException('Os XML das MDFe devem ser passados em um array.');
39
        }
40
        if ($indSinc == 1 && count($aXml) > 1) {
41
            throw new \InvalidArgumentException('Envio sincrono deve ser usado para enviar '
42
                . 'uma UNICA mdfe por vez. Você está tentando enviar varias.');
43
        }
44
        $servico = 'MDFeRecepcao';
45
        if ($indSinc == 1) {
46
            $servico = 'MDFeRecepcaoSinc';
47
        }
48
        $sxml = implode("", $aXml);
49
        $sxml = preg_replace("/<\?xml.*?\?>/", "", $sxml);
50
        $this->servico(
51
            $servico,
52
            $this->config->siglaUF,
53
            $this->tpAmb
54
        );
55
        $request = "<enviMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
56
            . "<idLote>$idLote</idLote>"
57
            . "$sxml"
58
            . "</enviMDFe>";
59
        $this->isValid($this->urlVersion, $request, 'enviMDFe');
60
        $this->lastRequest = $request;
61
        if ($indSinc == 1) {
62
            $request = base64_encode(gzencode($sxml));
63
        }
64
        //montagem dos dados da mensagem SOAP
65
        $parameters = ['mdfeDadosMsg' => $request];
66
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
67
        $this->lastResponse = $this->sendRequest($body, $parameters);
68
        return $this->lastResponse;
69
    }
70
71
    /**
72
     * @author Cleiton Perin
73
     *
74
     * @param string $recibo
75
     * @param string $tpAmb
76
     * @return   string
77
     */
78
    public function sefazConsultaRecibo($recibo, $tpAmb = null)
79
    {
80
        if (empty($tpAmb)) {
81
            $tpAmb = $this->tpAmb;
82
        }
83
        //carrega serviço
84
        $servico = 'MDFeRetRecepcao';
85
        $this->servico(
86
            $servico,
87
            $this->config->siglaUF,
88
            $tpAmb
89
        );
90
        $request = "<consReciMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
91
            . "<tpAmb>$tpAmb</tpAmb>"
92
            . "<nRec>$recibo</nRec>"
93
            . "</consReciMDFe>";
94
        $this->isValid($this->urlVersion, $request, 'consReciMDFe');
95
        //montagem dos dados da mensagem SOAP
96
        $this->lastRequest = $request;
97
        $parameters = ['mdfeDadosMsg' => $request];
98
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
99
        $this->lastResponse = $this->sendRequest($body, $parameters);
100
        return $this->lastResponse;
101
    }
102
103
    /**
104
     * @author Cleiton Perin
105
     * Consulta o status da MDFe pela chave de 44 digitos
106
     *
107
     * @param string $chave
108
     * @param string $tpAmb
109
     * @return   string
110
     */
111
    public function sefazConsultaChave($chave, $tpAmb = null)
112
    {
113
        if (empty($tpAmb)) {
114
            $tpAmb = $this->tpAmb;
115
        }
116
        //carrega serviço
117
        $servico = 'MDFeConsulta';
118
        $this->servico(
119
            $servico,
120
            $this->config->siglaUF,
121
            $tpAmb
122
        );
123
        $request = "<consSitMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
124
            . "<tpAmb>$tpAmb</tpAmb>"
125
            . "<xServ>CONSULTAR</xServ>"
126
            . "<chMDFe>$chave</chMDFe>"
127
            . "</consSitMDFe>";
128
        $this->isValid($this->urlVersion, $request, 'consSitMDFe');
129
        $this->lastRequest = $request;
130
        $parameters = ['mdfeDadosMsg' => $request];
131
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
132
        $this->lastResponse = $this->sendRequest($body, $parameters);
133
        return $this->lastResponse;
134
    }
135
136
    /**
137
     * @author Cleiton Perin
138
     *
139
     * @param string $uf sigla da unidade da Federação
140
     * @param string $tpAmb tipo de ambiente 1-produção e 2-homologação
141
     * @return   mixed string XML do retorno do webservice, ou false se ocorreu algum erro
142
     */
143
    public function sefazStatus($uf = '', $tpAmb = null)
144
    {
145
        if (empty($tpAmb)) {
146
            $tpAmb = $this->tpAmb;
147
        }
148
        if (empty($uf)) {
149
            $uf = $this->config->siglaUF;
150
        }
151
        //carrega serviço
152
        $servico = 'MDFeStatusServico';
153
        $this->servico(
154
            $servico,
155
            $uf,
156
            $tpAmb
157
        );
158
        $request = "<consStatServMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
159
            . "<tpAmb>$tpAmb</tpAmb>"
160
            . "<xServ>STATUS</xServ></consStatServMDFe>";
161
        $this->isValid($this->urlVersion, $request, 'consStatServMDFe');
162
        $this->lastRequest = $request;
163
        $parameters = ['mdfeDadosMsg' => $request];
164
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
165
        $this->lastResponse = $this->sendRequest($body, $parameters);
166
        return $this->lastResponse;
167
    }
168
169
    /**
170
     * @author Cleiton Perin
171
     *
172
     * @param string $chave
173
     * @param string $xJust
174
     * @param string $nProt
175
     * @return string
176
     */
177
    public function sefazCancela($chave, $xJust, $nProt)
178
    {
179
        $xJust = Strings::replaceSpecialsChars(
180
            substr(trim($xJust), 0, 255)
181
        );
182
        $tpEvento = 110111;
183
        $nSeqEvento = 1;
184
        $tagAdic = "<evCancMDFe>"
185
            . "<descEvento>Cancelamento</descEvento>"
186
            . "<nProt>$nProt</nProt>"
187
            . "<xJust>$xJust</xJust>"
188
            . "</evCancMDFe>";
189
        return $this->sefazEvento(
190
            $this->config->siglaUF,
191
            $chave,
192
            $tpEvento,
193
            $nSeqEvento,
194
            $tagAdic
195
        );
196
    }
197
198
    /**
199
     * @author Cleiton Perin
200
     *
201
     * @param string $chave
202
     * @param string $nProt
203
     * @param string $cUF
204
     * @param string $cMun
205
     * @param string $dtEnc
206
     * @return string
207
     */
208
    public function sefazEncerra(
209
        $chave = '',
210
        $nProt = '',
211
        $cUF = '',
212
        $cMun = '',
213
        $dtEnc = ''
214
    ) {
215
216
217
218
        $tpEvento = 110112;
219
        $nSeqEvento = 1;
220
        if ($dtEnc == '') {
221
            $dtEnc = date('Y-m-d');
222
        }
223
        $tagAdic = "<evEncMDFe>"
224
            . "<descEvento>Encerramento</descEvento>"
225
            . "<nProt>$nProt</nProt>"
226
            . "<dtEnc>$dtEnc</dtEnc>"
227
            . "<cUF>$cUF</cUF>"
228
            . "<cMun>$cMun</cMun>"
229
            . "</evEncMDFe>";
230
        return $this->sefazEvento(
231
            $this->config->siglaUF,
232
            $chave,
233
            $tpEvento,
234
            $nSeqEvento,
235
            $tagAdic
236
        );
237
    }
238
239
    /**
240
     * @author Cleiton Perin
241
     *
242
     * @param string $chave
243
     * @param string $nSeqEvento
244
     * @param string $xNome
245
     * @param string $cpf
246
     * @return string
247
     */
248
    public function sefazIncluiCondutor(
249
        $chave = '',
250
        $nSeqEvento = '1',
251
        $xNome = '',
252
        $cpf = ''
253
    ) {
254
        $tpEvento = 110114;
255
        $tagAdic = "<evIncCondutorMDFe>"
256
            . "<descEvento>Inclusao Condutor</descEvento>"
257
            . "<condutor>"
258
            . "<xNome>$xNome</xNome>"
259
            . "<CPF>$cpf</CPF>"
260
            . "</condutor>"
261
            . "</evIncCondutorMDFe>";
262
263
        return $this->sefazEvento(
264
            $this->config->siglaUF,
265
            $chave,
266
            $tpEvento,
267
            $nSeqEvento,
268
            $tagAdic
269
        );
270
    }
271
272
    /**
273
     * @author Cleiton Perin
274
     *
275
     * @return string
276
     */
277
    public function sefazConsultaNaoEncerrados()
278
    {
279
        //carrega serviço
280
        $servico = 'MDFeConsNaoEnc';
281
        $this->servico(
282
            $servico,
283
            $this->config->siglaUF,
284
            $this->tpAmb
285
        );
286
        $cnpj = $this->config->cnpj;
287
        $request = "<consMDFeNaoEnc xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
288
            . "<tpAmb>$this->tpAmb</tpAmb>"
289
            . "<xServ>CONSULTAR NÃO ENCERRADOS</xServ>"
290
            . "<CNPJ>$cnpj</CNPJ>"
291
            . "</consMDFeNaoEnc>";
292
        $this->lastRequest = $request;
293
        $parameters = ['mdfeDadosMsg' => $request];
294
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
295
        $this->lastResponse = $this->sendRequest($body, $parameters);
296
        return $this->lastResponse;
297
    }
298
299
    /**
300
     * @author Cleiton Perin
301
     *
302
     * @param string $uf
303
     * @param string $chave
304
     * @param string $cOrgao
0 ignored issues
show
Bug introduced by
There is no parameter named $cOrgao. Was it maybe removed?

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 method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
305
     * @param string $tpEvento
306
     * @param string $nSeqEvento
307
     * @param string $tagAdic
308
     * @return   string
309
     */
310
    protected function sefazEvento(
311
        $uf,
312
        $chave,
313
        $tpEvento,
314
        $nSeqEvento = 1,
315
        $tagAdic = ''
316
    ) {
317
        //carrega serviço
318
        $servico = 'MDFeRecepcaoEvento';
319
        $this->servico(
320
            $servico,
321
            $uf,
322
            $this->tpAmb
323
        );
324
        $cnpj = $this->config->cnpj;
325
        $sigla = strlen($cnpj) == 11 ? 'CPF' : 'CNPJ';
326
        $dt = new \DateTime();
327
        $dhEvento = $dt->format('Y-m-d\TH:i:sP');
328
        $sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT);
329
        $eventId = "ID" . $tpEvento . $chave . $sSeqEvento;
330
        $cOrgao = UFList::getCodeByUF($uf);
331
        $request = "<eventoMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
332
            . "<infEvento Id=\"$eventId\">"
333
            . "<cOrgao>$cOrgao</cOrgao>"
334
            . "<tpAmb>$this->tpAmb</tpAmb>"
335
            . "<$sigla>$cnpj</$sigla>"
336
            . "<chMDFe>$chave</chMDFe>"
337
            . "<dhEvento>$dhEvento</dhEvento>"
338
            . "<tpEvento>$tpEvento</tpEvento>"
339
            . "<nSeqEvento>$nSeqEvento</nSeqEvento>"
340
            . "<detEvento versaoEvento=\"$this->urlVersion\">"
341
            . "$tagAdic"
342
            . "</detEvento>"
343
            . "</infEvento>"
344
            . "</eventoMDFe>";
345
        //assinatura dos dados
346
        $request = Signer::sign(
347
            $this->certificate,
348
            $request,
349
            'infEvento',
350
            'Id',
351
            $this->algorithm,
352
            $this->canonical
353
        );
354
        $request = Strings::clearXmlString($request, true);
355
        $this->isValid($this->urlVersion, $request, 'eventoMDFe');
356
        $this->lastRequest = $request;
357
        $parameters = ['mdfeDadosMsg' => $request];
358
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
359
        $this->lastResponse = $this->sendRequest($body, $parameters);
360
        return $this->lastResponse;
361
    }
362
363
    /**
364
     * Service for the distribution of summary information and
365
     * electronic tax documents of interest to an actor.
366
     * @param integer $ultNSU  last NSU number recived
367
     * @param integer $numNSU  NSU number you wish to consult
368
     * @return string
369
     */
370
    public function sefazDistDFe(
371
        $ultNSU = 0,
372
        $numNSU = 0
373
    ) {
374
        //carrega serviço
375
        $servico = 'MDFeDistribuicaoDFe';
376
        $this->servico(
377
            $servico,
378
            $this->config->siglaUF,
379
            $this->tpAmb
380
        );
381
        $ultNSU = str_pad($ultNSU, 15, '0', STR_PAD_LEFT);
382
        $tagNSU = "<distNSU><ultNSU>$ultNSU</ultNSU></distNSU>";
383
        if ($numNSU != 0) {
384
            $numNSU = str_pad($numNSU, 15, '0', STR_PAD_LEFT);
385
            $tagNSU = "<consNSU><NSU>$numNSU</NSU></consNSU>";
386
        }
387
        //monta a consulta
388
        $request = "<distDFeInt xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
389
            . "<tpAmb>".$this->tpAmb."</tpAmb>"
390
            . ((strlen($this->config->cnpj)==14) ?
391
                "<CNPJ>".$this->config->cnpj."</CNPJ>" :
392
                "<CPF>".$this->config->cnpj."</CPF>"
393
            )
394
            . $tagNSU."</distDFeInt>";
395
        //valida o xml da requisição
396
        $this->isValid($this->urlVersion, $request, 'distDFeInt');
397
        $this->lastRequest = $request;
398
        //montagem dos dados da mensagem SOAP
399
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
400
        $parameters = ['mdfeDadosMsg' => $request];
401
        $this->lastResponse = $this->sendRequest($body, $parameters);
402
        return $this->lastResponse;
403
    }
404
}
405