Passed
Push — master ( 84520a...e53a66 )
by Francimar
03:44
created

Destinatario::loadNode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 14
cp 0.9286
rs 9.2
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 2
crap 4.0058
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 51
    public function __construct($destinatario = array())
55
    {
56 51
        parent::__construct($destinatario);
57 51
    }
58
59
    /**
60
     * Número identificador do destinatario
61
     */
62 25
    public function getID($normalize = false)
63
    {
64 25
        if (!is_null($this->getCNPJ())) {
65
            return $this->getCNPJ($normalize);
66
        }
67 25
        return $this->getCPF($normalize);
68
    }
69
70
    /**
71
     * Nome do destinatário
72
     */
73 34
    public function getNome($normalize = false)
74
    {
75 34
        return $this->getRazaoSocial($normalize);
76
    }
77
78 51
    public function setNome($nome)
79
    {
80 51
        return $this->setRazaoSocial($nome);
81
    }
82
83
    /**
84
     * CPF do cliente
85
     */
86 33
    public function getCPF($normalize = false)
87
    {
88 33
        if (!$normalize) {
89 7
            return $this->cpf;
90
        }
91 31
        return $this->cpf;
92
    }
93
94 51
    public function setCPF($cpf)
95
    {
96 51
        $this->cpf = $cpf;
97 51
        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 34
    public function getEmail($normalize = false)
105
    {
106 34
        if (!$normalize) {
107 34
            return $this->email;
108
        }
109 31
        return $this->email;
110
    }
111
112 51
    public function setEmail($email)
113
    {
114 51
        $this->email = $email;
115 51
        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 34
    public function getIndicador($normalize = false)
126
    {
127 34
        if (!$normalize) {
128 7
            return $this->indicador;
129
        }
130 33
        switch ($this->indicador) {
131 33
            case self::INDICADOR_PAGAMENTO:
132
                return '1';
133 33
            case self::INDICADOR_ISENTO:
134
                return '2';
135 33
            case self::INDICADOR_NENHUM:
136 5
                return '9';
137
        }
138 28
        return $this->indicador;
139
    }
140
141 51
    public function setIndicador($indicador)
142
    {
143 51
        $this->indicador = $indicador;
144 51
        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 51
    public function fromArray($destinatario = array())
158
    {
159 51
        if ($destinatario instanceof Destinatario) {
160 5
            $destinatario = $destinatario->toArray();
161 51
        } elseif (!is_array($destinatario)) {
162 5
            return $this;
163
        }
164 51
        parent::fromArray($destinatario);
165 51
        if (isset($destinatario['nome'])) {
166 5
            $this->setNome($destinatario['nome']);
167
        } else {
168 51
            $this->setNome(null);
169
        }
170 51
        if (isset($destinatario['cpf'])) {
171 4
            $this->setCPF($destinatario['cpf']);
172
        } else {
173 51
            $this->setCPF(null);
174
        }
175 51
        if (isset($destinatario['email'])) {
176 4
            $this->setEmail($destinatario['email']);
177
        } else {
178 51
            $this->setEmail(null);
179
        }
180 51
        if (!isset($destinatario['indicador']) || is_null($destinatario['indicador'])) {
181 51
            $this->setIndicador(self::INDICADOR_NENHUM);
182
        } else {
183 6
            $this->setIndicador($destinatario['indicador']);
184
        }
185 51
        return $this;
186
    }
187
188 33
    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 33
        $dom = new \DOMDocument('1.0', 'UTF-8');
191 33
        $element = $dom->createElement(is_null($name)?'dest':$name);
192 33
        if (!is_null($this->getCNPJ())) {
193 2
            Util::appendNode($element, 'CNPJ', $this->getCNPJ(true));
194
        } else {
195 31
            Util::appendNode($element, 'CPF', $this->getCPF(true));
196
        }
197 33
        if (!is_null($this->getNome())) {
198 31
            Util::appendNode($element, 'xNome', $this->getNome(true));
199
        }
200 33
        if (!is_null($this->getEndereco())) {
201 31
            $endereco = $this->getEndereco()->getNode('enderDest');
202 31
            $endereco = $dom->importNode($endereco, true);
203 31
            if (!is_null($this->getTelefone())) {
204 29
                Util::appendNode($endereco, 'fone', $this->getTelefone(true));
205
            }
206 31
            $element->appendChild($endereco);
207
        }
208 33
        Util::appendNode($element, 'indIEDest', $this->getIndicador(true));
209 33
        if (!is_null($this->getCNPJ()) && !is_null($this->getIE())) {
210 2
            Util::appendNode($element, 'IE', $this->getIE(true));
211
        }
212 33
        if (!is_null($this->getEmail())) {
213 31
            Util::appendNode($element, 'email', $this->getEmail(true));
214
        }
215 33
        return $element;
216
    }
217
218 28
    public function loadNode($element, $name = null)
219
    {
220 28
        $name = is_null($name)?'dest':$name;
221 28
        $element = parent::loadNode($element, $name);
222 28
        $cpf = Util::loadNode($element, 'CPF');
223 28
        if (is_null($cpf) && is_null($this->getCNPJ())) {
224
            throw new \Exception('Tag "CPF" não encontrada no Destinatario', 404);
225
        }
226 28
        $this->setCPF($cpf);
227 28
        $this->setEmail(Util::loadNode($element, 'email'));
228 28
        $this->setIndicador(
229 28
            Util::loadNode(
230 28
                $element,
231 28
                'indIEDest',
232 28
                'Tag "indIEDest" não encontrada no Destinatario'
233
            )
234
        );
235 28
        return $element;
236
    }
237
}
238