Complements   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 221
ccs 0
cts 162
cp 0
rs 10
c 0
b 0
f 0

6 Methods

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

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
158
     * @param string $cancelamento content of SEFAZ response
159
     * @return string
160
     * @throws \InvalidArgumentException
161
     */
162
    public static function cancelRegister($mdfe, $cancelamento)
163
    {
164
        $procXML = $mdfe;
165
        $dommdfe = new DOMDocument('1.0', 'utf-8');
166
        $dommdfe->formatOutput = false;
167
        $dommdfe->preserveWhiteSpace = false;
168
        $dommdfe->loadXML($mdfe);
169
        $proMDFe = $dommdfe->getElementsByTagName('protMDFe')->item(0);
170
        if (empty($proMDFe)) {
171
            //not protocoladed MDFe
172
            throw DocumentsException::wrongDocument(1);
173
        }
174
        $chaveMdfe = $proMDFe->getElementsByTagName('chMDFe')->item(0)->nodeValue;
175
        $domcanc = new DOMDocument('1.0', 'utf-8');
176
        $domcanc->formatOutput = false;
177
        $domcanc->preserveWhiteSpace = false;
178
        $domcanc->loadXML($cancelamento);
179
        $eventos = $domcanc->getElementsByTagName('retEventoMDFe');
180
        foreach ($eventos as $evento) {
181
            $infEvento = $evento->getElementsByTagName('infEvento')->item(0);
182
            $cStat = $infEvento->getElementsByTagName('cStat')
183
                ->item(0)
184
                ->nodeValue;
185
            $nProt = $infEvento->getElementsByTagName('nProt')
186
                ->item(0)
187
                ->nodeValue;
188
            $chaveEvento = $infEvento->getElementsByTagName('chMDFe')
189
                ->item(0)
190
                ->nodeValue;
191
            $tpEvento = $infEvento->getElementsByTagName('tpEvento')
192
                ->item(0)
193
                ->nodeValue;
194
            $proMDFe->getElementsByTagName('nProt')
195
                ->item(0)
196
                ->nodeValue = $nProt;
197
            if (in_array($cStat, ['135', '136', '155'])
198
                && $tpEvento == '110111'
199
                && $chaveEvento == $chaveMdfe
200
            ) {
201
                $proMDFe->getElementsByTagName('cStat')
202
                    ->item(0)
203
                    ->nodeValue = '101';
204
                $proMDFe->getElementsByTagName('xMotivo')
205
                    ->item(0)
206
                    ->nodeValue = 'Cancelamento de MDF-e homologado';
207
                $procXML = Strings::clearProtocoledXML($dommdfe->saveXML());
208
                break;
209
            } elseif (in_array($cStat, ['135', '136', '155'])
210
                && $tpEvento == '110112'
211
                && $chaveEvento == $chaveMdfe
212
            ) {
213
                $proMDFe->getElementsByTagName('cStat')
214
                    ->item(0)
215
                    ->nodeValue = '103';
216
                $proMDFe->getElementsByTagName('xMotivo')
217
                    ->item(0)
218
                    ->nodeValue = 'Encerramento de MDF-e homologado';
219
                $procXML = Strings::clearProtocoledXML($dommdfe->saveXML());
220
                break;
221
            }
222
        }
223
        return $procXML;
224
    }
225
226
    public static function closeRegister($mdfe, $encerramento)
227
    {
228
        return self::cancelRegister($mdfe, $encerramento);
229
    }
230
}
231