Completed
Push — master ( c523cc...f6da78 )
by Francimar
05:08
created

Protocolo   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 25.56%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 1
dl 0
loc 153
ccs 23
cts 90
cp 0.2556
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setChave() 0 5 1
A setValidacao() 0 5 1
A setNumero() 0 5 1
A getChave() 0 7 2
A getValidacao() 0 7 2
A getNumero() 0 7 2
A toArray() 0 8 1
B fromArray() 0 25 6
A loadNode() 0 19 4
B getNode() 0 29 3
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
32
/**
33
 * Protocolo de autorização da nota, é retornado pela autorização, recibo
34
 * ou situação e anexado à nota
35
 */
36
class Protocolo extends Retorno
37
{
38
39
    private $chave;
40
    private $validacao;
41
    private $numero;
42
43 1
    public function __construct($protocolo = array())
44
    {
45 1
        parent::__construct($protocolo);
46 1
    }
47
48
    /**
49
     * Chaves de acesso da NF-e, compostas por: UF do emitente, AAMM da emissão
50
     * da NFe, CNPJ do emitente, modelo, série e número da NF-e e código
51
     * numérico+DV.
52
     */
53
    public function getChave($normalize = false)
54
    {
55
        if (!$normalize) {
56
            return $this->chave;
57
        }
58
        return $this->chave;
59
    }
60
61 1
    public function setChave($chave)
62
    {
63 1
        $this->chave = $chave;
64 1
        return $this;
65
    }
66
67
    /**
68
     * Digest Value da NF-e processada. Utilizado para conferir a integridade
69
     * da NF-e original.
70
     */
71
    public function getValidacao($normalize = false)
72
    {
73
        if (!$normalize) {
74
            return $this->validacao;
75
        }
76
        return $this->validacao;
77
    }
78
79 1
    public function setValidacao($validacao)
80
    {
81 1
        $this->validacao = $validacao;
82 1
        return $this;
83
    }
84
85
    /**
86
     * Número do Protocolo de Status da NF-e. 1 posição (1 – Secretaria de
87
     * Fazenda Estadual 2 – Receita Federal); 2 - códiga da UF - 2 posições
88
     * ano; 10 seqüencial no ano.
89
     */
90
    public function getNumero($normalize = false)
91
    {
92
        if (!$normalize) {
93
            return $this->numero;
94
        }
95
        return $this->numero;
96
    }
97
98 1
    public function setNumero($numero)
99
    {
100 1
        $this->numero = $numero;
101 1
        return $this;
102
    }
103
104
    public function toArray()
105
    {
106
        $protocolo = parent::toArray();
107
        $protocolo['chave'] = $this->getChave();
108
        $protocolo['validacao'] = $this->getValidacao();
109
        $protocolo['numero'] = $this->getNumero();
110
        return $protocolo;
111
    }
112
113 1
    public function fromArray($protocolo = array())
114
    {
115 1
        if ($protocolo instanceof Protocolo) {
116
            $protocolo = $protocolo->toArray();
117 1
        } elseif (!is_array($protocolo)) {
118
            return $this;
119
        }
120 1
        parent::fromArray($protocolo);
121 1
        if (isset($protocolo['chave'])) {
122
            $this->setChave($protocolo['chave']);
123
        } else {
124 1
            $this->setChave(null);
125
        }
126 1
        if (isset($protocolo['validacao'])) {
127
            $this->setValidacao($protocolo['validacao']);
128
        } else {
129 1
            $this->setValidacao(null);
130
        }
131 1
        if (isset($protocolo['numero'])) {
132
            $this->setNumero($protocolo['numero']);
133
        } else {
134 1
            $this->setNumero(null);
135
        }
136 1
        return $this;
137
    }
138
139
    public function loadNode($element, $name = null)
140
    {
141
        $name = is_null($name)?'infProt':$name;
142
        $info = parent::loadNode($element, $name);
143
        $this->setChave($info->getElementsByTagName('chNFe')->item(0)->nodeValue);
144
        $_fields = $info->getElementsByTagName('digVal');
145
        $validacao = null;
146
        if ($_fields->length > 0) {
147
            $validacao = $_fields->item(0)->nodeValue;
148
        }
149
        $this->setValidacao($validacao);
150
        $_fields = $info->getElementsByTagName('nProt');
151
        $numero = null;
152
        if ($_fields->length > 0) {
153
            $numero = $_fields->item(0)->nodeValue;
154
        }
155
        $this->setNumero($numero);
156
        return $info;
157
    }
158
159
    public function getNode($name = null)
160
    {
161
        $old_uf = $this->getUF();
162
        $this->setUF(null);
163
        $info = parent::getNode('infProt');
164
        $this->setUF($old_uf);
165
        $dom = $info->ownerDocument;
166
        $element = $dom->createElement(is_null($name)?'protNFe':$name);
167
        $versao = $dom->createAttribute('versao');
168
        $versao->value = Nota::VERSAO;
169
        $element->appendChild($versao);
170
171
        $id = $dom->createAttribute('Id');
172
        $id->value = 'ID'.$this->getNumero(true);
173
        $info->appendChild($id);
174
175
        $status = $info->getElementsByTagName('cStat')->item(0);
176
        $info->insertBefore($dom->createElement('nProt', $this->getNumero(true)), $status);
177
        $info->insertBefore($dom->createElement('digVal', $this->getValidacao(true)), $status);
178
        $nodes = $info->getElementsByTagName('dhRecbto');
179
        if ($nodes->length > 0) {
180
            $recebimento = $nodes->item(0);
181
        } else {
182
            $recebimento = $status;
183
        }
184
        $info->insertBefore($dom->createElement('chNFe', $this->getChave(true)), $recebimento);
185
        $element->appendChild($info);
186
        return $element;
187
    }
188
}
189