Test Failed
Push — master ( e53a66...43458c )
by Francimar
16:26
created

Normal::loadNode()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 48
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 4.026

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 30
cts 34
cp 0.8824
rs 8.7396
c 0
b 0
f 0
cc 4
eloc 34
nc 6
nop 2
crap 4.026
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\Entity\Imposto\ICMS;
29
30
use NFe\Common\Util;
31
use NFe\Entity\Imposto\Fundo\Base as Fundo;
32
33
/**
34
 * Classe base do ICMS normal, estende de ICMS\Base
35
 */
36
class Normal extends Base
37
{
38
39
    const MODALIDADE_AGREGADO = 'agregado';
40
    const MODALIDADE_PAUTA = 'pauta';
41
    const MODALIDADE_TABELADO = 'tabelado';
42
    const MODALIDADE_OPERACAO = 'operacao';
43
44
    private $modalidade;
45
46 84
    public function __construct($normal = [])
47
    {
48 84
        parent::__construct($normal);
49 84
    }
50
51 79
    public function getModalidade($normalize = false)
52
    {
53 79
        if (!$normalize) {
54 74
            return $this->modalidade;
55
        }
56 22
        switch ($this->modalidade) {
57 22
            case self::MODALIDADE_AGREGADO:
58
                return '0';
59 22
            case self::MODALIDADE_PAUTA:
60
                return '1';
61 22
            case self::MODALIDADE_TABELADO:
62
                return '2';
63 22
            case self::MODALIDADE_OPERACAO:
64 10
                return '3';
65
        }
66 12
        return $this->modalidade;
67
    }
68
69 84
    public function setModalidade($modalidade)
70
    {
71 84
        $this->modalidade = $modalidade;
72 84
        return $this;
73
    }
74
75 31
    public function toArray($recursive = false)
76
    {
77 31
        $normal = parent::toArray($recursive);
78 31
        $normal['modalidade'] = $this->getModalidade();
79 31
        return $normal;
80
    }
81
82 84
    public function fromArray($normal = [])
83
    {
84 84
        if ($normal instanceof Normal) {
85 20
            $normal = $normal->toArray();
86 84
        } elseif (!is_array($normal)) {
87 1
            return $this;
88
        }
89 84
        parent::fromArray($normal);
90 84
        if (isset($normal['modalidade'])) {
91 11
            $this->setModalidade($normal['modalidade']);
92
        } else {
93 84
            $this->setModalidade(null);
94
        }
95 84
        if (!isset($normal['fundo']) || !($this->getFundo() instanceof Fundo)) {
96 84
            $this->setFundo(new Fundo());
97
        }
98 84
        return $this;
99
    }
100
101 22
    public function getNode($name = null)
102
    {
103 22
        $dom = new \DOMDocument('1.0', 'UTF-8');
104 22
        $element = $dom->createElement(is_null($name)?'ICMS':$name);
105 22
        Util::appendNode($element, 'orig', $this->getOrigem(true));
106 22
        Util::appendNode($element, 'CST', $this->getTributacao(true));
107 22
        Util::appendNode($element, 'modBC', $this->getModalidade(true));
108 22
        Util::appendNode($element, 'vBC', $this->getBase(true));
109 22
        Util::appendNode($element, 'pICMS', $this->getAliquota(true));
110 22
        Util::appendNode($element, 'vICMS', $this->getValor(true));
111 22
        return $this->exportFundo($element);
112
    }
113
114
115 10
    public function loadNode($element, $name = null)
116
    {
117 10
        $name = is_null($name)?'ICMS':$name;
118 10
        if ($element->nodeName != $name) {
119
            $_fields = $element->getElementsByTagName($name);
120
            if ($_fields->length == 0) {
121
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
122
            }
123
            $element = $_fields->item(0);
124
        }
125 10
        $this->setOrigem(
126 10
            Util::loadNode(
127 10
                $element,
128 10
                'orig',
129 10
                'Tag "orig" do campo "Origem" não encontrada'
130
            )
131
        );
132 10
        $this->setTributacao(
133 10
            Util::loadNode(
134 10
                $element,
135 10
                'CST',
136 10
                'Tag "CST" do campo "Tributacao" não encontrada'
137
            )
138
        );
139 10
        $this->setModalidade(
140 10
            Util::loadNode(
141 10
                $element,
142 10
                'modBC',
143 10
                'Tag "modBC" do campo "Modalidade" não encontrada'
144
            )
145
        );
146 10
        $this->setBase(
147 10
            Util::loadNode(
148 10
                $element,
149 10
                'vBC',
150 10
                'Tag "vBC" do campo "Base" não encontrada'
151
            )
152
        );
153 10
        $this->setAliquota(
154 10
            Util::loadNode(
155 10
                $element,
156 10
                'pICMS',
157 10
                'Tag "pICMS" do campo "Aliquota" não encontrada'
158
            )
159
        );
160 10
        $this->importFundo($element);
161 10
        return $element;
162
    }
163
}
164