QRCode   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 0
dl 0
loc 32
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A putQRTag() 0 23 2
1
<?php
2
3
namespace NFePHP\MDFe\Factories;
4
5
/**
6
 * Class QRCode create a string to make a QRCode string
7
 *
8
 * @author    Cleiton Perin <cperin20 at gmail dot com>
9
 * @package   NFePHP\MDFe\Factories\QRCode
10
 * @copyright NFePHP Copyright (c) 2008-2019
11
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
12
 * @license   https://opensource.org/licenses/MIT MIT
13
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
14
 * @category  NFePHP
15
 * @link      http://github.com/nfephp-org/sped-mdfe for the canonical source repository
16
 */
17
18
use DOMDocument;
19
use NFePHP\MDFe\Exception\DocumentsException;
20
21
class QRCode
22
{
23
    /**
24
     * putQRTag
25
     * @param DOMDocument $dom MDFe
26
     * @return string
27
     * @throws DocumentsException
28
     */
29
    public static function putQRTag(
30
        \DOMDocument $dom,
31
        $certificate
32
    ) {
33
        $mdfe = $dom->getElementsByTagName('MDFe')->item(0);
34
        $infMDFe = $dom->getElementsByTagName('infMDFe')->item(0);
35
        $ide = $dom->getElementsByTagName('ide')->item(0);
36
        $tpEmis = $ide->getElementsByTagName('tpEmis')->item(0)->nodeValue;
37
        $chMDFe = preg_replace('/[^0-9]/', '', $infMDFe->getAttribute("Id"));
38
        $sign = '';
39
        if ($tpEmis == 2) {
40
            $sign = "&sign=" . base64_encode($certificate->sign($chMDFe));
41
        }
42
        $tpAmb = $ide->getElementsByTagName('tpAmb')->item(0)->nodeValue;
43
        $urlQRCode = "https://dfe-portal.svrs.rs.gov.br/mdfe/qrCode?chMDFe=$chMDFe&tpAmb=$tpAmb{$sign}";
44
        $infMDFeSupl = $dom->createElement("infMDFeSupl");
45
        $qrCode = $infMDFeSupl->appendChild($dom->createElement('qrCodMDFe'));
46
        $qrCode->appendChild($dom->createCDATASection($urlQRCode));
47
        $signature = $dom->getElementsByTagName('Signature')->item(0);
48
        $mdfe->insertBefore($infMDFeSupl, $signature);
49
        $dom->formatOutput = false;
50
        return $dom->saveXML();
51
    }
52
}
53