Passed
Push — master ( dc09fe...b3fb7f )
by Francimar
12:59
created

Destinatario::fromArray()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

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