Completed
Push — master ( dc5f67...84520a )
by Francimar
11:49 queued 07:55
created

Cobranca::fromArray()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 11
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
crap 5
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
32
/**
33
 * Tributação pelo ICMS
34
 * 10 - Tributada e com cobrança do ICMS por
35
 * substituição tributária, estende de Parcial
36
 */
37
class Cobranca extends Parcial
38
{
39
40
    private $normal;
41
42 54
    public function __construct($cobranca = array())
43
    {
44 54
        parent::__construct($cobranca);
45 54
        $this->setTributacao('10');
46 54
    }
47
48 54
    public function getNormal()
49
    {
50 54
        return $this->normal;
51
    }
52
53 54
    public function setNormal($normal)
54
    {
55 54
        $this->normal = $normal;
56 54
        return $this;
57
    }
58
59
    /**
60
     * Calcula o valor do imposto com base na aliquota e valor base
61
     */
62 16
    public function getValor($normalize = false)
63
    {
64 16
        if (!$normalize) {
65 16
            return ($this->getBase() * $this->getAliquota()) / 100.0 - $this->getNormal()->getValor();
66
        }
67 10
        $valor = $this->getValor();
68 10
        return Util::toCurrency($valor);
69
    }
70
71
    /**
72
     * Obtém o valor total do imposto
73
     */
74
    public function getTotal($normalize = false)
75
    {
76
        return $this->getNormal()->getValor($normalize);
77
    }
78
79 16
    public function toArray($recursive = false)
80
    {
81 16
        $cobranca = parent::toArray($recursive);
82 16
        if (!is_null($this->getNormal()) && $recursive) {
83 1
            $cobranca['normal'] = $this->getNormal()->toArray($recursive);
84 1
        } else {
85 15
            $cobranca['normal'] = $this->getNormal();
86
        }
87 16
        return $cobranca;
88
    }
89
90 54
    public function fromArray($cobranca = array())
91
    {
92 54
        if ($cobranca instanceof Cobranca) {
93 1
            $cobranca = $cobranca->toArray();
94 54
        } elseif (!is_array($cobranca)) {
95 1
            return $this;
96
        }
97 54
        parent::fromArray($cobranca);
98 54
        if (!isset($cobranca['normal']) || is_null($cobranca['normal'])) {
99 54
            $this->setNormal(new Normal());
100 54
        } else {
101 15
            $this->setNormal($cobranca['normal']);
102
        }
103 54
        return $this;
104
    }
105
106 12
    public function getNode($name = null)
0 ignored issues
show
Complexity introduced by
This operation has 300 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...
107
    {
108 12
        $element = $this->getNormal()->getNode(is_null($name)?'ICMS10':$name);
109 12
        if (is_null($this->getModalidade())) {
110 2
            return $element;
111
        }
112 10
        $dom = $element->ownerDocument;
113 10
        $parcial = parent::getNode(is_null($name)?'ICMS10':$name);
114 10
        if (is_null($this->getNormal()->getModalidade())) {
115 2
            return $parcial;
116
        }
117 8
        foreach ($parcial->childNodes as $node) {
118 8
            $node = $dom->importNode($node, true);
119 8
            $list = $element->getElementsByTagName($node->nodeName);
120 8
            if ($list->length == 1) {
121 8
                $element->replaceChild($node, $list->item(0));
122 8
            } else {
123 8
                $element->appendChild($node);
124
            }
125 8
        }
126 8
        return $element;
127
    }
128
129 6
    public function loadNode($element, $name = null)
130
    {
131 6
        $name = is_null($name)?'ICMS10':$name;
132 6
        if ($element->nodeName != $name) {
133
            $_fields = $element->getElementsByTagName($name);
134
            if ($_fields->length == 0) {
135
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
136
            }
137
            $element = $_fields->item(0);
138
        }
139 6
        $normal = $this->getNormal();
140 6
        if (is_null($normal)) {
141
            $normal = new Normal();
142
        }
143 6
        $this->setNormal($normal);
144 6
        $_fields = $element->getElementsByTagName('modBCST');
145 6
        if ($_fields->length == 0) {
146 1
            $normal->loadNode($element, $name);
147 1
            return $element;
148
        }
149 5
        $element = parent::loadNode($element, $name);
150 5
        $_fields = $element->getElementsByTagName('modBC');
151 5
        if ($_fields->length == 0) {
152 1
            return $element;
153
        }
154 4
        $normal->loadNode($element, $name);
155 4
        return $element;
156
    }
157
}
158