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

Evento::loadNode()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 26
cts 28
cp 0.9286
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 26
nc 6
nop 2
crap 4.0058
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
use FR3D\XmlDSig\Adapter\AdapterInterface;
35
use FR3D\XmlDSig\Adapter\XmlseclibsAdapter;
36
37
class Evento extends Retorno
0 ignored issues
show
Complexity introduced by
This class has a complexity of 95 which exceeds the configured maximum of 50.

The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.

Some resources for further reading:

You can also find more detailed suggestions for refactoring in the “Code” section of your repository.

Loading history...
38
{
39
40
    const VERSAO = '1.00';
41
42
    const TIPO_CANCELAMENTO = '110111';
43
44
    private $id;
45
    private $orgao;
46
    private $identificador;
47
    private $chave;
48
    private $data;
49
    private $tipo;
50
    private $sequencia;
51
    private $descricao;
52
    private $numero;
53
    private $justificativa;
54
    private $email;
55
    private $modelo;
56
    private $informacao;
57
58 6
    public function __construct($evento = array())
59
    {
60 6
        parent::__construct($evento);
61 6
    }
62
63
    /**
64
     * Identificador da TAG a ser assinada, a regra de formação do Id é: "ID" +
65
     * tpEvento +  chave da NF-e + nSeqEvento
66
     */
67 5
    public function getID($normalize = false)
68
    {
69 5
        if (!$normalize) {
70 2
            return $this->id;
71
        }
72 4
        return 'ID'.$this->id;
73
    }
74
75 6
    public function setID($id)
76
    {
77 6
        $this->id = $id;
78 6
        return $this;
79
    }
80
81
    /**
82
     * Código do órgão de recepção do Evento. Utilizar a Tabela do IBGE
83
     * extendida, utilizar 91 para identificar o Ambiente Nacional
84
     */
85 4
    public function getOrgao($normalize = false)
86
    {
87 4
        if (!$normalize || is_numeric($this->orgao)) {
88 2
            return $this->orgao;
89
        }
90
91 4
        $db = SEFAZ::getInstance()->getConfiguracao()->getBanco();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
92 4
        return $db->getCodigoOrgao($this->orgao);
93
    }
94
95 6
    public function setOrgao($orgao)
96
    {
97 6
        $this->orgao = $orgao;
98 6
        return $this;
99
    }
100
101
    /**
102
     * Identificação do  autor do evento
103
     */
104 4
    public function getIdentificador($normalize = false)
105
    {
106 4
        if (!$normalize) {
107 4
            return $this->identificador;
108
        }
109 4
        return $this->identificador;
110
    }
111
112 6
    public function setIdentificador($identificador)
113
    {
114 6
        $this->identificador = $identificador;
115 6
        return $this;
116
    }
117
118
    /**
119
     * Chave de Acesso da NF-e vinculada ao evento
120
     */
121 4
    public function getChave($normalize = false)
122
    {
123 4
        if (!$normalize) {
124 2
            return $this->chave;
125
        }
126 4
        return $this->chave;
127
    }
128
129 6
    public function setChave($chave)
130
    {
131 6
        $this->chave = $chave;
132 6
        return $this;
133
    }
134
135
    /**
136
     * Data e Hora do Evento, formato UTC (AAAA-MM-DDThh:mm:ssTZD, onde TZD =
137
     * +hh:mm ou -hh:mm)
138
     */
139 4
    public function getData($normalize = false)
140
    {
141 4
        if (!$normalize) {
142 1
            return $this->data;
143
        }
144 4
        return Util::toDateTime($this->data);
145
    }
146
147 6
    public function setData($data)
148
    {
149 6
        if (!is_numeric($data)) {
150 6
            $data = strtotime($data);
151 6
        }
152 6
        $this->data = $data;
153 6
        return $this;
154
    }
155
156
    /**
157
     * Tipo do Evento
158
     */
159 4
    public function getTipo($normalize = false)
160
    {
161 4
        if (!$normalize) {
162 1
            return $this->tipo;
163
        }
164 4
        return $this->tipo;
165
    }
166
167 6
    public function setTipo($tipo)
168
    {
169 6
        $this->tipo = $tipo;
170 6
        return $this;
171
    }
172
173
    /**
174
     * Seqüencial do evento para o mesmo tipo de evento.  Para maioria dos
175
     * eventos será 1, nos casos em que possa existir mais de um evento, como é
176
     * o caso da carta de correção, o autor do evento deve numerar de forma
177
     * seqüencial.
178
     */
179 4
    public function getSequencia($normalize = false)
180
    {
181 4
        if (!$normalize) {
182 1
            return $this->sequencia;
183
        }
184 4
        return $this->sequencia;
185
    }
186
187 6
    public function setSequencia($sequencia)
188
    {
189 6
        $this->sequencia = $sequencia;
190 6
        return $this;
191
    }
192
193
    /**
194
     * Descrição do Evento
195
     */
196 4
    public function getDescricao($normalize = false)
197
    {
198 4
        if (!$normalize) {
199 1
            return $this->descricao;
200
        }
201 4
        return $this->descricao;
202
    }
203
204 6
    public function setDescricao($descricao)
205
    {
206 6
        $this->descricao = $descricao;
207 6
        return $this;
208
    }
209
210
    /**
211
     * Número do Protocolo de Status da NF-e. 1 posição (1 – Secretaria de
212
     * Fazenda Estadual 2 – Receita Federal); 2 posições ano; 10 seqüencial no
213
     * ano.
214
     */
215 4
    public function getNumero($normalize = false)
216
    {
217 4
        if (!$normalize) {
218 1
            return $this->numero;
219
        }
220 4
        return $this->numero;
221
    }
222
223 6
    public function setNumero($numero)
224
    {
225 6
        $this->numero = $numero;
226 6
        return $this;
227
    }
228
229
    /**
230
     * Justificativa do cancelamento
231
     */
232 4
    public function getJustificativa($normalize = false)
233
    {
234 4
        if (!$normalize) {
235 1
            return $this->justificativa;
236
        }
237 4
        return $this->justificativa;
238
    }
239
240 6
    public function setJustificativa($justificativa)
241
    {
242 6
        $this->justificativa = $justificativa;
243 6
        return $this;
244
    }
245
246
    /**
247
     * email do destinatário
248
     */
249 2
    public function getEmail($normalize = false)
250
    {
251 2
        if (!$normalize) {
252 2
            return $this->email;
253
        }
254
        return $this->email;
255
    }
256
257 6
    public function setEmail($email)
258
    {
259 6
        $this->email = $email;
260 6
        return $this;
261
    }
262
263
    /**
264
     * Código do modelo do Documento Fiscal. 55 = NF-e; 65 = NFC-e.
265
     * @param boolean $normalize informa se o modelo deve estar no formato do XML
266
     * @return mixed modelo do Envio
267
     */
268 3
    public function getModelo($normalize = false)
269
    {
270 3
        if (!$normalize) {
271 3
            return $this->modelo;
272
        }
273
        switch ($this->modelo) {
274
            case Nota::MODELO_NFE:
275
                return '55';
276
            case Nota::MODELO_NFCE:
277
                return '65';
278
        }
279
        return $this->modelo;
280
    }
281
282
    /**
283
     * Altera o valor do Modelo para o informado no parâmetro
284
     * @param mixed $modelo novo valor para Modelo
285
     * @return Envio A própria instância da classe
286
     */
287 6
    public function setModelo($modelo)
288
    {
289
        switch ($modelo) {
290 6
            case '55':
291
                $modelo = Nota::MODELO_NFE;
292
                break;
293 6
            case '65':
294
                $modelo = Nota::MODELO_NFCE;
295
                break;
296
        }
297 6
        $this->modelo = $modelo;
298 6
        return $this;
299
    }
300
301
    /**
302
     * Resposta de informação do evento
303
     */
304 3
    public function getInformacao()
305
    {
306 3
        return $this->informacao;
307
    }
308
309 6
    public function setInformacao($informacao)
310
    {
311 6
        $this->informacao = $informacao;
312 6
        return $this;
313
    }
314
315
    /**
316
     * Informa se a identificação é um CNPJ
317
     */
318 4
    public function isCNPJ()
319
    {
320 4
        return strlen($this->getIdentificador()) == 14;
321
    }
322
323
    /**
324
     * Informa se o lote já foi processado e já tem um protocolo
325
     */
326 3
    public function isProcessado()
327
    {
328 3
        return $this->getStatus() == '128';
329
    }
330
331
    /**
332
     * Informa se a nota foi cancelada com sucesso
333
     */
334 1
    public function isCancelado()
335
    {
336 1
        return in_array($this->getStatus(), array('135', '155'));
337
    }
338
339 1
    public function toArray($recursive = false)
340
    {
341 1
        $evento = parent::toArray($recursive);
342 1
        $evento['id'] = $this->getID();
343 1
        $evento['orgao'] = $this->getOrgao();
344 1
        $evento['identificador'] = $this->getIdentificador();
345 1
        $evento['chave'] = $this->getChave();
346 1
        $evento['data'] = $this->getData();
347 1
        $evento['tipo'] = $this->getTipo();
348 1
        $evento['sequencia'] = $this->getSequencia();
349 1
        $evento['descricao'] = $this->getDescricao();
350 1
        $evento['numero'] = $this->getNumero();
351 1
        $evento['justificativa'] = $this->getJustificativa();
352 1
        $evento['email'] = $this->getEmail();
353 1
        $evento['modelo'] = $this->getModelo();
354 1
        $evento['informacao'] = $this->getInformacao();
355 1
        return $evento;
356
    }
357
358 6
    public function fromArray($evento = array())
0 ignored issues
show
Complexity introduced by
This operation has 82944 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...
359
    {
360 6
        if ($evento instanceof Evento) {
361 1
            $evento = $evento->toArray();
362 6
        } elseif (!is_array($evento)) {
363 1
            return $this;
364
        }
365 6
        parent::fromArray($evento);
366 6
        if (isset($evento['id'])) {
367 1
            $this->setID($evento['id']);
368 1
        } else {
369 6
            $this->setID(null);
370
        }
371 6
        if (isset($evento['orgao'])) {
372 1
            $this->setOrgao($evento['orgao']);
373 1
        } else {
374 6
            $this->setOrgao(null);
375
        }
376 6
        if (isset($evento['identificador'])) {
377 1
            $this->setIdentificador($evento['identificador']);
378 1
        } else {
379 6
            $this->setIdentificador(null);
380
        }
381 6
        if (isset($evento['chave'])) {
382 1
            $this->setChave($evento['chave']);
383 1
        } else {
384 6
            $this->setChave(null);
385
        }
386 6
        if (isset($evento['data'])) {
387 1
            $this->setData($evento['data']);
388 1
        } else {
389 6
            $this->setData(null);
390
        }
391 6
        if (!isset($evento['tipo']) || is_null($evento['tipo'])) {
392 6
            $this->setTipo(self::TIPO_CANCELAMENTO);
393 6
        } else {
394 1
            $this->setTipo($evento['tipo']);
395
        }
396 6
        if (!isset($evento['sequencia']) || is_null($evento['sequencia'])) {
397 6
            $this->setSequencia(1);
398 6
        } else {
399 1
            $this->setSequencia($evento['sequencia']);
400
        }
401 6
        if (!isset($evento['descricao']) || is_null($evento['descricao'])) {
402 6
            $this->setDescricao('Cancelamento');
403 6
        } else {
404 1
            $this->setDescricao($evento['descricao']);
405
        }
406 6
        if (isset($evento['numero'])) {
407 1
            $this->setNumero($evento['numero']);
408 1
        } else {
409 6
            $this->setNumero(null);
410
        }
411 6
        if (isset($evento['justificativa'])) {
412 1
            $this->setJustificativa($evento['justificativa']);
413 1
        } else {
414 6
            $this->setJustificativa(null);
415
        }
416 6
        if (isset($evento['email'])) {
417
            $this->setEmail($evento['email']);
418
        } else {
419 6
            $this->setEmail(null);
420
        }
421 6
        if (isset($evento['modelo'])) {
422 1
            $this->setModelo($evento['modelo']);
423 1
        } else {
424 6
            $this->setModelo(null);
425
        }
426 6
        if (isset($evento['informacao'])) {
427 1
            $this->setInformacao($evento['informacao']);
428 1
        } else {
429 6
            $this->setInformacao(null);
430
        }
431 6
        return $this;
432
    }
433
434
    /**
435
     * Gera o ID, a regra de formação do Id é: "ID" +
436
     * tpEvento +  chave da NF-e + nSeqEvento
437
     */
438 4
    public function gerarID()
439
    {
440 4
        $id = sprintf(
441 4
            '%s%s%02d',
442 4
            $this->getTipo(true),
443 4
            $this->getChave(true),
444 4
            $this->getSequencia(true)
445 4
        );
446 4
        return $id;
447
    }
448
449 4
    public function getNode($name = null)
450
    {
451 4
        $this->setID($this->gerarID());
452
453 4
        $dom = new \DOMDocument('1.0', 'UTF-8');
454 4
        $element = $dom->createElement(is_null($name)?'evento':$name);
455 4
        $element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
456 4
        $versao = $dom->createAttribute('versao');
457 4
        $versao->value = self::VERSAO;
458 4
        $element->appendChild($versao);
459
460 4
        $info = $dom->createElement('infEvento');
461 4
        $dom = $element->ownerDocument;
462 4
        $id = $dom->createAttribute('Id');
463 4
        $id->value = $this->getID(true);
464 4
        $info->appendChild($id);
465
        
466 4
        Util::appendNode($info, 'cOrgao', $this->getOrgao(true));
467 4
        Util::appendNode($info, 'tpAmb', $this->getAmbiente(true));
468 4
        if ($this->isCNPJ()) {
469 4
            Util::appendNode($info, 'CNPJ', $this->getIdentificador(true));
470 4
        } else {
471 2
            Util::appendNode($info, 'CPF', $this->getIdentificador(true));
472
        }
473 4
        Util::appendNode($info, 'chNFe', $this->getChave(true));
474 4
        Util::appendNode($info, 'dhEvento', $this->getData(true));
475 4
        Util::appendNode($info, 'tpEvento', $this->getTipo(true));
476 4
        Util::appendNode($info, 'nSeqEvento', $this->getSequencia(true));
477 4
        Util::appendNode($info, 'verEvento', self::VERSAO);
478
479 4
        $detalhes = $dom->createElement('detEvento');
480 4
        $versao = $dom->createAttribute('versao');
481 4
        $versao->value = self::VERSAO;
482 4
        $detalhes->appendChild($versao);
483
484 4
        Util::appendNode($detalhes, 'descEvento', $this->getDescricao(true));
485 4
        Util::appendNode($detalhes, 'nProt', $this->getNumero(true));
486 4
        Util::appendNode($detalhes, 'xJust', $this->getJustificativa(true));
487 4
        $info->appendChild($detalhes);
488
489 4
        $element->appendChild($info);
490 4
        $dom->appendChild($element);
491 4
        return $element;
492
    }
493
494 2
    public function getReturnNode()
495
    {
496 2
        $outros = parent::getNode('infEvento');
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getNode() instead of getReturnNode()). Are you sure this is correct? If so, you might want to change this to $this->getNode().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
497 2
        $element = $this->getNode('retEvento');
498 2
        $dom = $element->ownerDocument;
499 2
        $element->removeAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns');
500 2
        $info = $dom->getElementsByTagName('infEvento')->item(0);
501 2
        $info->removeAttribute('Id');
502 2
        $removeTags = array('detEvento', 'verEvento', 'dhEvento', 'CNPJ', 'CPF', 'cOrgao');
0 ignored issues
show
Coding Style introduced by
$removeTags 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...
503 2
        foreach ($removeTags as $key) {
0 ignored issues
show
Coding Style introduced by
$removeTags 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...
504 2
            $_fields = $info->getElementsByTagName($key);
505 2
            if ($_fields->length == 0) {
506 2
                continue;
507
            }
508 2
            $node = $_fields->item(0);
509 2
            $info->removeChild($node);
510 2
        }
511 2
        $chave = $info->getElementsByTagName('chNFe')->item(0);
512 2
        foreach ($outros->childNodes as $node) {
513 2
            $node = $dom->importNode($node, true);
514 2
            $list = $info->getElementsByTagName($node->nodeName);
515 2
            if ($list->length == 1) {
516 2
                continue;
517
            }
518 2
            $info->insertBefore($node, $chave);
519 2
        }
520 2
        $status = $info->getElementsByTagName('cStat')->item(0);
521 2
        Util::appendNode($info, 'cOrgao', $this->getOrgao(true), $status);
522 2
        $sequencia = $info->getElementsByTagName('nSeqEvento')->item(0);
523 2
        Util::appendNode($info, 'xEvento', $this->getDescricao(true), $sequencia);
524 2
        if (!is_null($this->getIdentificador())) {
525
            if ($this->isCNPJ()) {
526
                Util::appendNode($info, 'CNPJDest', $this->getIdentificador(true));
527
            } else {
528
                Util::appendNode($info, 'CPFDest', $this->getIdentificador(true));
529
            }
530
        }
531 2
        if (!is_null($this->getEmail())) {
532
            Util::appendNode($info, 'emailDest', $this->getEmail(true));
533
        }
534 2
        Util::appendNode($info, 'dhRegEvento', $this->getData(true));
535 2
        Util::appendNode($info, 'nProt', $this->getNumero(true));
536 2
        return $element;
537
    }
538
539 3
    public function loadNode($element, $name = null)
540
    {
541 3
        $name = is_null($name)?'infEvento':$name;
542 3
        $element = parent::loadNode($element, $name);
543 3
        $this->setOrgao(
544 3
            Util::loadNode(
545 3
                $element,
546 3
                'cOrgao',
547
                'Tag "cOrgao" do campo "Orgao" não encontrada'
548 3
            )
549 3
        );
550 3
        if ($name == 'retEnvEvento') {
551 3
            return $element;
552
        }
553 3
        $this->setChave(Util::loadNode($element, 'chNFe'));
554 3
        $this->setTipo(Util::loadNode($element, 'tpEvento'));
555 3
        $this->setDescricao(Util::loadNode($element, 'xEvento'));
556 3
        $this->setSequencia(Util::loadNode($element, 'nSeqEvento'));
557 3
        if ($element->getElementsByTagName('CNPJDest')->length > 0) {
558
            $this->setIdentificador(Util::loadNode($element, 'CNPJDest'));
559
        } else {
560 3
            $this->setIdentificador(Util::loadNode($element, 'CPFDest'));
561
        }
562 3
        $this->setEmail(Util::loadNode($element, 'emailDest'));
563 3
        $this->setData(
564 3
            Util::loadNode(
565 3
                $element,
566 3
                'dhRegEvento',
567
                'Tag "dhRegEvento" do campo "Data" não encontrada'
568 3
            )
569 3
        );
570 3
        $this->setNumero(Util::loadNode($element, 'nProt'));
571 3
        return $element;
572
    }
573
574 3
    private function getConteudo($dom)
575
    {
576 3
        $config = SEFAZ::getInstance()->getConfiguracao();
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
577 3
        $dob = new \DOMDocument('1.0', 'UTF-8');
578 3
        $envio = $dob->createElement('envEvento');
579 3
        $envio->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
580 3
        $versao = $dob->createAttribute('versao');
581 3
        $versao->value = self::VERSAO;
582 3
        $envio->appendChild($versao);
583 3
        Util::appendNode($envio, 'idLote', self::genLote());
584
        // Corrige xmlns:default
585
        // $data = $dob->importNode($dom->documentElement, true);
586
        // $envio->appendChild($data);
587 3
        Util::appendNode($envio, 'evento', 0);
588 3
        $dob->appendChild($envio);
589
        // Corrige xmlns:default
590
        // return $dob;
591 3
        $xml = $dob->saveXML($dob->documentElement);
592 3
        return str_replace('<evento>0</evento>', $dom->saveXML($dom->documentElement), $xml);
593
    }
594
595 3
    public function envia($dom)
596
    {
597 3
        $envio = new Envio();
598 3
        $envio->setServico(Envio::SERVICO_EVENTO);
599 3
        $envio->setAmbiente($this->getAmbiente());
600 3
        $envio->setModelo($this->getModelo());
601 3
        $envio->setEmissao(Nota::EMISSAO_NORMAL);
602 3
        $envio->setConteudo($this->getConteudo($dom));
603 3
        $resp = $envio->envia();
604 3
        $this->loadNode($resp, 'retEnvEvento');
605 3
        if (!$this->isProcessado()) {
606
            throw new \Exception($this->getMotivo(), $this->getStatus());
607
        }
608 3
        $retorno = new Evento();
609 3
        $retorno->loadNode($resp);
610 3
        $this->setInformacao($retorno);
611 3
        return $retorno;
612
    }
613
614
    /**
615
     * Adiciona a informação no XML do evento
616
     */
617 3
    public function addInformacao($dom)
618
    {
619 3
        if (is_null($this->getInformacao())) {
620 1
            throw new \Exception('A informação não foi informado no evento "'.$this->getID().'"', 404);
621
        }
622 2
        $evento = $dom->getElementsByTagName('evento')->item(0);
623
        // Corrige xmlns:default
624 2
        $evento_xml = $dom->saveXML($evento);
625
626 2
        $element = $dom->createElement('procEventoNFe');
627 2
        $element->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns', Nota::PORTAL);
628 2
        $versao = $dom->createAttribute('versao');
629 2
        $versao->value = self::VERSAO;
630 2
        $element->appendChild($versao);
631 2
        $dom->removeChild($evento);
632
        // Corrige xmlns:default
633 2
        $evento = $dom->createElement('evento', 0);
634
635 2
        $element->appendChild($evento);
636 2
        $info = $this->getInformacao()->getReturnNode();
637 2
        $info = $dom->importNode($info, true);
638 2
        $element->appendChild($info);
639 2
        $dom->appendChild($element);
640
        // Corrige xmlns:default
641 2
        $xml = $dom->saveXML();
642 2
        $xml = str_replace('<evento>0</evento>', $evento_xml, $xml);
643 2
        $dom->loadXML($xml);
644
645 2
        return $dom;
646
    }
647
648
    /**
649
     * Assina o XML com a assinatura eletrônica do tipo A1
650
     */
651 4
    public function assinar($dom = null)
652
    {
653 4
        if (is_null($dom)) {
654 1
            $xml = $this->getNode();
655 1
            $dom = $xml->ownerDocument;
656 1
        }
657 4
        $config = SEFAZ::getInstance()->getConfiguracao();
658
659 4
        $adapter = new XmlseclibsAdapter();
660 4
        $adapter->setPrivateKey($config->getChavePrivada());
661 4
        $adapter->setPublicKey($config->getChavePublica());
662 4
        $adapter->addTransform(AdapterInterface::ENVELOPED);
663 4
        $adapter->addTransform(AdapterInterface::XML_C14N);
664 4
        $adapter->sign($dom, 'infEvento');
665 4
        return $dom;
666
    }
667
668
    /**
669
     * Valida o documento após assinar
670
     */
671 4
    public function validar($dom)
672
    {
673 4
        $dom->loadXML($dom->saveXML());
674 4
        $xsd_path = dirname(__DIR__) . '/Core/schema';
675 4
        $xsd_file = $xsd_path . '/cancelamento/eventoCancNFe_v1.00.xsd';
676 4
        if (!file_exists($xsd_file)) {
677
            throw new \Exception('O arquivo "'.$xsd_file.'" de esquema XSD não existe!', 404);
678
        }
679
        // Enable user error handling
680 4
        $save = libxml_use_internal_errors(true);
681 4
        if ($dom->schemaValidate($xsd_file)) {
682 3
            libxml_use_internal_errors($save);
683 3
            return $dom;
684
        }
685 1
        $msg = array();
686 1
        $errors = libxml_get_errors();
687 1
        foreach ($errors as $error) {
688 1
            $msg[] = 'Não foi possível validar o XML: '.$error->message;
689 1
        }
690 1
        libxml_clear_errors();
691 1
        libxml_use_internal_errors($save);
692 1
        throw new ValidationException($msg);
693
    }
694
}
695