Completed
Push — master ( 15ee14...3f43fb )
by Francimar
17:55
created

Transporte::loadNode()   C

Complexity

Conditions 9
Paths 130

Size

Total Lines 57
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 46
cts 46
cp 1
rs 6.4931
c 0
b 0
f 0
cc 9
eloc 46
nc 130
nop 2
crap 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
use NFe\Entity\Transporte\Veiculo;
33
use NFe\Entity\Transporte\Tributo;
34
use NFe\Entity\Transporte\Transportador;
35
36
/**
37
 * Dados dos transportes da NF-e
38
 */
39
class Transporte implements Node
0 ignored issues
show
Complexity introduced by
This class has a complexity of 77 which exceeds the configured maximum of 50.

The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.

Some resources for further reading:

You can also find more detailed suggestions for refactoring in the “Code” section of your repository.

Loading history...
40
{
41
42
    /**
43
     * Modalidade do frete
44
     * 0- Por conta do emitente;
45
     * 1- Por conta do
46
     * destinatário/remetente;
47
     * 2- Por conta de terceiros;
48
     * 9- Sem frete (v2.0)
49
     */
50
    const FRETE_EMITENTE = 'emitente';
51
    const FRETE_DESTINATARIO = 'destinatario';
52
    const FRETE_TERCEIROS = 'terceiros';
53
    const FRETE_NENHUM = 'nenhum';
54
55
    private $frete;
56
    private $transportador;
57
    private $retencao;
58
    private $veiculo;
59
    private $reboque;
60
    private $vagao;
61
    private $balsa;
62
    private $volumes;
63
64 40
    public function __construct($transporte = array())
65
    {
66 40
        $this->fromArray($transporte);
67 40
    }
68
69
    /**
70
     * Modalidade do frete
71
     * 0- Por conta do emitente;
72
     * 1- Por conta do
73
     * destinatário/remetente;
74
     * 2- Por conta de terceiros;
75
     * 9- Sem frete (v2.0)
76
     */
77 35
    public function getFrete($normalize = false)
78
    {
79 35
        if (!$normalize) {
80 35
            return $this->frete;
81
        }
82 35
        switch ($this->frete) {
83 35
            case self::FRETE_EMITENTE:
84 2
                return '0';
85 33
            case self::FRETE_DESTINATARIO:
86 2
                return '1';
87 32
            case self::FRETE_TERCEIROS:
88 2
                return '2';
89 30
            case self::FRETE_NENHUM:
90 29
                return '9';
91
        }
92 1
        return $this->frete;
93
    }
94
95 40
    public function setFrete($frete)
96
    {
97
        switch ($frete) {
98 40
            case '0':
99 1
                $frete = self::FRETE_EMITENTE;
100 1
                break;
101 40
            case '1':
102 1
                $frete = self::FRETE_DESTINATARIO;
103 1
                break;
104 40
            case '2':
105 1
                $frete = self::FRETE_TERCEIROS;
106 1
                break;
107 40
            case '9':
108 26
                $frete = self::FRETE_NENHUM;
109 26
                break;
110
        }
111 40
        $this->frete = $frete;
112 40
        return $this;
113
    }
114
115
    /**
116
     * Dados da transportadora
117
     */
118 7
    public function getTransportador()
119
    {
120 7
        return $this->transportador;
121
    }
122
123 40
    public function setTransportador($transportador)
124
    {
125 40
        $this->transportador = $transportador;
126 40
        return $this;
127
    }
128
129
    /**
130
     * Dados da retenção  ICMS do Transporte
131
     */
132 7
    public function getRetencao()
133
    {
134 7
        return $this->retencao;
135
    }
136
137 40
    public function setRetencao($retencao)
138
    {
139 40
        $this->retencao = $retencao;
140 40
        return $this;
141
    }
142
143
    /**
144
     * Dados do veículo
145
     */
146 7
    public function getVeiculo()
147
    {
148 7
        return $this->veiculo;
149
    }
150
151 40
    public function setVeiculo($veiculo)
152
    {
153 40
        $this->veiculo = $veiculo;
154 40
        return $this;
155
    }
156
157
    /**
158
     * Dados do reboque/Dolly (v2.0)
159
     */
160 7
    public function getReboque()
161
    {
162 7
        return $this->reboque;
163
    }
164
165 40
    public function setReboque($reboque)
166
    {
167 40
        $this->reboque = $reboque;
168 40
        return $this;
169
    }
170
171
    /**
172
     * Identificação do vagão (v2.0)
173
     */
174 7
    public function getVagao($normalize = false)
175
    {
176 7
        if (!$normalize) {
177 7
            return $this->vagao;
178
        }
179 6
        return $this->vagao;
180
    }
181
182 40
    public function setVagao($vagao)
183
    {
184 40
        $this->vagao = $vagao;
185 40
        return $this;
186
    }
187
188
    /**
189
     * Identificação da balsa (v2.0)
190
     */
191 7
    public function getBalsa($normalize = false)
192
    {
193 7
        if (!$normalize) {
194 7
            return $this->balsa;
195
        }
196 6
        return $this->balsa;
197
    }
198
199 40
    public function setBalsa($balsa)
200
    {
201 40
        $this->balsa = $balsa;
202 40
        return $this;
203
    }
204
205
    /**
206
     * Dados dos volumes
207
     */
208 7
    public function getVolumes()
209
    {
210 7
        return $this->volumes;
211
    }
212
213 40
    public function setVolumes($volumes)
214
    {
215 40
        $this->volumes = $volumes;
216 40
        return $this;
217
    }
218
219 3
    public function addVolume($volume)
220
    {
221 3
        $this->volumes[] = $volume;
222 3
        return $this;
223
    }
224
225 4
    public function toArray($recursive = false)
0 ignored issues
show
Complexity introduced by
This operation has 243 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...
226
    {
227 4
        $transporte = array();
228 4
        $transporte['frete'] = $this->getFrete();
229 4
        if (!is_null($this->getTransportador()) && $recursive) {
230
            $transporte['transportador'] = $this->getTransportador()->toArray($recursive);
231
        } else {
232 4
            $transporte['transportador'] = $this->getTransportador();
233
        }
234 4
        if (!is_null($this->getRetencao()) && $recursive) {
235
            $transporte['retencao'] = $this->getRetencao()->toArray($recursive);
236
        } else {
237 4
            $transporte['retencao'] = $this->getRetencao();
238
        }
239 4
        if (!is_null($this->getVeiculo()) && $recursive) {
240
            $transporte['veiculo'] = $this->getVeiculo()->toArray($recursive);
241
        } else {
242 4
            $transporte['veiculo'] = $this->getVeiculo();
243
        }
244 4
        if (!is_null($this->getReboque()) && $recursive) {
245
            $transporte['reboque'] = $this->getReboque()->toArray($recursive);
246
        } else {
247 4
            $transporte['reboque'] = $this->getReboque();
248
        }
249 4
        $transporte['vagao'] = $this->getVagao();
250 4
        $transporte['balsa'] = $this->getBalsa();
251 4
        if ($recursive) {
252 1
            $volumes = array();
253 1
            $_volumes = $this->getVolumes();
254 1
            foreach ($_volumes as $_volume) {
255
                $volumes[] = $_volume->toArray($recursive);
256
            }
257 1
            $transporte['volumes'] = $volumes;
258
        } else {
259 3
            $transporte['volumes'] = $this->getVolumes();
260
        }
261 4
        return $transporte;
262
    }
263
264 40
    public function fromArray($transporte = array())
0 ignored issues
show
Complexity introduced by
This operation has 8748 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...
265
    {
266 40
        if ($transporte instanceof Transporte) {
267 3
            $transporte = $transporte->toArray();
268 40
        } elseif (!is_array($transporte)) {
269 3
            return $this;
270
        }
271 40
        if (!isset($transporte['frete']) || is_null($transporte['frete'])) {
272 40
            $this->setFrete(self::FRETE_NENHUM);
273
        } else {
274 3
            $this->setFrete($transporte['frete']);
275
        }
276 40
        if (!isset($transporte['transportador']) || is_null($transporte['transportador'])) {
277 40
            $this->setTransportador(new Transportador());
278
        } else {
279 3
            $this->setTransportador($transporte['transportador']);
280
        }
281 40
        if (!isset($transporte['retencao']) || is_null($transporte['retencao'])) {
282 40
            $this->setRetencao(new Tributo());
283
        } else {
284 3
            $this->setRetencao($transporte['retencao']);
285
        }
286 40
        if (!isset($transporte['veiculo']) || is_null($transporte['veiculo'])) {
287 40
            $this->setVeiculo(new Veiculo());
288
        } else {
289 3
            $this->setVeiculo($transporte['veiculo']);
290
        }
291 40
        if (!isset($transporte['reboque']) || is_null($transporte['reboque'])) {
292 40
            $this->setReboque(new Veiculo());
293
        } else {
294 3
            $this->setReboque($transporte['reboque']);
295
        }
296 40
        if (isset($transporte['vagao'])) {
297 3
            $this->setVagao($transporte['vagao']);
298
        } else {
299 40
            $this->setVagao(null);
300
        }
301 40
        if (isset($transporte['balsa'])) {
302 3
            $this->setBalsa($transporte['balsa']);
303
        } else {
304 40
            $this->setBalsa(null);
305
        }
306 40
        if (!isset($transporte['volumes']) || is_null($transporte['volumes'])) {
307 40
            $this->setVolumes(array());
308
        } else {
309 3
            $this->setVolumes($transporte['volumes']);
310
        }
311 40
        return $this;
312
    }
313
314 35
    public function getNode($name = null)
0 ignored issues
show
Complexity introduced by
This operation has 1920 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...
315
    {
316 35
        $dom = new \DOMDocument('1.0', 'UTF-8');
317 35
        $element = $dom->createElement(is_null($name)?'transp':$name);
318 35
        Util::appendNode($element, 'modFrete', $this->getFrete(true));
319 35
        if ($this->getFrete() == self::FRETE_NENHUM) {
320 29
            return $element;
321
        }
322 6
        if (!is_null($this->getTransportador())) {
323 6
            $transportador = $this->getTransportador()->getNode();
324 6
            $transportador = $dom->importNode($transportador, true);
325 6
            $element->appendChild($transportador);
326
        }
327 6
        if (!is_null($this->getRetencao())) {
328 6
            $retencao = $this->getRetencao()->getNode();
329 6
            $retencao = $dom->importNode($retencao, true);
330 6
            $element->appendChild($retencao);
331
        }
332 6
        if (!is_null($this->getVeiculo())) {
333 6
            $veiculo = $this->getVeiculo()->getNode('veicTransp');
334 6
            $veiculo = $dom->importNode($veiculo, true);
335 6
            $element->appendChild($veiculo);
336
        }
337 6
        if (!is_null($this->getReboque())) {
338 6
            $reboque = $this->getReboque()->getNode('reboque');
339 6
            $reboque = $dom->importNode($reboque, true);
340 6
            $element->appendChild($reboque);
341
        }
342 6
        if (!is_null($this->getVagao())) {
343 6
            Util::appendNode($element, 'vagao', $this->getVagao(true));
344
        }
345 6
        if (!is_null($this->getBalsa())) {
346 6
            Util::appendNode($element, 'balsa', $this->getBalsa(true));
347
        }
348 6
        if (!is_null($this->getVolumes())) {
349 6
            $_volumes = $this->getVolumes();
350 6
            foreach ($_volumes as $_volume) {
351 6
                $volume = $_volume->getNode();
352 6
                $volume = $dom->importNode($volume, true);
353 6
                $element->appendChild($volume);
354
            }
355
        }
356 6
        return $element;
357
    }
358
359 31
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 480 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...
360
    {
361 31
        $name = is_null($name)?'transp':$name;
362 31
        if ($element->nodeName != $name) {
363 2
            $_fields = $element->getElementsByTagName($name);
364 2
            if ($_fields->length == 0) {
365 1
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
366
            }
367 1
            $element = $_fields->item(0);
368
        }
369 30
        $this->setFrete(
370 30
            Util::loadNode(
371 30
                $element,
372 30
                'modFrete',
373 30
                'Tag "modFrete" do campo "Frete" não encontrada'
374
            )
375
        );
376 29
        $_fields = $element->getElementsByTagName('transporta');
377 29
        $transportador = null;
378 29
        if ($_fields->length > 0) {
379 3
            $transportador = new Transportador();
380 3
            $transportador->loadNode($_fields->item(0), 'transporta');
381
        }
382 29
        $this->setTransportador($transportador);
383 29
        $_fields = $element->getElementsByTagName('retTransp');
384 29
        $retencao = null;
385 29
        if ($_fields->length > 0) {
386 3
            $retencao = new Tributo();
387 3
            $retencao->loadNode($_fields->item(0), 'retTransp');
388
        }
389 29
        $this->setRetencao($retencao);
390 29
        $_fields = $element->getElementsByTagName('veicTransp');
391 29
        $veiculo = null;
392 29
        if ($_fields->length > 0) {
393 3
            $veiculo = new Veiculo();
394 3
            $veiculo->loadNode($_fields->item(0), 'veicTransp');
395
        }
396 29
        $this->setVeiculo($veiculo);
397 29
        $_fields = $element->getElementsByTagName('reboque');
398 29
        $reboque = null;
399 29
        if ($_fields->length > 0) {
400 3
            $reboque = new Veiculo();
401 3
            $reboque->loadNode($_fields->item(0), 'reboque');
402
        }
403 29
        $this->setReboque($reboque);
404 29
        $this->setVagao(Util::loadNode($element, 'vagao'));
405 29
        $this->setBalsa(Util::loadNode($element, 'balsa'));
406 29
        $volumes = array();
407 29
        $_fields = $element->getElementsByTagName('vol');
408 29
        foreach ($_fields as $_item) {
409 3
            $volume = new Volume();
410 3
            $volume->loadNode($_item, 'vol');
411 3
            $volumes[] = $volume;
412
        }
413 29
        $this->setVolumes($volumes);
414 29
        return $element;
415
    }
416
}
417