Completed
Pull Request — master (#83)
by
unknown
11:11
created

Tools::zSefazEvento()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 77
ccs 0
cts 55
cp 0
rs 8.5018
c 0
b 0
f 0
cc 4
nc 6
nop 7
crap 20

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 3
     * @return   string
29
     * @throws   Exception\InvalidArgumentException
30
     */
31
    public function sefazEnviaLote(
32
        $aXml,
33
        $idLote = ''
34
    ) {
35
36
        if (!is_array($aXml)) {
37
            throw new \InvalidArgumentException('Os XML das MDFe devem ser passados em um array.');
38
        }
39
        $servico = 'MDFeRecepcao';
40
        $sxml = implode("", $aXml);
41
        $sxml = preg_replace("/<\?xml.*?\?>/", "", $sxml);
42
        $this->servico(
43
            $servico,
44
            $this->config->siglaUF,
45
            $this->tpAmb
46
        );
47
        $request = "<enviMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
48
            . "<idLote>$idLote</idLote>"
49
            . "$sxml"
50
            . "</enviMDFe>";
51
        $this->isValid($this->urlVersion, $request, 'enviMDFe');
52
        $this->lastRequest = $request;
53
        //montagem dos dados da mensagem SOAP
54
        $parameters = ['mdfeDadosMsg' => $request];
55
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
56
        $this->lastResponse = $this->sendRequest($body, $parameters);
57
        return $this->lastResponse;
58
    }
59
60
    /**
61
     * @author Cleiton Perin
62
     *
63
     * @param string $recibo
64
     * @param string $tpAmb
65
     * @return   string
66
     */
67 View Code Duplication
    public function sefazConsultaRecibo($recibo, $tpAmb = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        if (empty($tpAmb)) {
70
            $tpAmb = $this->tpAmb;
71
        }
72
        //carrega serviço
73
        $servico = 'MDFeRetRecepcao';
74
        $this->servico(
75
            $servico,
76
            $this->config->siglaUF,
77
            $tpAmb
78
        );
79
        $request = "<consReciMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
80
            . "<tpAmb>$tpAmb</tpAmb>"
81
            . "<nRec>$recibo</nRec>"
82
            . "</consReciMDFe>";
83
        $this->isValid($this->urlVersion, $request, 'consReciMDFe');
84
        //montagem dos dados da mensagem SOAP
85
        $this->lastRequest = $request;
86
        $parameters = ['mdfeDadosMsg' => $request];
87
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
88
        $this->lastResponse = $this->sendRequest($body, $parameters);
89
        return $this->lastResponse;
90
    }
91
92
    /**
93
     * @author Cleiton Perin
94
     * Consulta o status da MDFe pela chave de 44 digitos
95
     *
96
     * @param string $chave
97
     * @param string $tpAmb
98
     * @return   string
99
     */
100 View Code Duplication
    public function sefazConsultaChave($chave, $tpAmb = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        if (empty($tpAmb)) {
103
            $tpAmb = $this->tpAmb;
104
        }
105
        //carrega serviço
106
        $servico = 'MDFeConsulta';
107
        $this->servico(
108
            $servico,
109
            $this->config->siglaUF,
110
            $tpAmb
111
        );
112
        $request = "<consSitMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
113
            . "<tpAmb>$tpAmb</tpAmb>"
114
            . "<xServ>CONSULTAR</xServ>"
115
            . "<chMDFe>$chave</chMDFe>"
116
            . "</consSitMDFe>";
117
        $this->isValid($this->urlVersion, $request, 'consSitMDFe');
118
        $this->lastRequest = $request;
119
        $parameters = ['mdfeDadosMsg' => $request];
120
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
121
        $this->lastResponse = $this->sendRequest($body, $parameters);
122
        return $this->lastResponse;
123
    }
124
125
    /**
126
     * @author Cleiton Perin
127
     *
128
     * @param string $uf sigla da unidade da Federação
129
     * @param string $tpAmb tipo de ambiente 1-produção e 2-homologação
130
     * @return   mixed string XML do retorno do webservice, ou false se ocorreu algum erro
131
     */
132 View Code Duplication
    public function sefazStatus($uf = '', $tpAmb = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
    {
134
        if (empty($tpAmb)) {
135
            $tpAmb = $this->tpAmb;
136
        }
137
        if (empty($uf)) {
138
            $uf = $this->config->siglaUF;
139
        }
140
        //carrega serviço
141
        $servico = 'MDFeStatusServico';
142
        $this->servico(
143
            $servico,
144
            $uf,
145
            $tpAmb
146
        );
147
        $request = "<consStatServMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
148
            . "<tpAmb>$tpAmb</tpAmb>"
149
            . "<xServ>STATUS</xServ></consStatServMDFe>";
150
        $this->isValid($this->urlVersion, $request, 'consStatServMDFe');
151
        $this->lastRequest = $request;
152
        $parameters = ['mdfeDadosMsg' => $request];
153
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
154
        $this->lastResponse = $this->sendRequest($body, $parameters);
155
        return $this->lastResponse;
156
    }
157
158
    /**
159
     * @author Cleiton Perin
160
     *
161
     * @param string $chave
162
     * @param string $xJust
163
     * @param string $nProt
164
     * @return string
165
     */
166
    public function sefazCancela($chave, $xJust, $nProt)
167
    {
168
        $xJust = Strings::replaceSpecialsChars(
169
            substr(trim($xJust), 0, 255)
170
        );
171
        $tpEvento = 110111;
172
        $nSeqEvento = 1;
173
        $tagAdic = "<evCancMDFe>"
174
            . "<descEvento>Cancelamento</descEvento>"
175
            . "<nProt>$nProt</nProt>"
176
            . "<xJust>$xJust</xJust>"
177
            . "</evCancMDFe>";
178
        return $this->sefazEvento(
179
            $this->config->siglaUF,
180
            $chave,
181
            $tpEvento,
182
            $nSeqEvento,
183
            $tagAdic
184
        );
185
    }
186
187
    /**
188
     * @author Cleiton Perin
189
     *
190
     * @param string $chave
191
     * @param string $nProt
192
     * @param string $cUF
193
     * @param string $cMun
194
     * @return string
195
     */
196
    public function sefazEncerra(
197
        $chave = '',
198
        $nProt = '',
199
        $cUF = '',
200
        $cMun = ''
201
    ) {
202
203
204
        $tpEvento = 110112;
205
        $nSeqEvento = 1;
206
        $dtEnc = date('Y-m-d');
207
        $tagAdic = "<evEncMDFe>"
208
            . "<descEvento>Encerramento</descEvento>"
209
            . "<nProt>$nProt</nProt>"
210
            . "<dtEnc>$dtEnc</dtEnc>"
211
            . "<cUF>$cUF</cUF>"
212
            . "<cMun>$cMun</cMun>"
213
            . "</evEncMDFe>";
214
        return $this->sefazEvento(
215
            $this->config->siglaUF,
216
            $chave,
217
            $tpEvento,
218
            $nSeqEvento,
219
            $tagAdic
220
        );
221
    }
222
223
    /**
224
     * @author Cleiton Perin
225
     *
226
     * @param string $chave
227
     * @param string $nSeqEvento
228
     * @param string $xNome
229
     * @param string $cpf
230
     * @return string
231
     */
232
    public function sefazIncluiCondutor(
233
        $chave = '',
234
        $nSeqEvento = '1',
235
        $xNome = '',
236
        $cpf = ''
237
    ) {
238
239
240
        $tpEvento = 110114;
241
        $tagAdic = "<evIncCondutorMDFe>"
242
            . "<descEvento>Inclusao Condutor</descEvento>"
243
            . "<condutor>"
244
            . "<xNome>$xNome</xNome>"
245
            . "<CPF>$cpf</CPF>"
246
            . "</condutor>"
247
            . "</evIncCondutorMDFe>";
248
        return $this->sefazEvento(
249
            $this->config->siglaUF,
250
            $chave,
251
            $tpEvento,
252
            $nSeqEvento,
253
            $tagAdic
254
        );
255
    }
256
257
    /**
258
     * @author Cleiton Perin
259
     *
260
     * @return string
261
     */
262
    public function sefazConsultaNaoEncerrados()
263
    {
264
        //carrega serviço
265
        $servico = 'MDFeConsNaoEnc';
266
        $this->servico(
267
            $servico,
268
            $this->config->siglaUF,
269
            $this->tpAmb
270
        );
271
        $cnpj = $this->config->cnpj;
272
        $request = "<consMDFeNaoEnc xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
273
            . "<tpAmb>$this->tpAmb</tpAmb>"
274
            . "<xServ>CONSULTAR NÃO ENCERRADOS</xServ>"
275
            . "<CNPJ>$cnpj</CNPJ>"
276
            . "</consMDFeNaoEnc>";
277
        $this->lastRequest = $request;
278
        $parameters = ['mdfeDadosMsg' => $request];
279
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
280
        $this->lastResponse = $this->sendRequest($body, $parameters);
281
        return $this->lastResponse;
282
    }
283
284
    /**
285
     * @author Cleiton Perin
286
     *
287
     * @param string $uf
288
     * @param string $chave
289
     * @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...
290
     * @param string $tpEvento
291
     * @param string $nSeqEvento
292
     * @param string $tagAdic
293
     * @return   string
294
     */
295
    protected function sefazEvento(
296
        $uf,
297
        $chave,
298
        $tpEvento,
299
        $nSeqEvento = 1,
300
        $tagAdic = ''
301
    ) {
302
303
304
        //carrega serviço
305
        $servico = 'MDFeRecepcaoEvento';
306
        $this->servico(
307
            $servico,
308
            $uf,
309
            $this->tpAmb
310
        );
311
        $cnpj = $this->config->cnpj;
312
        $dt = new \DateTime();
313
        $dhEvento = $dt->format('Y-m-d\TH:i:sP');
314
        $sSeqEvento = str_pad($nSeqEvento, 2, "0", STR_PAD_LEFT);
315
        $eventId = "ID" . $tpEvento . $chave . $sSeqEvento;
316
        $cOrgao = UFList::getCodeByUF($uf);
317
        $request = "<eventoMDFe xmlns=\"$this->urlPortal\" versao=\"$this->urlVersion\">"
318
            . "<infEvento Id=\"$eventId\">"
319
            . "<cOrgao>$cOrgao</cOrgao>"
320
            . "<tpAmb>$this->tpAmb</tpAmb>"
321
            . "<CNPJ>$cnpj</CNPJ>"
322
            . "<chMDFe>$chave</chMDFe>"
323
            . "<dhEvento>$dhEvento</dhEvento>"
324
            . "<tpEvento>$tpEvento</tpEvento>"
325
            . "<nSeqEvento>$nSeqEvento</nSeqEvento>"
326
            . "<detEvento versaoEvento=\"$this->urlVersion\">"
327
            . "$tagAdic"
328
            . "</detEvento>"
329
            . "</infEvento>"
330
            . "</eventoMDFe>";
331
        //assinatura dos dados
332
        $request = Signer::sign(
333
            $this->certificate,
334
            $request,
335
            'infEvento',
336
            'Id',
337
            $this->algorithm,
338
            $this->canonical
339
        );
340
        $request = Strings::clearXmlString($request, true);
341
        $this->isValid($this->urlVersion, $request, 'eventoMDFe');
342
        $this->lastRequest = $request;
343
        $parameters = ['mdfeDadosMsg' => $request];
344
        $body = "<mdfeDadosMsg xmlns=\"$this->urlNamespace\">$request</mdfeDadosMsg>";
345
        $this->lastResponse = $this->sendRequest($body, $parameters);
346
        return $this->lastResponse;
347
    }
348
}
349