Passed
Pull Request — master (#921)
by
unknown
08:01
created

ContingencyNFe::adjust()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 67
Code Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 55
dl 0
loc 67
ccs 0
cts 62
cp 0
rs 8.6707
c 1
b 0
f 0
cc 5
nc 9
nop 2
crap 30

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\NFe\Factories;
4
5
use NFePHP\Common\Strings;
6
use NFePHP\Common\Signer;
7
use NFePHP\Common\Keys;
8
use NFePHP\Common\TimeZoneByUF;
9
use NFePHP\Common\UFList;
10
use DateTime;
11
12
class ContingencyNFe
13
{
14
    /**
15
     * Corrects NFe fields when in contingency mode
16
     * @param string $xml NFe xml content
17
     * @param Contingency $contingency
18
     * @return string
19
     */
20
    public static function adjust($xml, Contingency $contingency)
21
    {
22
        if ($contingency->type == '') {
23
            return $xml;
24
        }
25
        $xml = Signer::removeSignature($xml);
26
27
        $dom = new \DOMDocument('1.0', 'UTF-8');
28
        $dom->preserveWhiteSpace = false;
29
        $dom->formatOutput = false;
30
        $dom->loadXML($xml);
31
32
        $ide = $dom->getElementsByTagName('ide')->item(0);
33
        $cUF = $ide->getElementsByTagName('cUF')->item(0)->nodeValue;
34
        $cNF = $ide->getElementsByTagName('cNF')->item(0)->nodeValue;
35
        $nNF = $ide->getElementsByTagName('nNF')->item(0)->nodeValue;
36
        $serie = $ide->getElementsByTagName('serie')->item(0)->nodeValue;
37
        $mod = $ide->getElementsByTagName('mod')->item(0)->nodeValue;
38
        $dtEmi = new DateTime($ide->getElementsByTagName('dhEmi')->item(0)->nodeValue);
39
        $ano = $dtEmi->format('y');
40
        $mes = $dtEmi->format('m');
41
        $tpEmis = (string) $contingency->tpEmis;
42
        $emit = $dom->getElementsByTagName('emit')->item(0);
43
        if (!empty($emit->getElementsByTagName('CNPJ')->item(0)->nodeValue)) {
44
            $doc = $emit->getElementsByTagName('CNPJ')->item(0)->nodeValue;
45
        } else {
46
            $doc = $emit->getElementsByTagName('CPF')->item(0)->nodeValue;
47
        }
48
        $motivo = trim(Strings::replaceUnacceptableCharacters($contingency->motive));
0 ignored issues
show
Bug introduced by
It seems like NFePHP\Common\Strings::r...s($contingency->motive) can also be of type null; however, parameter $string of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
        $motivo = trim(/** @scrutinizer ignore-type */ Strings::replaceUnacceptableCharacters($contingency->motive));
Loading history...
49
50
        $tz = TimeZoneByUF::get(UFList::getUFByCode($cUF));
51
        $dt = new \DateTime(date("Y-m-d H:i:sP"), new \DateTimeZone($tz));
52
53
        $dt->setTimestamp($contingency->timestamp);
54
        $ide->getElementsByTagName('tpEmis')
55
            ->item(0)
56
            ->nodeValue = $contingency->tpEmis;
57
        if (!empty($ide->getElementsByTagName('dhCont')->item(0)->nodeValue)) {
58
            $ide->getElementsByTagName('dhCont')
59
                ->item(0)
60
                ->nodeValue = $dt->format('Y-m-d\TH:i:sP');
61
        } else {
62
            $dhCont = $dom->createElement('dhCont', $dt->format('Y-m-d\TH:i:sP'));
63
            $ide->appendChild($dhCont);
64
        }
65
        if (!empty($ide->getElementsByTagName('xJust')->item(0)->nodeValue)) {
66
            $ide->getElementsByTagName('xJust')->item(0)->nodeValue = $motivo;
67
        } else {
68
            $xJust = $dom->createElement('xJust', $motivo);
69
            $ide->appendChild($xJust);
70
        }
71
        //corrigir a chave
72
        $infNFe = $dom->getElementsByTagName('infNFe')->item(0);
73
        $chave = Keys::build(
74
            $cUF,
75
            $ano,
76
            $mes,
77
            $doc,
78
            $mod,
79
            $serie,
80
            $nNF,
81
            $tpEmis,
82
            $cNF
83
        );
84
        $ide->getElementsByTagName('cDV')->item(0)->nodeValue = substr($chave, -1);
85
        $infNFe->setAttribute('Id', 'NFe' . $chave);
86
        return Strings::clearXmlString($dom->saveXML(), true);
87
    }
88
}
89