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

Recibo::loadNode()   C

Complexity

Conditions 8
Paths 38

Size

Total Lines 39
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 34
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 31
nc 38
nop 2
crap 72
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\Exception\ValidationException;
32
33
class Recibo extends Retorno
34
{
35
36
    const INFO_TAGNAME = 'infRec';
37
38
    private $numero;
39
    private $tempo_medio;
40
    private $codigo;
41
    private $mensagem;
42
    private $modelo;
43
44 1
    public function __construct($recibo = array())
45
    {
46 1
        parent::__construct($recibo);
47 1
    }
48
49
    /**
50
     * Número do Recibo
51
     */
52
    public function getNumero($normalize = false)
53
    {
54
        if (!$normalize) {
55
            return $this->numero;
56
        }
57
        return $this->numero;
58
    }
59
60 1
    public function setNumero($numero)
61
    {
62 1
        $this->numero = $numero;
63 1
        return $this;
64
    }
65
66
    /**
67
     * Tempo médio de resposta do serviço (em segundos) dos últimos 5 minutos
68
     */
69
    public function getTempoMedio($normalize = false)
70
    {
71
        if (!$normalize) {
72
            return $this->tempo_medio;
73
        }
74
        return $this->tempo_medio;
75
    }
76
77 1
    public function setTempoMedio($tempo_medio)
78
    {
79 1
        $this->tempo_medio = $tempo_medio;
80 1
        return $this;
81
    }
82
83
    /**
84
     * Código da Mensagem (v2.0) alterado para tamanho variavel 1-4.
85
     * (NT2011/004)
86
     */
87
    public function getCodigo($normalize = false)
88
    {
89
        if (!$normalize) {
90
            return $this->codigo;
91
        }
92
        return $this->codigo;
93
    }
94
95 1
    public function setCodigo($codigo)
96
    {
97 1
        $this->codigo = $codigo;
98 1
        return $this;
99
    }
100
101
    /**
102
     * Mensagem da SEFAZ para o emissor. (v2.0)
103
     */
104
    public function getMensagem($normalize = false)
105
    {
106
        if (!$normalize) {
107
            return $this->mensagem;
108
        }
109
        return $this->mensagem;
110
    }
111
112 1
    public function setMensagem($mensagem)
113
    {
114 1
        $this->mensagem = $mensagem;
115 1
        return $this;
116
    }
117
118
    /**
119
     * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e.
120
     */
121
    public function getModelo($normalize = false)
122
    {
123
        if (!$normalize) {
124
            return $this->modelo;
125
        }
126
        return $this->modelo;
127
    }
128
129 1
    public function setModelo($modelo)
130
    {
131 1
        $this->modelo = $modelo;
132 1
        return $this;
133
    }
134
135
    public function toArray()
136
    {
137
        $recibo = parent::toArray();
138
        $recibo['numero'] = $this->getNumero();
139
        $recibo['tempo_medio'] = $this->getTempoMedio();
140
        $recibo['codigo'] = $this->getCodigo();
141
        $recibo['mensagem'] = $this->getMensagem();
142
        $recibo['modelo'] = $this->getModelo();
143
        return $recibo;
144
    }
145
146 1
    public function fromArray($recibo = array())
147
    {
148 1
        if ($recibo instanceof Recibo) {
149
            $recibo = $recibo->toArray();
150 1
        } elseif (!is_array($recibo)) {
151
            return $this;
152
        }
153 1
        parent::fromArray($recibo);
154 1
        if (isset($recibo['numero'])) {
155
            $this->setNumero($recibo['numero']);
156
        } else {
157 1
            $this->setNumero(null);
158
        }
159 1
        if (isset($recibo['tempo_medio'])) {
160
            $this->setTempoMedio($recibo['tempo_medio']);
161
        } else {
162 1
            $this->setTempoMedio(null);
163
        }
164 1
        if (isset($recibo['codigo'])) {
165
            $this->setCodigo($recibo['codigo']);
166
        } else {
167 1
            $this->setCodigo(null);
168
        }
169 1
        if (isset($recibo['mensagem'])) {
170
            $this->setMensagem($recibo['mensagem']);
171
        } else {
172 1
            $this->setMensagem(null);
173
        }
174 1
        if (isset($recibo['modelo'])) {
175
            $this->setModelo($recibo['modelo']);
176
        } else {
177 1
            $this->setModelo(null);
178
        }
179 1
        return $this;
180
    }
181
182
    public function envia($dom)
183
    {
184
        $envio = new Envio();
185
        $envio->setServico(Envio::SERVICO_RETORNO);
186
        $envio->setAmbiente($this->getAmbiente());
187
        $envio->setModelo($this->getModelo());
188
        $envio->setEmissao(Nota::EMISSAO_NORMAL);
189
        $envio->setConteudo($dom);
190
        $resp = $envio->envia();
191
        $this->loadNode($resp);
192
        if (!$this->isProcessado()) {
193
            return $this;
194
        }
195
        $protocolo = new Protocolo();
196
        $protocolo->loadNode($resp);
197
        return $protocolo;
198
    }
199
200
    public function consulta($nota = null)
201
    {
202
        if (!is_null($nota)) {
203
            $this->setAmbiente($nota->getAmbiente());
204
            $this->setModelo($nota->getModelo());
205
        }
206
        $dom = $this->getNode()->ownerDocument;
207
        $dom = $this->validar($dom);
208
        $retorno = $this->envia($dom);
209
        if ($retorno->isAutorizado() && !is_null($nota)) {
210
            $nota->setProtocolo($retorno);
211
        }
212
        return $retorno;
213
    }
214
215
    public function getNode($name = null)
216
    {
217
        $dom = new \DOMDocument('1.0', 'UTF-8');
218
        $element = $dom->createElement(is_null($name)?'consReciNFe':$name);
219
        $element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
220
        $versao = $dom->createAttribute('versao');
221
        $versao->value = Nota::VERSAO;
222
        $element->appendChild($versao);
223
224
        $element->appendChild($dom->createElement('tpAmb', $this->getAmbiente(true)));
225
        $element->appendChild($dom->createElement('nRec', $this->getNumero(true)));
226
        $dom->appendChild($element);
227
        return $element;
228
    }
229
230
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 240 execution paths which exceeds the configured maximum of 200.

A high number of execution paths generally suggests many nested conditional statements and make the code less readible. This can usually be fixed by splitting the method into several smaller methods.

You can also find more information in the “Code” section of your repository.

Loading history...
231
    {
232
        $name = is_null($name)?'retConsReciNFe':$name;
233
        if ($name == 'infRec') {
234
            $_fields = $element->getElementsByTagName($name);
235
            if ($_fields->length == 0) {
236
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
237
            }
238
            $element = $_fields->item(0);
239
        } else {
240
            $element = parent::loadNode($element, $name);
241
        }
242
        $_fields = $element->getElementsByTagName('nRec');
243
        if ($_fields->length > 0) {
244
            $numero = $_fields->item(0)->nodeValue;
245
        } else {
246
            throw new \Exception('Tag "nRec" do campo "Numero" não encontrada', 404);
247
        }
248
        $this->setNumero($numero);
249
        $_fields = $element->getElementsByTagName('tMed');
250
        $tempo_medio = null;
251
        if ($_fields->length > 0) {
252
            $tempo_medio = $_fields->item(0)->nodeValue;
253
        }
254
        $this->setTempoMedio($tempo_medio);
255
        $_fields = $element->getElementsByTagName('cMsg');
256
        $codigo = null;
257
        if ($_fields->length > 0) {
258
            $codigo = $_fields->item(0)->nodeValue;
259
        }
260
        $this->setCodigo($codigo);
261
        $_fields = $element->getElementsByTagName('xMsg');
262
        $mensagem = null;
263
        if ($_fields->length > 0) {
264
            $mensagem = $_fields->item(0)->nodeValue;
265
        }
266
        $this->setMensagem($mensagem);
267
        return $element;
268
    }
269
270
    /**
271
     * Valida o documento após assinar
272
     */
273
    public function validar($dom)
274
    {
275
        $dom->loadXML($dom->saveXML());
276
        $xsd_path = dirname(__DIR__) . '/Core/schema';
277
        $xsd_file = $xsd_path . '/consReciNFe_v3.10.xsd';
278
        if (!file_exists($xsd_file)) {
279
            throw new \Exception('O arquivo "'.$xsd_file.'" de esquema XSD não existe!', 404);
280
        }
281
        // Enable user error handling
282
        $save = libxml_use_internal_errors(true);
283
        if ($dom->schemaValidate($xsd_file)) {
284
            libxml_use_internal_errors($save);
285
            return $dom;
286
        }
287
        $msg = array();
288
        $errors = libxml_get_errors();
289
        foreach ($errors as $error) {
290
            $msg[] = 'Não foi possível validar o XML: '.$error->message;
291
        }
292
        libxml_clear_errors();
293
        libxml_use_internal_errors($save);
294
        throw new ValidationException($msg);
295
    }
296
}
297