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

QRCode::get200()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0494

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 34
ccs 14
cts 17
cp 0.8235
rs 9.7333
c 0
b 0
f 0
cc 3
nc 4
nop 12
crap 3.0494

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Class QRCode create a string to make a QRCode string to NFCe
5
 * NOTE: this class only works with model 65 NFCe only
6
 *
7
 * @category  NFePHP
8
 * @package   NFePHP\NFe\Factories\QRCode
9
 * @copyright NFePHP Copyright (c) 2008-2019
10
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
11
 * @license   https://opensource.org/licenses/MIT MIT
12
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
13
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
14
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
15
 */
16
17
namespace NFePHP\NFe\Factories;
18
19
use DOMDocument;
20
use NFePHP\NFe\Exception\DocumentsException;
21
22
class QRCode
23
{
24
    /**
25
     * putQRTag
26
     * Mount URI for QRCode and create three XML tags in signed xml
27
     * NOTE: included Manual_de_Especificações_Técnicas_do_DANFE_NFC-e_QR_Code
28
     *       versão 5.0 since fevereiro de 2018
29
     * @param DOMDocument $dom NFe
30
     * @param string $token CSC number
31
     * @param string $idToken CSC identification
32
     * @param string $versao version of field
33
     * @param string $urlqr URL for search by QRCode
34
     * @param string $urichave URL for search by chave layout 4.00 only
35
     * @return string
36
     * @throws DocumentsException
37
     */
38 3
    public static function putQRTag(
39
        \DOMDocument $dom,
40
        $token,
41
        $idToken,
42
        $versao,
43
        $urlqr,
44
        $urichave = ''
45
    ) {
46 3
        $token = trim($token);
47 3
        $idToken = trim($idToken);
48 3
        $versao = trim($versao);
49 3
        $urlqr = trim($urlqr);
50 3
        $urichave = trim($urichave);
51 3
        if (empty($token)) {
52 1
            throw DocumentsException::wrongDocument(9); //Falta o CSC no config.json
53
        }
54 2
        if (empty($idToken)) {
55 1
            throw DocumentsException::wrongDocument(10); //Falta o CSCId no config.json
56
        }
57 1
        if (empty($urlqr)) {
58 1
            throw DocumentsException::wrongDocument(11); //Falta a URL do serviço NfeConsultaQR
59
        }
60
        if (empty($versao)) {
61
            $versao = '200';
62
        }
63
        $nfe = $dom->getElementsByTagName('NFe')->item(0);
64
        $infNFe = $dom->getElementsByTagName('infNFe')->item(0);
65
        $layoutver = $infNFe->getAttribute('versao');
0 ignored issues
show
Unused Code introduced by
The assignment to $layoutver is dead and can be removed.
Loading history...
66
        $ide = $dom->getElementsByTagName('ide')->item(0);
67
        $dest = $dom->getElementsByTagName('dest')->item(0);
68
        $icmsTot = $dom->getElementsByTagName('ICMSTot')->item(0);
69
        $signedInfo = $dom->getElementsByTagName('SignedInfo')->item(0);
70
        $chNFe = preg_replace('/[^0-9]/', '', $infNFe->getAttribute("Id"));
71
        $tpAmb = $ide->getElementsByTagName('tpAmb')->item(0)->nodeValue;
72
        $dhEmi = $ide->getElementsByTagName('dhEmi')->item(0)->nodeValue;
73
        $tpEmis = $ide->getElementsByTagName('tpEmis')->item(0)->nodeValue;
74
        $cDest = '';
75
        if (!empty($dest)) {
76
            $cDest = !empty($dest->getElementsByTagName('CNPJ')->item(0)->nodeValue)
77
                ? $dest->getElementsByTagName('CNPJ')->item(0)->nodeValue
78
                : '';
79
            if (empty($cDest)) {
80
                $cDest = !empty($dest->getElementsByTagName('CPF')->item(0)->nodeValue)
81
                    ? $dest->getElementsByTagName('CPF')->item(0)->nodeValue
82
                    : '';
83
                if (empty($cDest)) {
84
                    $cDest = $dest->getElementsByTagName('idEstrangeiro')->item(0)->nodeValue;
85
                }
86
            }
87
        }
88
        $vNF = $icmsTot->getElementsByTagName('vNF')->item(0)->nodeValue;
89
        $vICMS = $icmsTot->getElementsByTagName('vICMS')->item(0)->nodeValue;
90
        $digVal = $signedInfo->getElementsByTagName('DigestValue')->item(0)->nodeValue;
91
        $qrMethod = "get$versao";
92
        $qrcode = self::$qrMethod(
93
            $chNFe,
94
            $urlqr,
95
            $tpAmb,
96
            $dhEmi,
97
            $vNF,
98
            $vICMS,
99
            $digVal,
100
            $token,
101
            $idToken,
102
            $versao,
103
            $tpEmis,
104
            $cDest
105
        );
106
        $infNFeSupl = $dom->createElement("infNFeSupl");
107
        $infNFeSupl->appendChild($dom->createElement('qrCode', $qrcode));
108
        //$nodeqr = $infNFeSupl->appendChild($dom->createElement('qrCode'));
109
        //$nodeqr->appendChild($dom->createCDATASection($qrcode));
110
        $infNFeSupl->appendChild($dom->createElement('urlChave', $urichave));
111
        $signature = $dom->getElementsByTagName('Signature')->item(0);
112
        $nfe->insertBefore($infNFeSupl, $signature);
113
        $dom->formatOutput = false;
114
        return $dom->saveXML();
115
    }
116
117
    /**
118
     * Return a QRCode version 2 string to be used in NFCe layout 4.00
119
     * @param  string $chNFe
120
     * @param  string $url
121
     * @param  string $tpAmb
122
     * @param  string $dhEmi
123
     * @param  string $vNF
124
     * @param  string $vICMS
125
     * @param  string $digVal
126
     * @param  string $token
127
     * @param  string $idToken
128
     * @param  string $versao
129
     * @param  int    $tpEmis
130
     * @param  string $cDest
131
     * @return string
132
     */
133 1
    protected static function get200(
134
        $chNFe,
135
        $url,
136
        $tpAmb,
137
        $dhEmi,
138
        $vNF,
139
        $vICMS,
140
        $digVal,
141
        $token,
142
        $idToken,
143
        $versao,
144
        $tpEmis,
145
        $cDest
146
    ) {
147 1
        $ver = $versao / 100;
148 1
        $cscId = (int) $idToken;
149 1
        $csc = $token;
150 1
        if (strpos($url, '?p=') === false) {
151 1
            $url = $url . '?p=';
152
        }
153 1
        if ($tpEmis != 9) {
154
            //emissão on-line
155
            $seq = "$chNFe|$ver|$tpAmb|$cscId";
156
            $hash = strtoupper(sha1($seq . $csc));
157
            return "$url$seq|$hash";
158
        }
159
        //emissão off-line
160 1
        $dt = new \DateTime($dhEmi);
161 1
        $dia = $dt->format('d');
162 1
        $valor = number_format((float)$vNF, 2, '.', '');
163 1
        $digHex = self::str2Hex($digVal);
164 1
        $seq = "$chNFe|$ver|$tpAmb|$dia|$valor|$digHex|$cscId";
165 1
        $hash = strtoupper(sha1($seq . $csc));
166 1
        return "$url$seq|$hash";
167
    }
168
169
    /**
170
     * Convert string to hexadecimal ASCII equivalent
171
     * @param  string $str
172
     * @return string
173
     */
174 1
    protected static function str2Hex($str)
175
    {
176 1
        $hex = "";
177 1
        $iCount = 0;
178 1
        $tot = strlen($str);
179
        do {
180 1
            $hex .= sprintf("%02x", ord($str[$iCount]));
181 1
            $iCount++;
182 1
        } while ($iCount < $tot);
183 1
        return $hex;
184
    }
185
}
186