Passed
Push — master ( c1d1cf...f48ccd )
by Francimar
03:58
created

Destinatario::loadNode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 14
cts 15
cp 0.9333
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
crap 4.0047
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;
29
30
use NFe\Common\Util;
31
32
/**
33
 * Cliente pessoa física ou jurídica que está comprando os produtos e irá
34
 * receber a nota fiscal
35
 */
36
class Destinatario extends Pessoa
37
{
38
    
39
    /**
40
     * Indicador da IE do destinatário:
41
     * 1 – Contribuinte ICMSpagamento à
42
     * vista;
43
     * 2 – Contribuinte isento de inscrição;
44
     * 9 – Não Contribuinte
45
     */
46
    const INDICADOR_PAGAMENTO = 'pagamento';
47
    const INDICADOR_ISENTO = 'isento';
48
    const INDICADOR_NENHUM = 'nenhum';
49
50
    private $cpf;
51
    private $email;
52
    private $indicador;
53
54 49
    public function __construct($destinatario = array())
55
    {
56 49
        parent::__construct($destinatario);
57 49
    }
58
59
    /**
60
     * Número identificador do destinatario
61
     */
62 23
    public function getID($normalize = false)
63
    {
64 23
        if (!is_null($this->getCNPJ())) {
65
            return $this->getCNPJ($normalize);
66
        }
67 23
        return $this->getCPF($normalize);
68
    }
69
70
    /**
71
     * Nome do destinatário
72
     */
73 32
    public function getNome($normalize = false)
74
    {
75 32
        return $this->getRazaoSocial($normalize);
76
    }
77
78 49
    public function setNome($nome)
79
    {
80 49
        return $this->setRazaoSocial($nome);
81
    }
82
83
    /**
84
     * CPF do cliente
85
     */
86 31
    public function getCPF($normalize = false)
87
    {
88 31
        if (!$normalize) {
89 7
            return $this->cpf;
90
        }
91 29
        return $this->cpf;
92
    }
93
94 49
    public function setCPF($cpf)
95
    {
96 49
        $this->cpf = $cpf;
97 49
        return $this;
98
    }
99
100
    /**
101
     * Informar o e-mail do destinatário. O campo pode ser utilizado para
102
     * informar o e-mail de recepção da NF-e indicada pelo destinatário
103
     */
104 32
    public function getEmail($normalize = false)
105
    {
106 32
        if (!$normalize) {
107 32
            return $this->email;
108
        }
109 29
        return $this->email;
110
    }
111
112 49
    public function setEmail($email)
113
    {
114 49
        $this->email = $email;
115 49
        return $this;
116
    }
117
118
    /**
119
     * Indicador da IE do destinatário:
120
     * 1 – Contribuinte ICMSpagamento à
121
     * vista;
122
     * 2 – Contribuinte isento de inscrição;
123
     * 9 – Não Contribuinte
124
     */
125 32
    public function getIndicador($normalize = false)
126
    {
127 32
        if (!$normalize) {
128 7
            return $this->indicador;
129
        }
130 31
        switch ($this->indicador) {
131 31
            case self::INDICADOR_PAGAMENTO:
132
                return '1';
133 31
            case self::INDICADOR_ISENTO:
134
                return '2';
135 31
            case self::INDICADOR_NENHUM:
136 5
                return '9';
137 26
        }
138 26
        return $this->indicador;
139
    }
140
141 49
    public function setIndicador($indicador)
142
    {
143 49
        $this->indicador = $indicador;
144 49
        return $this;
145
    }
146
147 7
    public function toArray($recursive = false)
148
    {
149 7
        $destinatario = parent::toArray($recursive);
150 7
        $destinatario['nome'] = $this->getNome();
151 7
        $destinatario['cpf'] = $this->getCPF();
152 7
        $destinatario['email'] = $this->getEmail();
153 7
        $destinatario['indicador'] = $this->getIndicador();
154 7
        return $destinatario;
155
    }
156
157 49
    public function fromArray($destinatario = array())
158
    {
159 49
        if ($destinatario instanceof Destinatario) {
160 5
            $destinatario = $destinatario->toArray();
161 49
        } elseif (!is_array($destinatario)) {
162 5
            return $this;
163
        }
164 49
        parent::fromArray($destinatario);
165 49
        if (isset($destinatario['nome'])) {
166 5
            $this->setNome($destinatario['nome']);
167 5
        } else {
168 49
            $this->setNome(null);
169
        }
170 49
        if (isset($destinatario['cpf'])) {
171 4
            $this->setCPF($destinatario['cpf']);
172 4
        } else {
173 49
            $this->setCPF(null);
174
        }
175 49
        if (isset($destinatario['email'])) {
176 4
            $this->setEmail($destinatario['email']);
177 4
        } else {
178 49
            $this->setEmail(null);
179
        }
180 49
        if (!isset($destinatario['indicador']) || is_null($destinatario['indicador'])) {
181 49
            $this->setIndicador(self::INDICADOR_NENHUM);
182 49
        } else {
183 6
            $this->setIndicador($destinatario['indicador']);
184
        }
185 49
        return $this;
186
    }
187
188 31
    public function getNode($name = null)
0 ignored issues
show
Complexity introduced by
This operation has 360 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...
189
    {
190 31
        $dom = new \DOMDocument('1.0', 'UTF-8');
191 31
        $element = $dom->createElement(is_null($name)?'dest':$name);
192 31
        if (!is_null($this->getCNPJ())) {
193 2
            Util::appendNode($element, 'CNPJ', $this->getCNPJ(true));
194 2
        } else {
195 29
            Util::appendNode($element, 'CPF', $this->getCPF(true));
196
        }
197 31
        if (!is_null($this->getNome())) {
198 29
            Util::appendNode($element, 'xNome', $this->getNome(true));
199 29
        }
200 31
        if (!is_null($this->getEndereco())) {
201 29
            $endereco = $this->getEndereco()->getNode('enderDest');
202 29
            $endereco = $dom->importNode($endereco, true);
203 29
            if (!is_null($this->getTelefone())) {
204 27
                Util::appendNode($endereco, 'fone', $this->getTelefone(true));
205 27
            }
206 29
            $element->appendChild($endereco);
207 29
        }
208 31
        Util::appendNode($element, 'indIEDest', $this->getIndicador(true));
209 31
        if (!is_null($this->getCNPJ()) && !is_null($this->getIE())) {
210 2
            Util::appendNode($element, 'IE', $this->getIE(true));
211 2
        }
212 31
        if (!is_null($this->getEmail())) {
213 29
            Util::appendNode($element, 'email', $this->getEmail(true));
214 29
        }
215 31
        return $element;
216
    }
217
218 26
    public function loadNode($element, $name = null)
219
    {
220 26
        $name = is_null($name)?'dest':$name;
221 26
        $element = parent::loadNode($element, $name);
222 26
        $cpf = Util::loadNode($element, 'CPF');
223 26
        if (is_null($cpf) && is_null($this->getCNPJ())) {
224
            throw new \Exception('Tag "CPF" não encontrada no Destinatario', 404);
225
        }
226 26
        $this->setCPF($cpf);
227 26
        $this->setEmail(Util::loadNode($element, 'email'));
228 26
        $this->setIndicador(
229 26
            Util::loadNode(
230 26
                $element,
231 26
                'indIEDest',
232
                'Tag "indIEDest" não encontrada no Destinatario'
233 26
            )
234 26
        );
235 26
        return $element;
236
    }
237
}
238