Passed
Push — master ( c1d1cf...f48ccd )
by Francimar
03:58
created

Autorizacao::loadNode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * MIT License
4
 *
5
 * Copyright (c) 2016 MZ Desenvolvimento de Sistemas LTDA
6
 *
7
 * @author Francimar Alves <[email protected]>
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
namespace NFe\Task;
29
30
use NFe\Core\Nota;
31
use NFe\Core\SEFAZ;
32
use NFe\Common\Util;
33
34
class Autorizacao extends Retorno
35
{
36
37 4
    public function __construct($autorizacao = array())
38
    {
39 4
        parent::__construct($autorizacao);
40 4
    }
41
42 2
    public function toArray($recursive = false)
43
    {
44 2
        $autorizacao = parent::toArray($recursive);
45 2
        return $autorizacao;
46
    }
47
48 4
    public function fromArray($autorizacao = array())
49
    {
50 4
        if ($autorizacao instanceof Autorizacao) {
51 1
            $autorizacao = $autorizacao->toArray();
52 4
        } elseif (!is_array($autorizacao)) {
53 1
            return $this;
54
        }
55 4
        parent::fromArray($autorizacao);
56 4
        return $this;
57
    }
58
59 3
    private function getConteudo($dom)
60
    {
61 3
        $config = SEFAZ::getInstance()->getConfiguracao();
62 3
        $dob = new \DOMDocument('1.0', 'UTF-8');
63 3
        $envio = $dob->createElement('enviNFe');
64 3
        $envio->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
65 3
        $versao = $dob->createAttribute('versao');
66 3
        $versao->value = Nota::VERSAO;
67 3
        $envio->appendChild($versao);
68 3
        Util::appendNode($envio, 'idLote', self::genLote());
69 3
        Util::appendNode($envio, 'indSinc', $config->getSincrono(true));
70
        // Corrige xmlns:default
71
        // $data = $dob->importNode($dom->documentElement, true);
72
        // $envio->appendChild($data);
73 3
        Util::appendNode($envio, 'NFe', 0);
74 3
        $dob->appendChild($envio);
75
        // Corrige xmlns:default
76
        // return $dob;
77 3
        $xml = $dob->saveXML($dob->documentElement);
78 3
        return str_replace('<NFe>0</NFe>', $dom->saveXML($dom->documentElement), $xml);
79
    }
80
81 3
    public function envia($nota, $dom)
82
    {
83 3
        $envio = new Envio();
84 3
        $envio->setServico(Envio::SERVICO_AUTORIZACAO);
85 3
        $envio->setAmbiente($nota->getAmbiente());
86 3
        $envio->setModelo($nota->getModelo());
87 3
        $envio->setEmissao($nota->getEmissao());
88 3
        $envio->setConteudo($this->getConteudo($dom));
89 3
        $resp = $envio->envia();
90 3
        $this->loadNode($resp);
91 3
        if ($this->isProcessado()) {
92 1
            $protocolo = new Protocolo();
93 1
            $protocolo->loadNode($resp);
94 1
            if ($protocolo->isAutorizado()) {
95 1
                $nota->setProtocolo($protocolo);
96 1
            }
97 1
            return $protocolo;
98 2
        } elseif ($this->isRecebido()) {
99 1
            $recibo = new Recibo($this->toArray());
100 1
            $recibo->setModelo($nota->getModelo());
101 1
            $recibo->loadNode($resp, Recibo::INFO_TAGNAME);
102 1
            return $recibo;
103
        }
104 1
        return $this;
105
    }
106
107 3
    public function loadNode($element, $name = null)
108
    {
109 3
        $tag = is_null($name)?'retEnviNFe':$name;
110 3
        $element = parent::loadNode($element, $tag);
111 3
        return $element;
112
    }
113
}
114