Passed
Push — master ( 22a4bb...4eeb09 )
by Francimar
05:29
created

Aliquota::getTributacao()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 4
nop 1
crap 4.0218
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\PIS;
29
30
use NFe\Common\Util;
31
use NFe\Entity\Imposto;
32
33
class Aliquota extends Imposto
34
{
35
36
    const TRIBUTACAO_NORMAL = 'normal';
37
    const TRIBUTACAO_DIFERENCIADA = 'diferenciada';
38
39 10
    public function __construct($pis = array())
40
    {
41 10
        parent::__construct($pis);
42 10
        $this->setGrupo(self::GRUPO_PIS);
43 10
    }
44
45 10
    public function getTributacao($normalize = false)
46
    {
47 10
        if (!$normalize) {
48 10
            return parent::getTributacao();
49
        }
50 9
        switch (parent::getTributacao()) {
51 9
            case self::TRIBUTACAO_NORMAL:
52 5
                return '01';
53 4
            case self::TRIBUTACAO_DIFERENCIADA:
54
                return '02';
55
        }
56 4
        return parent::getTributacao($normalize);
57
    }
58
59 4
    public function toArray($recursive = false)
60
    {
61 4
        $pis = parent::toArray($recursive);
62 4
        return $pis;
63
    }
64
65 10
    public function fromArray($pis = array())
66
    {
67 10
        if ($pis instanceof Aliquota) {
68 3
            $pis = $pis->toArray();
69 10
        } elseif (!is_array($pis)) {
70 3
            return $this;
71
        }
72 10
        parent::fromArray($pis);
73 10
        if (is_null($this->getTributacao())) {
74 10
            $this->setTributacao(self::TRIBUTACAO_NORMAL);
75
        }
76 10
        return $this;
77
    }
78
79 9
    public function getNode($name = null)
80
    {
81 9
        $dom = new \DOMDocument('1.0', 'UTF-8');
82 9
        $element = $dom->createElement(is_null($name)?'PISAliq':$name);
83 9
        Util::appendNode($element, 'CST', $this->getTributacao(true));
84 9
        Util::appendNode($element, 'vBC', $this->getBase(true));
85 9
        Util::appendNode($element, 'pPIS', $this->getAliquota(true));
86 9
        Util::appendNode($element, 'vPIS', $this->getValor(true));
87 9
        return $element;
88
    }
89
90 4
    public function loadNode($element, $name = null)
91
    {
92 4
        $name = is_null($name)?'PISAliq':$name;
93 4
        if ($element->tagName != $name) {
94
            $_fields = $element->getElementsByTagName($name);
95
            if ($_fields->length == 0) {
96
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
97
            }
98
            $element = $_fields->item(0);
99
        }
100 4
        $this->setTributacao(
101 4
            Util::loadNode(
102
                $element,
103 4
                'CST',
104 4
                'Tag "CST" do campo "Tributacao" não encontrada'
105
            )
106
        );
107 4
        $this->setBase(
108 4
            Util::loadNode(
109
                $element,
110 4
                'vBC',
111 4
                'Tag "vBC" do campo "Base" não encontrada'
112
            )
113
        );
114 4
        $this->setAliquota(
115 4
            Util::loadNode(
116
                $element,
117 4
                'pPIS',
118 4
                'Tag "pPIS" do campo "Aliquota" não encontrada'
119
            )
120
        );
121 4
        return $element;
122
    }
123
}
124