Completed
Pull Request — master (#83)
by
unknown
11:11
created

Complements   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 137
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toAuthorize() 0 11 3
B addMDFeProtocol() 0 44 5
A addEventoMDFeProtocol() 0 36 4
A join() 0 10 1
1
<?php
2
3
namespace NFePHP\MDFe;
4
5
use DOMDocument;
6
use NFePHP\MDFe\Common\Standardize;
7
use NFePHP\MDFe\Exception\DocumentsException;
8
9
class Complements
10
{
11
    protected static $urlPortal = 'http://www.portalfiscal.inf.br/mdfe';
12
13
    /**
14
     * Authorize document adding his protocol
15
     * @param string $request
16
     * @param string $response
17
     * @return string
18
     */
19
    public static function toAuthorize($request, $response)
20
    {
21
        $st = new Standardize();
22
        $key = ucfirst($st->whichIs($request));
23
        if ($key != 'MDFe' && $key != 'EventoMDFe') {
24
            //wrong document, this document is not able to recieve a protocol
25
            throw DocumentsException::wrongDocument(0, $key);
26
        }
27
        $func = "add" . $key . "Protocol";
28
        return self::$func($request, $response);
29
    }
30
31
    /**
32
     * Authorize MDFe
33
     * @param string $request
34
     * @param string $response
35
     * @return string
36
     * @throws InvalidArgumentException
37
     */
38
    protected static function addMDFeProtocol($request, $response)
39
    {
40
        $req = new DOMDocument('1.0', 'UTF-8');
41
        $req->preserveWhiteSpace = false;
42
        $req->formatOutput = false;
43
        $req->loadXML($request);
44
45
        $mdfe = $req->getElementsByTagName('MDFe')->item(0);
46
        $infMDFe = $req->getElementsByTagName('infMDFe')->item(0);
47
        $versao = $infMDFe->getAttribute("versao");
48
        $digMDFe = $req->getElementsByTagName('DigestValue')
49
            ->item(0)
50
            ->nodeValue;
51
52
        $ret = new DOMDocument('1.0', 'UTF-8');
53
        $ret->preserveWhiteSpace = false;
54
        $ret->formatOutput = false;
55
        $ret->loadXML($response);
56
        $retProt = $ret->getElementsByTagName('protMDFe')->item(0);
57
        if (!isset($retProt)) {
58
            throw DocumentsException::wrongDocument(3, "&lt;protMDFe&gt;");
59
        }
60
        $infProt = $ret->getElementsByTagName('infProt')->item(0);
61
        $cStat = $infProt->getElementsByTagName('cStat')->item(0)->nodeValue;
62
        $xMotivo = $infProt->getElementsByTagName('xMotivo')->item(0)->nodeValue;
63
        $dig = $infProt->getElementsByTagName("digVal")->item(0);
64
        $digProt = '000';
65
        if (isset($dig)) {
66
            $digProt = $dig->nodeValue;
67
        }
68
        //100 Autorizado
69
        if ($cStat != '100') {
70
            throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
71
        }
72
        if ($digMDFe !== $digProt) {
73
            throw DocumentsException::wrongDocument(5, "O digest é diferente");
74
        }
75
        return self::join(
76
            $req->saveXML($mdfe),
77
            $ret->saveXML($retProt),
78
            'mdfeProc',
79
            $versao
80
        );
81
    }
82
83
    /**
84
     * Authorize Event
85
     * @param string $request
86
     * @param string $response
87
     * @return string
88
     * @throws InvalidArgumentException
89
     */
90
    protected static function addEventoMDFeProtocol($request, $response)
91
    {
92
        $ev = new \DOMDocument('1.0', 'UTF-8');
93
        $ev->preserveWhiteSpace = false;
94
        $ev->formatOutput = false;
95
        $ev->loadXML($request);
96
        //extrai tag evento do xml origem (solicitação)
97
        $event = $ev->getElementsByTagName('eventoMDFe')->item(0);
98
        $versao = $event->getAttribute('versao');
99
100
        $ret = new \DOMDocument('1.0', 'UTF-8');
101
        $ret->preserveWhiteSpace = false;
102
        $ret->formatOutput = false;
103
        $ret->loadXML($response);
104
        //extrai a rag retEvento da resposta (retorno da SEFAZ)
105
        $retEv = $ret->getElementsByTagName('retEventoMDFe')->item(0);
106
        $cStat = $retEv->getElementsByTagName('cStat')->item(0)->nodeValue;
107
        $xMotivo = $retEv->getElementsByTagName('xMotivo')->item(0)->nodeValue;
108
        $tpEvento = $retEv->getElementsByTagName('tpEvento')->item(0)->nodeValue;
109
        if ($tpEvento == '110111') {
110
            $node = 'procCancMDFe';
111
        } elseif ($tpEvento == '110112') {
112
            $node = 'procEncMDFe';
113
        } else {
114
            throw DocumentsException::wrongDocument(4, "Evento não disponivel.");
115
        }
116
        if ($cStat != '135') {
117
            throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
118
        }
119
        return self::join(
120
            $ev->saveXML($event),
121
            $ret->saveXML($retEv),
122
            $node,
123
            $versao
124
        );
125
    }
126
127
    /**
128
     * Join the pieces of the source document with those of the answer
129
     * @param string $first
130
     * @param string $second
131
     * @param string $nodename
132
     * @param string $versao
133
     * @return string
134
     */
135
    protected static function join($first, $second, $nodename, $versao)
136
    {
137
        $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
138
            . "<$nodename versao=\"$versao\" "
139
            . "xmlns=\"" . self::$urlPortal . "\">";
140
        $xml .= $first;
141
        $xml .= $second;
142
        $xml .= "</$nodename>";
143
        return $xml;
144
    }
145
}
146