Passed
Push — master ( 97ea75...e3009b )
by
unknown
45s
created

Complements::cancelRegister()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 0
cts 51
cp 0
rs 8.425
c 0
b 0
f 0
cc 6
nc 4
nop 2
crap 42

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\CTe;
4
5
use NFePHP\Common\Strings;
6
use NFePHP\CTe\Common\Standardize;
7
use NFePHP\CTe\Exception\DocumentsException;
8
use DOMDocument;
9
10
class Complements
11
{
12
    protected static $urlPortal = 'http://www.portalfiscal.inf.br/cte';
13
14
    /**
15
     * Authorize document adding his protocol
16
     * @param string $request
17
     * @param string $response
18
     * @return string
19
     */
20
    public static function toAuthorize($request, $response)
21
    {
22
        $st = new Standardize();
23
        $key = ucfirst($st->whichIs($request));
24
        if ($key != 'CTe' && $key != 'CTeOS' && $key != 'EventoCTe' && $key != 'InutCTe') {
25
            //wrong document, this document is not able to recieve a protocol
26
            throw DocumentsException::wrongDocument(0, $key);
27
        }
28
        $func = "add" . $key . "Protocol";
29
        return self::$func($request, $response);
30
    }
31
32
    /**
33
     * Authorize Inutilization of numbers
34
     * @param string $request
35
     * @param string $response
36
     * @return string
37
     * @throws InvalidArgumentException
38
     */
39
    protected static function addInutCTeProtocol($request, $response)
40
    {
41
        $req = new DOMDocument('1.0', 'UTF-8');
42
        $req->preserveWhiteSpace = false;
43
        $req->formatOutput = false;
44
        $req->loadXML($request);
45
        $inutCTe = $req->getElementsByTagName('inutCTe')->item(0);
46
        $versao = $inutCTe->getAttribute("versao");
47
        $infInut = $req->getElementsByTagName('infInut')->item(0);
48
        $tpAmb = $infInut->getElementsByTagName('tpAmb')->item(0)->nodeValue;
49
        $cUF = $infInut->getElementsByTagName('cUF')->item(0)->nodeValue;
50
        $ano = $infInut->getElementsByTagName('ano')->item(0)->nodeValue;
51
        $cnpj = $infInut->getElementsByTagName('CNPJ')->item(0)->nodeValue;
52
        $mod = $infInut->getElementsByTagName('mod')->item(0)->nodeValue;
53
        $serie = $infInut->getElementsByTagName('serie')->item(0)->nodeValue;
54
        $nCTIni = $infInut->getElementsByTagName('nCTIni')->item(0)->nodeValue;
55
        $nCTFin = $infInut->getElementsByTagName('nCTFin')->item(0)->nodeValue;
56
57
        $ret = new DOMDocument('1.0', 'UTF-8');
58
        $ret->preserveWhiteSpace = false;
59
        $ret->formatOutput = false;
60
        $ret->loadXML($response);
61
        $retInutCTe = $ret->getElementsByTagName('retInutCTe')->item(0);
62
        if (!isset($retInutCTe)) {
63
            throw DocumentsException::wrongDocument(3, "&lt;retInutCTe;");
64
        }
65
        $retversao = $retInutCTe->getAttribute("versao");
66
        $retInfInut = $ret->getElementsByTagName('infInut')->item(0);
67
        $cStat = $retInfInut->getElementsByTagName('cStat')->item(0)->nodeValue;
68
        $xMotivo = $retInfInut->getElementsByTagName('xMotivo')->item(0)->nodeValue;
69
        if ($cStat != 102) {
70
            throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo.");
71
        }
72
        $rettpAmb = $retInfInut->getElementsByTagName('tpAmb')->item(0)->nodeValue;
73
        $retcUF = $retInfInut->getElementsByTagName('cUF')->item(0)->nodeValue;
74
        $retano = $retInfInut->getElementsByTagName('ano')->item(0)->nodeValue;
75
        $retcnpj = $retInfInut->getElementsByTagName('CNPJ')->item(0)->nodeValue;
76
        $retmod = $retInfInut->getElementsByTagName('mod')->item(0)->nodeValue;
77
        $retserie = $retInfInut->getElementsByTagName('serie')->item(0)->nodeValue;
78
        $retnCTIni = $retInfInut->getElementsByTagName('nCTIni')->item(0)->nodeValue;
79
        $retnCTFin = $retInfInut->getElementsByTagName('nCTFin')->item(0)->nodeValue;
80
        if (
81
            $versao != $retversao ||
82
            $tpAmb != $rettpAmb ||
83
            $cUF != $retcUF ||
84
            $ano != $retano ||
85
            $cnpj != $retcnpj ||
86
            $mod != $retmod ||
87
            $serie != $retserie ||
88
            $nCTIni != $retnCTIni ||
89
            $nCTFin != $retnCTFin
90
        ) {
91
            throw DocumentsException::wrongDocument(5);
92
        }
93
        return self::join(
94
            $req->saveXML($inutCTe),
95
            $ret->saveXML($retInutCTe),
96
            'procInutCTe',
97
            $versao
98
        );
99
    }
100
101
    /**
102
     * Authorize CTe
103
     * @param string $request
104
     * @param string $response
105
     * @return string
106
     * @throws InvalidArgumentException
107
     */
108
    protected static function addCTeProtocol($request, $response)
109
    {
110
        $req = new DOMDocument('1.0', 'UTF-8');
111
        $req->preserveWhiteSpace = false;
112
        $req->formatOutput = false;
113
        $req->loadXML($request);
114
115
        $cte = $req->getElementsByTagName('CTe')->item(0);
116
        $infCTe = $req->getElementsByTagName('infCte')->item(0);
117
        $versao = $infCTe->getAttribute("versao");
118
        $chave = preg_replace('/[^0-9]/', '', $infCTe->getAttribute("Id"));
0 ignored issues
show
Unused Code introduced by
$chave is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
        $digCTe = $req->getElementsByTagName('DigestValue')
120
            ->item(0)
121
            ->nodeValue;
122
123
        $ret = new DOMDocument('1.0', 'UTF-8');
124
        $ret->preserveWhiteSpace = false;
125
        $ret->formatOutput = false;
126
        $ret->loadXML($response);
127
        $retProt = $ret->getElementsByTagName('protCTe')->item(0);
128
        if (!isset($retProt)) {
129
            throw DocumentsException::wrongDocument(3, "&lt;protCTe&gt;");
130
        }
131
        $infProt = $ret->getElementsByTagName('infProt')->item(0);
132
        $cStat  = $infProt->getElementsByTagName('cStat')->item(0)->nodeValue;
133
        $xMotivo = $infProt->getElementsByTagName('xMotivo')->item(0)->nodeValue;
134
        $dig = $infProt->getElementsByTagName("digVal")->item(0);
135
        $digProt = '000';
136
        if (isset($dig)) {
137
            $digProt = $dig->nodeValue;
138
        }
139
        //100 Autorizado
140
        //150 Autorizado fora do prazo
141
        //110 Uso Denegado
142
        //205 CTe Denegada
143
        $cstatpermit = ['100', '150', '110', '205'];
144
        if (!in_array($cStat, $cstatpermit)) {
145
            throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
146
        }
147
        if ($digCTe !== $digProt) {
148
            throw DocumentsException::wrongDocument(5, "O digest é diferente");
149
        }
150
        return self::join(
151
            $req->saveXML($cte),
152
            $ret->saveXML($retProt),
153
            'cteProc',
154
            $versao
155
        );
156
    }
157
158
    /**
159
     * Authorize CTeOS
160
     * @param string $request
161
     * @param string $response
162
     * @return string
163
     * @throws InvalidArgumentException
164
     */
165
    protected static function addCTeOSProtocol($request, $response)
166
    {
167
        $req = new DOMDocument('1.0', 'UTF-8');
168
        $req->preserveWhiteSpace = false;
169
        $req->formatOutput = false;
170
        $req->loadXML($request);
171
172
        $cte = $req->getElementsByTagName('CTeOS')->item(0);
173
        $infCTe = $req->getElementsByTagName('infCte')->item(0);
174
        $versao = $infCTe->getAttribute("versao");
175
        $chave = preg_replace('/[^0-9]/', '', $infCTe->getAttribute("Id"));
0 ignored issues
show
Unused Code introduced by
$chave is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
        $digCTe = $req->getElementsByTagName('DigestValue')
177
            ->item(0)
178
            ->nodeValue;
179
180
        $ret = new DOMDocument('1.0', 'UTF-8');
181
        $ret->preserveWhiteSpace = false;
182
        $ret->formatOutput = false;
183
        $ret->loadXML($response);
184
        $retProt = $ret->getElementsByTagName('protCTe')->item(0);
185
        if (!isset($retProt)) {
186
            throw DocumentsException::wrongDocument(3, "&lt;protCTe&gt;");
187
        }
188
        $infProt = $ret->getElementsByTagName('infProt')->item(0);
189
        $cStat  = $infProt->getElementsByTagName('cStat')->item(0)->nodeValue;
190
        $xMotivo = $infProt->getElementsByTagName('xMotivo')->item(0)->nodeValue;
191
        $dig = $infProt->getElementsByTagName("digVal")->item(0);
192
        $digProt = '000';
193
        if (isset($dig)) {
194
            $digProt = $dig->nodeValue;
195
        }
196
        //100 Autorizado
197
        //150 Autorizado fora do prazo
198
        //110 Uso Denegado
199
        //205 CTe Denegada
200
        $cstatpermit = ['100', '150', '110', '205'];
201
        if (!in_array($cStat, $cstatpermit)) {
202
            throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
203
        }
204
        if ($digCTe !== $digProt) {
205
            throw DocumentsException::wrongDocument(5, "O digest é diferente");
206
        }
207
        return self::join(
208
            $req->saveXML($cte),
209
            $ret->saveXML($retProt),
210
            'cteOSProc',
211
            $versao
212
        );
213
    }
214
215
    /**
216
     * Authorize Event
217
     * @param string $request
218
     * @param string $response
219
     * @return string
220
     * @throws InvalidArgumentException
221
     */
222
    protected static function addEventoCTeProtocol($request, $response)
223
    {
224
        $ev = new \DOMDocument('1.0', 'UTF-8');
225
        $ev->preserveWhiteSpace = false;
226
        $ev->formatOutput = false;
227
        $ev->loadXML($request);
228
        //extrai numero do lote do envio
229
        //$envLote = $ev->getElementsByTagName('idLote')->item(0)->nodeValue;
230
        //extrai tag evento do xml origem (solicitação)
231
        $event = $ev->getElementsByTagName('eventoCTe')->item(0);
232
        $versao = $event->getAttribute('versao');
233
234
        $ret = new \DOMDocument('1.0', 'UTF-8');
235
        $ret->preserveWhiteSpace = false;
236
        $ret->formatOutput = false;
237
        $ret->loadXML($response);
238
        //extrai numero do lote da resposta
239
        //        $resLote = $ret->getElementsByTagName('idLote')->item(0)->nodeValue;
240
        //extrai a rag retEvento da resposta (retorno da SEFAZ)
241
        $retEv = $ret->getElementsByTagName('retEventoCTe')->item(0);
242
        $cStat  = $retEv->getElementsByTagName('cStat')->item(0)->nodeValue;
243
        $xMotivo = $retEv->getElementsByTagName('xMotivo')->item(0)->nodeValue;
244
        $tpEvento = $retEv->getElementsByTagName('tpEvento')->item(0)->nodeValue;
245
        $cStatValids = ['135', '136'];
246
        if ($tpEvento == '110111') {
247
            $cStatValids[] = '155';
248
        }
249
        if (!in_array($cStat, $cStatValids)) {
250
            throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
251
        }
252
253
        return self::join(
254
            $ev->saveXML($event),
255
            $ret->saveXML($retEv),
256
            'procEventoCTe',
257
            $versao
258
        );
259
    }
260
261
262
    /**
263
     * Add cancel protocol to a autorized CTe
264
     * if event is not a cancellation will return
265
     * the same autorized CTe passing
266
     * NOTE: This action is not necessary, I use only for my needs to
267
     * leave the CTe marked as Canceled in order to avoid mistakes
268
     * after its cancellation.
269
     * @param string $cte content of autorized CTe XML
270
     * @param string $cancelamento content of SEFAZ response
271
     * @return string
272
     * @throws \InvalidArgumentException
273
     */
274
    public static function cancelRegister($cte, $cancelamento)
275
    {
276
        $procXML = $cte;
277
        $domcte = new DOMDocument('1.0', 'utf-8');
278
        $domcte->formatOutput = false;
279
        $domcte->preserveWhiteSpace = false;
280
        $domcte->loadXML($cte);
281
        $proCTe = $domcte->getElementsByTagName('protCTe')->item(0);
282
        if (empty($proCTe)) {
283
            //not protocoladed CTe
284
            throw DocumentsException::wrongDocument(1);
285
        }
286
        $chaveCTe = $proCTe->getElementsByTagName('chCTe')->item(0)->nodeValue;
287
        $domcanc = new DOMDocument('1.0', 'utf-8');
288
        $domcanc->formatOutput = false;
289
        $domcanc->preserveWhiteSpace = false;
290
        $domcanc->loadXML($cancelamento);
291
        $eventos = $domcanc->getElementsByTagName('retEventoCTe');
292
        foreach ($eventos as $evento) {
293
            $infEvento = $evento->getElementsByTagName('infEvento')->item(0);
294
            $cStat = $infEvento->getElementsByTagName('cStat')
295
                ->item(0)
296
                ->nodeValue;
297
            $nProt = $infEvento->getElementsByTagName('nProt')
298
                ->item(0)
299
                ->nodeValue;
300
            $chaveEvento = $infEvento->getElementsByTagName('chCTe')
301
                ->item(0)
302
                ->nodeValue;
303
            $tpEvento = $infEvento->getElementsByTagName('tpEvento')
304
                ->item(0)
305
                ->nodeValue;
306
            if (
307
                in_array($cStat, ['135', '136', '155'])
308
                && $tpEvento == '110111'
309
                && $chaveEvento == $chaveCTe
310
            ) {
311
                $proCTe->getElementsByTagName('cStat')
312
                    ->item(0)
313
                    ->nodeValue = '101';
314
                $proCTe->getElementsByTagName('nProt')
315
                    ->item(0)
316
                    ->nodeValue = $nProt;
317
                $proCTe->getElementsByTagName('xMotivo')
318
                    ->item(0)
319
                    ->nodeValue = 'Cancelamento de CT-e homologado';
320
                $procXML = Strings::clearProtocoledXML($domcte->saveXML());
321
                break;
322
            }
323
        }
324
        return $procXML;
325
    }
326
327
    /**
328
     * Join the pieces of the source document with those of the answer
329
     * @param string $first
330
     * @param string $second
331
     * @param string $nodename
332
     * @param string $versao
333
     * @return string
334
     */
335
    protected static function join($first, $second, $nodename, $versao)
336
    {
337
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
338
            . "<$nodename versao=\"$versao\" "
339
            . "xmlns=\"" . self::$urlPortal . "\">";
340
        $xml .= $first;
341
        $xml .= $second;
342
        $xml .= "</$nodename>";
343
        return $xml;
344
    }
345
}
346