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

Parcial::loadNode()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 62
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 62
ccs 40
cts 44
cp 0.9091
rs 8.9167
c 0
b 0
f 0
cc 4
eloc 44
nc 6
nop 2
crap 4.0119

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Substituido;
32
33
/**
34
 * Tributação pelo ICMS
35
 * 30 - Isenta ou não tributada e com cobrança do ICMS
36
 * por substituição tributária, estende de Base
37
 */
38
class Parcial extends Base
39
{
40
41
    /**
42
     * Modalidade de determinação da BC do ICMS ST:
43
     * 0 – Preço tabelado ou
44
     * máximo  sugerido;
45
     * 1 - Lista Negativa (valor);
46
     * 2 - Lista Positiva
47
     * (valor);
48
     * 3 - Lista Neutra (valor);
49
     * 4 - Margem Valor Agregado (%);
50
     * 5 -
51
     * Pauta (valor).
52
     */
53
    const MODALIDADE_TABELADO = 'tabelado';
54
    const MODALIDADE_NEGATIVO = 'negativo';
55
    const MODALIDADE_POSITIVO = 'positivo';
56
    const MODALIDADE_NEUTRO = 'neutro';
57
    const MODALIDADE_AGREGADO = 'agregado';
58
    const MODALIDADE_PAUTA = 'pauta';
59
60
    private $modalidade;
61
    private $margem;
62
    private $reducao;
63
64 76
    public function __construct($parcial = [])
65
    {
66 76
        parent::__construct($parcial);
67 76
    }
68
69
    /**
70
     * Modalidade de determinação da BC do ICMS ST:
71
     * 0 – Preço tabelado ou
72
     * máximo  sugerido;
73
     * 1 - Lista Negativa (valor);
74
     * 2 - Lista Positiva
75
     * (valor);
76
     * 3 - Lista Neutra (valor);
77
     * 4 - Margem Valor Agregado (%);
78
     * 5 -
79
     * Pauta (valor).
80
     */
81 74
    public function getModalidade($normalize = false)
82
    {
83 74
        if (!$normalize) {
84 71
            return $this->modalidade;
85
        }
86 18
        switch ($this->modalidade) {
87 18
            case self::MODALIDADE_TABELADO:
88
                return '0';
89 18
            case self::MODALIDADE_NEGATIVO:
90
                return '1';
91 18
            case self::MODALIDADE_POSITIVO:
92
                return '2';
93 18
            case self::MODALIDADE_NEUTRO:
94
                return '3';
95 18
            case self::MODALIDADE_AGREGADO:
96 9
                return '4';
97 9
            case self::MODALIDADE_PAUTA:
98
                return '5';
99
        }
100 9
        return $this->modalidade;
101
    }
102
103 76
    public function setModalidade($modalidade)
104
    {
105 76
        $this->modalidade = $modalidade;
106 76
        return $this;
107
    }
108
109 36
    public function getMargem($normalize = false)
110
    {
111 36
        if (!$normalize) {
112 27
            return $this->margem;
113
        }
114 18
        return Util::toFloat($this->margem);
115
    }
116
117 76
    public function setMargem($margem)
118
    {
119 76
        $this->margem = $margem;
120 76
        return $this;
121
    }
122
123 36
    public function getReducao($normalize = false)
124
    {
125 36
        if (!$normalize) {
126 28
            return $this->reducao;
127
        }
128 18
        return Util::toFloat($this->reducao);
129
    }
130
131 76
    public function setReducao($reducao)
132
    {
133 76
        $this->reducao = $reducao;
134 76
        return $this;
135
    }
136
137 27
    public function toArray($recursive = false)
138
    {
139 27
        $parcial = parent::toArray($recursive);
140 27
        $parcial['modalidade'] = $this->getModalidade();
141 27
        $parcial['margem'] = $this->getMargem();
142 27
        $parcial['reducao'] = $this->getReducao();
143 27
        return $parcial;
144
    }
145
146 76
    public function fromArray($parcial = [])
147
    {
148 76
        if ($parcial instanceof Parcial) {
149 2
            $parcial = $parcial->toArray();
150 76
        } elseif (!is_array($parcial)) {
151 2
            return $this;
152
        }
153 76
        parent::fromArray($parcial);
154 76
        if (isset($parcial['modalidade'])) {
155 9
            $this->setModalidade($parcial['modalidade']);
156
        } else {
157 76
            $this->setModalidade(null);
158
        }
159 76
        if (isset($parcial['margem'])) {
160 9
            $this->setMargem($parcial['margem']);
161
        } else {
162 76
            $this->setMargem(null);
163
        }
164 76
        if (isset($parcial['reducao'])) {
165 9
            $this->setReducao($parcial['reducao']);
166
        } else {
167 76
            $this->setReducao(null);
168
        }
169 76
        if (!isset($parcial['fundo']) || !($this->getFundo() instanceof Substituido)) {
170 76
            $this->setFundo(new Substituido());
171
        }
172 76
        if (!isset($parcial['tributacao'])) {
173 76
            $this->setTributacao('30');
174
        }
175 76
        return $this;
176
    }
177
178 14
    public function getNode($name = null)
179
    {
180 14
        $dom = new \DOMDocument('1.0', 'UTF-8');
181 14
        $element = $dom->createElement(is_null($name)?'ICMS30':$name);
182 14
        Util::appendNode($element, 'orig', $this->getOrigem(true));
183 14
        Util::appendNode($element, 'CST', $this->getTributacao(true));
184 14
        Util::appendNode($element, 'modBCST', $this->getModalidade(true));
185 14
        Util::appendNode($element, 'pMVAST', $this->getMargem(true));
186 14
        Util::appendNode($element, 'pRedBCST', $this->getReducao(true));
187 14
        Util::appendNode($element, 'vBCST', $this->getBase(true));
188 14
        Util::appendNode($element, 'pICMSST', $this->getAliquota(true));
189 14
        Util::appendNode($element, 'vICMSST', $this->getValor(true));
190 14
        return $this->exportFundo($element);
191
    }
192
193 7
    public function loadNode($element, $name = null)
194
    {
195 7
        $name = is_null($name)?'ICMS30':$name;
196 7
        if ($element->nodeName != $name) {
197
            $_fields = $element->getElementsByTagName($name);
198
            if ($_fields->length == 0) {
199
                throw new \Exception('Tag "'.$name.'" do ICMS Parcial não encontrada', 404);
200
            }
201
            $element = $_fields->item(0);
202
        }
203 7
        $this->setOrigem(
204 7
            Util::loadNode(
205 7
                $element,
206 7
                'orig',
207 7
                'Tag "orig" do campo "Origem" não encontrada no ICMS Parcial'
208
            )
209
        );
210 7
        $this->setTributacao(
211 7
            Util::loadNode(
212 7
                $element,
213 7
                'CST',
214 7
                'Tag "CST" do campo "Tributacao" não encontrada no ICMS Parcial'
215
            )
216
        );
217 7
        $this->setModalidade(
218 7
            Util::loadNode(
219 7
                $element,
220 7
                'modBCST',
221 7
                'Tag "modBCST" do campo "Modalidade" não encontrada no ICMS Parcial'
222
            )
223
        );
224 7
        $this->setMargem(
225 7
            Util::loadNode(
226 7
                $element,
227 7
                'pMVAST',
228 7
                'Tag "pMVAST" do campo "Margem" não encontrada no ICMS Parcial'
229
            )
230
        );
231 7
        $this->setReducao(
232 7
            Util::loadNode(
233 7
                $element,
234 7
                'pRedBCST',
235 7
                'Tag "pRedBCST" do campo "Reducao" não encontrada no ICMS Parcial'
236
            )
237
        );
238 7
        $this->setBase(
239 7
            Util::loadNode(
240 7
                $element,
241 7
                'vBCST',
242 7
                'Tag "vBCST" do campo "Base" não encontrada no ICMS Parcial'
243
            )
244
        );
245 7
        $this->setAliquota(
246 7
            Util::loadNode(
247 7
                $element,
248 7
                'pICMSST',
249 7
                'Tag "pICMSST" do campo "Aliquota" não encontrada no ICMS Parcial'
250
            )
251
        );
252 7
        $this->importFundo($element);
253 7
        return $element;
254
    }
255
}
256