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

Base   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 110
ccs 33
cts 44
cp 0.75
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 5 1
A fromArray() 0 11 3
A exists() 0 8 1
A getNode() 0 9 2
B loadNode() 0 28 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\Fundo;
29
30
use NFe\Common\Util;
31
use NFe\Entity\Imposto;
32
33
/**
34
 * Valor e Percentual do imposto para o Fundo de Combate à Pobreza
35
 */
36
class Base extends Imposto
37
{
38
    /**
39
     * Grupo do imposto
40
     */
41
    const GRUPO_FCP = 'fcp';
42
    const GRUPO_FCPST = 'fcpst';
43
    const GRUPO_FCPSTRET = 'fcpstret';
44
45
    
46
    /**
47
     * Constroi uma instância de Base vazia
48
     * @param  array $base Array contendo dados do Base
49
     */
50 90
    public function __construct($base = [])
51
    {
52 90
        parent::__construct($base);
53 90
    }
54
55
    /**
56
     * Converte a instância da classe para um array de campos com valores
57
     * @return array Array contendo todos os campos e valores da instância
58
     */
59 2
    public function toArray($recursive = false)
60
    {
61 2
        $base = parent::toArray($recursive);
62 2
        return $base;
63
    }
64
65
    /**
66
     * Atribui os valores do array para a instância atual
67
     * @param mixed $base Array ou instância de Base, para copiar os valores
68
     * @return Base A própria instância da classe
69
     */
70 90
    public function fromArray($base = [])
71
    {
72 90
        if ($base instanceof Base) {
73
            $base = $base->toArray();
74 90
        } elseif (!is_array($base)) {
75
            return $this;
76
        }
77 90
        parent::fromArray($base);
78 90
        $this->setGrupo(self::GRUPO_FCP);
79 90
        return $this;
80
    }
81
82
    /**
83
     * Verifica se o elemento informado contém os dados dessa instância
84
     * @param DOMElement $element Nó que pode contér os dados dessa instância
85
     * @return boolean   True se contém os dados dessa instância ou false caso contrário
86
     */
87 10
    public function exists($element)
88
    {
89
        // se o primeiro campo obrigatório existir, significa que deve ter os outros campos
90 10
        return Util::nodeExists(
91 10
            $element,
92 10
            'pFCP'
93
        );
94
    }
95
96
    /**
97
     * Cria um nó XML do base de acordo com o leiaute da NFe
98
     * @param  string $name Nome do nó que será criado
99
     * @return DOMElement   Nó que contém todos os campos da classe
100
     */
101 2
    public function getNode($name = null)
102
    {
103 2
        $dom = new \DOMDocument('1.0', 'UTF-8');
104 2
        $element = $dom->createElement(is_null($name)?'FCP':$name);
105 2
        Util::appendNode($element, 'vBCFCP', $this->getBase(true));
106 2
        Util::appendNode($element, 'pFCP', $this->getAliquota(true));
107 2
        Util::appendNode($element, 'vFCP', $this->getValor(true));
108 2
        return $element;
109
    }
110
111
    /**
112
     * Carrega as informações do nó e preenche a instância da classe
113
     * @param  DOMElement $element Nó do xml com todos as tags dos campos
114
     * @param  string $name        Nome do nó que será carregado
115
     * @return DOMElement          Instância do nó que foi carregado
116
     */
117 1
    public function loadNode($element, $name = null)
118
    {
119 1
        $name = is_null($name)?'FCP':$name;
120 1
        if ($element->nodeName != $name) {
121
            $_fields = $element->getElementsByTagName($name);
122
            if ($_fields->length == 0) {
123
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
124
            }
125
            $element = $_fields->item(0);
126
        }
127 1
        if (is_null($this->getBase())) {
128
            $this->setBase(
129
                Util::loadNode(
130
                    $element,
131
                    'vBCFCP',
132
                    'Tag "vBCFCP" do campo "Base" não encontrada'
133
                )
134
            );
135
        }
136 1
        $this->setAliquota(
137 1
            Util::loadNode(
138 1
                $element,
139 1
                'pFCP',
140 1
                'Tag "pFCP" do campo "Aliquota" não encontrada'
141
            )
142
        );
143 1
        return $element;
144
    }
145
}
146