Passed
Push — master ( 84520a...e53a66 )
by Francimar
03:44
created

Autorizacao::validar()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.4042

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 10
cts 18
cp 0.5556
rs 8.6845
c 1
b 0
f 0
cc 4
eloc 18
nc 4
nop 1
crap 5.4042
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
use NFe\Exception\ValidationException;
34
35
class Autorizacao extends Retorno
36
{
37
38 4
    public function __construct($autorizacao = array())
39
    {
40 4
        parent::__construct($autorizacao);
41 4
    }
42
43 2
    public function toArray($recursive = false)
44
    {
45 2
        $autorizacao = parent::toArray($recursive);
46 2
        return $autorizacao;
47
    }
48
49 4
    public function fromArray($autorizacao = array())
50
    {
51 4
        if ($autorizacao instanceof Autorizacao) {
52 1
            $autorizacao = $autorizacao->toArray();
53 4
        } elseif (!is_array($autorizacao)) {
54 1
            return $this;
55
        }
56 4
        parent::fromArray($autorizacao);
57 4
        return $this;
58
    }
59
60 3
    private function getConteudo($dom)
61
    {
62 3
        $config = SEFAZ::getInstance()->getConfiguracao();
63 3
        $dob = new \DOMDocument('1.0', 'UTF-8');
64 3
        $envio = $dob->createElement('enviNFe');
65 3
        $envio->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
66 3
        $versao = $dob->createAttribute('versao');
67 3
        $versao->value = Nota::VERSAO;
68 3
        $envio->appendChild($versao);
69 3
        Util::appendNode($envio, 'idLote', self::genLote());
70 3
        Util::appendNode($envio, 'indSinc', $config->getSincrono(true));
71
        // Corrige xmlns:default
72
        // $data = $dob->importNode($dom->documentElement, true);
73
        // $envio->appendChild($data);
74 3
        Util::appendNode($envio, 'NFe', 0);
75 3
        $dob->appendChild($envio);
76
        // Corrige xmlns:default
77
        // return $dob;
78 3
        $xml = $dob->saveXML($dob->documentElement);
79 3
        return str_replace('<NFe>0</NFe>', $dom->saveXML($dom->documentElement), $xml);
80
    }
81
82 3
    public function envia($nota, $dom)
83
    {
84 3
        $xmlContent = $this->getConteudo($dom);
0 ignored issues
show
Coding Style introduced by
$xmlContent does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
85 3
        $domLote = $this->validar($xmlContent);
0 ignored issues
show
Coding Style introduced by
$domLote does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
86 3
        $envio = new Envio();
87 3
        $envio->setServico(Envio::SERVICO_AUTORIZACAO);
88 3
        $envio->setAmbiente($nota->getAmbiente());
89 3
        $envio->setModelo($nota->getModelo());
90 3
        $envio->setEmissao($nota->getEmissao());
91 3
        $envio->setConteudo($domLote);
0 ignored issues
show
Coding Style introduced by
$domLote does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
92 3
        $resp = $envio->envia();
93 3
        $this->loadNode($resp);
94 3
        if ($this->isProcessado()) {
95 1
            $protocolo = new Protocolo();
96 1
            $protocolo->loadNode($resp);
97 1
            if ($protocolo->isAutorizado()) {
98 1
                $nota->setProtocolo($protocolo);
99
            }
100 1
            return $protocolo;
101 2
        } elseif ($this->isRecebido()) {
102 1
            $recibo = new Recibo($this->toArray());
103 1
            $recibo->setModelo($nota->getModelo());
104 1
            $recibo->loadNode($resp, Recibo::INFO_TAGNAME);
105 1
            return $recibo;
106 1
        } elseif ($this->isParalisado()) {
107
            $config = SEFAZ::getInstance()->getConfiguracao();
108
            $config->setOffline(time());
109
            throw new \NFe\Exception\NetworkException('Serviço paralisado ou em manutenção', $this->getStatus());
110
        }
111 1
        return $this;
112
    }
113
114 3
    public function loadNode($element, $name = null)
115
    {
116 3
        $tag = is_null($name)?'retEnviNFe':$name;
117 3
        $element = parent::loadNode($element, $tag);
118 3
        return $element;
119
    }
120
121
    /**
122
     * Valida o XML em lote
123
     */
124 3
    public function validar($xmlContent)
0 ignored issues
show
Coding Style introduced by
$xmlContent does not seem to conform to the naming convention (^[a-z][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
125
    {
126 3
        $dom = new \DOMDocument('1.0', 'UTF-8');
127 3
        $dom->loadXML($xmlContent);
0 ignored issues
show
Coding Style introduced by
$xmlContent does not seem to conform to the naming convention (^[a-z_][a-z0-9_]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
128 3
        $xsd_path = dirname(__DIR__) . '/Core/schema';
129 3
        $xsd_file = $xsd_path . '/enviNFe_v3.10.xsd';
130 3
        if (!file_exists($xsd_file)) {
131
            throw new \Exception('O arquivo "'.$xsd_file.'" de esquema XSD não existe!', 404);
132
        }
133
        // Enable user error handling
134 3
        $save = libxml_use_internal_errors(true);
135 3
        if ($dom->schemaValidate($xsd_file)) {
136 3
            libxml_use_internal_errors($save);
137 3
            return $dom;
138
        }
139
        $msg = array();
140
        $errors = libxml_get_errors();
141
        foreach ($errors as $error) {
142
            $msg[] = 'Não foi possível validar o XML: '.$error->message;
143
        }
144
        libxml_clear_errors();
145
        libxml_use_internal_errors($save);
146
        throw new ValidationException($msg);
147
    }
148
}
149