Passed
Push — master ( 43458c...ac6f10 )
by Francimar
03:09
created

Pessoa::getID()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\Node;
31
use NFe\Common\Util;
32
33
/**
34
 * Classe base para preenchimento de informações de pessoas físicas e
35
 * empresas
36
 */
37
abstract class Pessoa implements Node
38
{
39
40
    private $razao_social;
41
    private $cnpj;
42
    private $ie;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ie. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
43
    private $im;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $im. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
44
    private $endereco;
45
    private $telefone;
46
47 107
    public function __construct($pessoa = [])
48
    {
49 107
        $this->fromArray($pessoa);
50 107
    }
51
52
    /**
53
     * Número identificador da pessoa
54
     */
55
    public function getID($normalize = false)
56
    {
57
        return $this->getCNPJ($normalize);
58
    }
59
60
    /**
61
     * Razão Social ou Nome
62
     */
63 52
    public function getRazaoSocial($normalize = false)
64
    {
65 52
        if (!$normalize) {
66 45
            return $this->razao_social;
67
        }
68 48
        return $this->razao_social;
69
    }
70
71 107
    public function setRazaoSocial($razao_social)
72
    {
73 107
        $this->razao_social = $razao_social;
74 107
        return $this;
75
    }
76
77
    /**
78
     * Identificador da pessoa na receita
79
     */
80 60
    public function getCNPJ($normalize = false)
81
    {
82 60
        if (!$normalize) {
83 59
            return $this->cnpj;
84
        }
85 46
        return $this->cnpj;
86
    }
87
88 107
    public function setCNPJ($cnpj)
89
    {
90 107
        $this->cnpj = $cnpj;
91 107
        return $this;
92
    }
93
94
    /**
95
     * Inscrição Estadual
96
     */
97 50
    public function getIE($normalize = false)
98
    {
99 50
        if (!$normalize) {
100 18
            return $this->ie;
101
        }
102 46
        return $this->ie;
103
    }
104
105 107
    public function setIE($ie)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ie. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
106
    {
107 107
        $this->ie = $ie;
108 107
        return $this;
109
    }
110
111
    /**
112
     * Inscrição Municipal
113
     */
114 43
    public function getIM($normalize = false)
115
    {
116 43
        if (!$normalize) {
117 43
            return $this->im;
118
        }
119 34
        return $this->im;
120
    }
121
122 107
    public function setIM($im)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $im. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
123
    {
124 107
        $this->im = $im;
125 107
        return $this;
126
    }
127
128
    /**
129
     * Dados do endereço
130
     */
131 110
    public function getEndereco()
132
    {
133 110
        return $this->endereco;
134
    }
135
136 107
    public function setEndereco($endereco)
137
    {
138 107
        $this->endereco = $endereco;
139 107
        return $this;
140
    }
141
142 45
    public function getTelefone($normalize = false)
143
    {
144 45
        if (!$normalize) {
145 45
            return $this->telefone;
146
        }
147 36
        return $this->telefone;
148
    }
149
150 107
    public function setTelefone($telefone)
151
    {
152 107
        $this->telefone = $telefone;
153 107
        return $this;
154
    }
155
156 17
    public function toArray($recursive = false)
157
    {
158 17
        $pessoa = [];
159 17
        $pessoa['razao_social'] = $this->getRazaoSocial();
160 17
        $pessoa['cnpj'] = $this->getCNPJ();
161 17
        $pessoa['ie'] = $this->getIE();
162 17
        $pessoa['im'] = $this->getIM();
163 17
        if (!is_null($this->getEndereco()) && $recursive) {
164 1
            $pessoa['endereco'] = $this->getEndereco()->toArray($recursive);
165
        } else {
166 16
            $pessoa['endereco'] = $this->getEndereco();
167
        }
168 17
        $pessoa['telefone'] = $this->getTelefone();
169 17
        return $pessoa;
170
    }
171
172 107
    public function fromArray($pessoa = [])
0 ignored issues
show
Complexity introduced by
This operation has 288 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...
173
    {
174 107
        if ($pessoa instanceof Pessoa) {
175
            $pessoa = $pessoa->toArray();
176 107
        } elseif (!is_array($pessoa)) {
177
            return $this;
178
        }
179 107
        if (isset($pessoa['razao_social'])) {
180 14
            $this->setRazaoSocial($pessoa['razao_social']);
181
        } else {
182 107
            $this->setRazaoSocial(null);
183
        }
184 107
        if (isset($pessoa['cnpj'])) {
185 13
            $this->setCNPJ($pessoa['cnpj']);
186
        } else {
187 107
            $this->setCNPJ(null);
188
        }
189 107
        if (isset($pessoa['ie'])) {
190 13
            $this->setIE($pessoa['ie']);
191
        } else {
192 107
            $this->setIE(null);
193
        }
194 107
        if (isset($pessoa['im'])) {
195 8
            $this->setIM($pessoa['im']);
196
        } else {
197 107
            $this->setIM(null);
198
        }
199 107
        if (!array_key_exists('endereco', $pessoa)) {
200 107
            $this->setEndereco(new Endereco());
201 16
        } elseif (isset($pessoa['endereco'])) {
202 15
            $this->setEndereco(new Endereco($pessoa['endereco']));
203
        } else {
204 1
            $this->setEndereco($pessoa['endereco']);
205
        }
206 107
        if (isset($pessoa['telefone'])) {
207 9
            $this->setTelefone($pessoa['telefone']);
208
        } else {
209 107
            $this->setTelefone(null);
210
        }
211 107
        return $this;
212
    }
213
214 31
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 4860 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...
215
    {
216 31
        $name = is_null($name)?'emit':$name;
217 31
        if ($element->nodeName != $name) {
218
            $_fields = $element->getElementsByTagName($name);
219
            if ($_fields->length == 0) {
220
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
221
            }
222
            $element = $_fields->item(0);
223
        }
224 31
        $razao_social = Util::loadNode($element, 'xNome');
225 31
        if (is_null($razao_social) && $this instanceof Emitente) {
226
            throw new \Exception('Tag "xNome" do campo "RazaoSocial" não encontrada', 404);
227
        }
228 31
        $this->setRazaoSocial($razao_social);
229 31
        $cnpj = Util::loadNode($element, 'CNPJ');
230 31
        if (is_null($cnpj) && $this instanceof Emitente) {
231
            throw new \Exception('Tag "CNPJ" do campo "CNPJ" não encontrada', 404);
232
        }
233 31
        $this->setCNPJ($cnpj);
234 31
        $ie = Util::loadNode($element, 'IE');
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ie. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
235 31
        if (is_null($ie) && $this instanceof Emitente) {
236
            throw new \Exception('Tag "IE" do campo "IE" não encontrada', 404);
237
        }
238 31
        $this->setIE($ie);
239 31
        $this->setIM(Util::loadNode($element, 'IM'));
240 31
        if ($this instanceof Emitente) {
241 28
            $tag_ender = 'enderEmit';
242
        } else {
243 30
            $tag_ender = 'enderDest';
244
        }
245 31
        $endereco = null;
246 31
        $_fields = $element->getElementsByTagName($tag_ender);
247 31
        if ($_fields->length > 0) {
248 30
            $endereco = new Endereco();
249 30
            $endereco->loadNode($_fields->item(0), $tag_ender);
250 1
        } elseif ($this instanceof Emitente) {
251
            throw new \Exception('Tag "'.$tag_ender.'" do objeto "Endereco" não encontrada', 404);
252
        }
253 31
        $this->setEndereco($endereco);
254 31
        $telefone = null;
255 31
        if ($_fields->length > 0) {
256 30
            $telefone = Util::loadNode($_fields->item(0), 'fone');
257
        }
258 31
        $this->setTelefone($telefone);
259 31
        return $element;
260
    }
261
}
262