Passed
Push — master ( 18e2e8...ca323f )
by Roberto
02:38 queued 01:35
created

src/Factories/QRCode.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace NFePHP\NFe\Factories;
4
5
/**
6
 * Class QRCode create a string to make a QRCode string to NFCe
7
 * NOTE: this class only works with model 65 NFCe only
8
 *
9
 * @category  NFePHP
10
 * @package   NFePHP\NFe\Factories\QRCode
11
 * @copyright NFePHP Copyright (c) 2008-2019
12
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
13
 * @license   https://opensource.org/licenses/MIT MIT
14
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
15
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
16
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
17
 */
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
$layoutver 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...
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,
0 ignored issues
show
The parameter $vICMS is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
        $digVal,
141
        $token,
142
        $idToken,
143
        $versao,
144
        $tpEmis,
145
        $cDest
0 ignored issues
show
The parameter $cDest is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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($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