Test Failed
Push — master ( e53a66...43458c )
by Francimar
16:26
created

Pagamento::fromArray()   F

Complexity

Conditions 10
Paths 257

Size

Total Lines 44
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 10.005

Importance

Changes 0
Metric Value
dl 0
loc 44
ccs 26
cts 27
cp 0.963
rs 3.1304
c 0
b 0
f 0
cc 10
eloc 34
nc 257
nop 1
crap 10.005

How to fix   Complexity   

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
33
class Pagamento implements Node
0 ignored issues
show
Complexity introduced by
This class has a complexity of 94 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...
34
{
35
    /**
36
     * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à
37
     * prazo.
38
     */
39
    const INDICADOR_AVISTA = 'avista';
40
    const INDICADOR_APRAZO = 'aprazo';
41
42
    /**
43
     * Forma de Pagamento:01-Dinheiro;02-Cheque;03-Cartão de Crédito;04-Cartão
44
     * de Débito;05-Crédito Loja;10-Vale Alimentação;11-Vale Refeição;12-Vale
45
     * Presente;13-Vale Combustível;99 - Outros
46
     */
47
    const FORMA_DINHEIRO = 'dinheiro';
48
    const FORMA_CHEQUE = 'cheque';
49
    const FORMA_CREDITO = 'credito';
50
    const FORMA_DEBITO = 'debito';
51
    const FORMA_CREDIARIO = 'crediario';
52
    const FORMA_ALIMENTACAO = 'alimentacao';
53
    const FORMA_REFEICAO = 'refeicao';
54
    const FORMA_PRESENTE = 'presente';
55
    const FORMA_COMBUSTIVEL = 'combustivel';
56
    const FORMA_OUTROS = 'outros';
57
58
    /**
59
     * Bandeira da operadora de cartão de crédito/débito:01–Visa;
60
     * 02–Mastercard; 03–American Express; 04–Sorocred; 99–Outros
61
     */
62
    const BANDEIRA_VISA = 'visa';
63
    const BANDEIRA_MASTERCARD = 'mastercard';
64
    const BANDEIRA_AMEX = 'amex';
65
    const BANDEIRA_SOROCRED = 'sorocred';
66
    const BANDEIRA_OUTROS = 'outros';
67
68
    /**
69
     * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à
70
     * prazo.
71
     */
72
    private $indicador;
73
    private $forma;
74
    private $valor;
75
    private $integrado;
76
    private $credenciadora;
77
    private $autorizacao;
78
    private $bandeira;
79
80 40
    public function __construct($pagamento = [])
81
    {
82 40
        $this->fromArray($pagamento);
83 40
    }
84
85
    /**
86
     * Indicador da forma de pagamento: 0 – pagamento à vista; 1 – pagamento à
87
     * prazo.
88
     * @param boolean $normalize informa se o indicador deve estar no formato do XML
89
     * @return mixed indicador da Nota
90
     */
91 38
    public function getIndicador($normalize = false)
92
    {
93 38
        if (!$normalize) {
94 38
            return $this->indicador;
95
        }
96
        switch ($this->indicador) {
97
            case self::INDICADOR_AVISTA:
98
                return '0';
99
            case self::INDICADOR_APRAZO:
100
                return '1';
101
        }
102
        return $this->indicador;
103
    }
104
105
    /**
106
     * Altera o valor do Indicador para o informado no parâmetro
107
     * @param mixed $indicador novo valor para Indicador
108
     * @return Nota A própria instância da classe
109
     */
110 40
    public function setIndicador($indicador)
111
    {
112
        switch ($indicador) {
113 40
            case '0':
114
                $indicador = self::INDICADOR_AVISTA;
115
                break;
116 40
            case '1':
117
                $indicador = self::INDICADOR_APRAZO;
118
                break;
119
        }
120 40
        $this->indicador = $indicador;
121 40
        return $this;
122
    }
123
124
    /**
125
     * Forma de Pagamento:01-Dinheiro;02-Cheque;03-Cartão de Crédito;04-Cartão
126
     * de Débito;05-Crédito Loja;10-Vale Alimentação;11-Vale Refeição;12-Vale
127
     * Presente;13-Vale Combustível;99 - Outros
128
     */
129 38
    public function getForma($normalize = false)
130
    {
131 38
        if (!$normalize) {
132 38
            return $this->forma;
133
        }
134 37
        switch ($this->forma) {
135 37
            case self::FORMA_DINHEIRO:
136 33
                return '01';
137 35
            case self::FORMA_CHEQUE:
138
                return '02';
139 35
            case self::FORMA_CREDITO:
140 33
                return '03';
141 2
            case self::FORMA_DEBITO:
142 2
                return '04';
143
            case self::FORMA_CREDIARIO:
144
                return '05';
145
            case self::FORMA_ALIMENTACAO:
146
                return '10';
147
            case self::FORMA_REFEICAO:
148
                return '11';
149
            case self::FORMA_PRESENTE:
150
                return '12';
151
            case self::FORMA_COMBUSTIVEL:
152
                return '13';
153
            case self::FORMA_OUTROS:
154
                return '99';
155
        }
156
        return $this->forma;
157
    }
158
159 40
    public function setForma($forma)
160
    {
161
        switch ($forma) {
162 40
            case '01':
163 27
                $forma = self::FORMA_DINHEIRO;
164 27
                break;
165 40
            case '02':
166
                $forma = self::FORMA_CHEQUE;
167
                break;
168 40
            case '03':
169 27
                $forma = self::FORMA_CREDITO;
170 27
                break;
171 40
            case '04':
172 1
                $forma = self::FORMA_DEBITO;
173 1
                break;
174 40
            case '05':
175
                $forma = self::FORMA_CREDIARIO;
176
                break;
177 40
            case '10':
178
                $forma = self::FORMA_ALIMENTACAO;
179
                break;
180 40
            case '11':
181
                $forma = self::FORMA_REFEICAO;
182
                break;
183 40
            case '12':
184
                $forma = self::FORMA_PRESENTE;
185
                break;
186 40
            case '13':
187
                $forma = self::FORMA_COMBUSTIVEL;
188
                break;
189 40
            case '99':
190
                $forma = self::FORMA_OUTROS;
191
                break;
192
        }
193 40
        $this->forma = $forma;
194 40
        return $this;
195
    }
196
197
    /**
198
     * Valor do Pagamento
199
     */
200 39
    public function getValor($normalize = false)
201
    {
202 39
        if (!$normalize) {
203 39
            return $this->valor;
204
        }
205 39
        return Util::toCurrency($this->valor);
206
    }
207
208 40
    public function setValor($valor)
209
    {
210 40
        $valor = floatval($valor);
211 40
        $this->valor = $valor;
212 40
        return $this;
213
    }
214
215
    /**
216
     * Tipo de Integração do processo de pagamento com o sistema de automação
217
     * da empresa/1=Pagamento integrado com o sistema de automação da empresa
218
     * Ex. equipamento TEF , Comercio Eletronico 2=Pagamento não integrado com
219
     * o sistema de automação da empresa Ex: equipamento POS
220
     */
221 37
    public function getIntegrado($normalize = false)
222
    {
223 37
        if (!$normalize) {
224 9
            return $this->integrado;
225
        }
226 35
        return $this->isIntegrado()?'1':'2';
227
    }
228
229
    /**
230
     * Tipo de Integração do processo de pagamento com o sistema de automação
231
     * da empresa/1=Pagamento integrado com o sistema de automação da empresa
232
     * Ex. equipamento TEF , Comercio Eletronico 2=Pagamento não integrado com
233
     * o sistema de automação da empresa Ex: equipamento POS
234
     */
235 35
    public function isIntegrado()
236
    {
237 35
        return $this->integrado == 'Y';
238
    }
239
240 40
    public function setIntegrado($integrado)
241
    {
242 40
        if (is_bool($integrado)) {
243
            $integrado = $integrado ? 'Y': 'N';
244
        }
245 40
        $this->integrado = in_array($integrado, ['Y', '1']) ? 'Y' : 'N';
246 40
        return $this;
247
    }
248
249
    /**
250
     * CNPJ da credenciadora de cartão de crédito/débito
251
     */
252 37
    public function getCredenciadora($normalize = false)
253
    {
254 37
        if (!$normalize) {
255 36
            return $this->credenciadora;
256
        }
257 2
        return $this->credenciadora;
258
    }
259
260 40
    public function setCredenciadora($credenciadora)
261
    {
262 40
        $this->credenciadora = $credenciadora;
263 40
        return $this;
264
    }
265
266
    /**
267
     * Número de autorização da operação cartão de crédito/débito
268
     */
269 10
    public function getAutorizacao($normalize = false)
270
    {
271 10
        if (!$normalize) {
272 9
            return $this->autorizacao;
273
        }
274 2
        return $this->autorizacao;
275
    }
276
277 40
    public function setAutorizacao($autorizacao)
278
    {
279 40
        $this->autorizacao = $autorizacao;
280 40
        return $this;
281
    }
282
283
    /**
284
     * Bandeira da operadora de cartão de crédito/débito:01–Visa;
285
     * 02–Mastercard; 03–American Express; 04–Sorocred; 99–Outros
286
     */
287 37
    public function getBandeira($normalize = false)
288
    {
289 37
        if (!$normalize) {
290 9
            return $this->bandeira;
291
        }
292 35
        switch ($this->bandeira) {
293 35
            case self::BANDEIRA_VISA:
294 2
                return '01';
295 33
            case self::BANDEIRA_MASTERCARD:
296 33
                return '02';
297
            case self::BANDEIRA_AMEX:
298
                return '03';
299
            case self::BANDEIRA_SOROCRED:
300
                return '04';
301
            case self::BANDEIRA_OUTROS:
302
                return '99';
303
        }
304
        return $this->bandeira;
305
    }
306
307 40
    public function setBandeira($bandeira)
308
    {
309
        switch ($bandeira) {
310 40
            case '01':
311 1
                $bandeira = self::BANDEIRA_VISA;
312 1
                break;
313 40
            case '02':
314 27
                $bandeira = self::BANDEIRA_MASTERCARD;
315 27
                break;
316 40
            case '03':
317
                $bandeira = self::BANDEIRA_AMEX;
318
                break;
319 40
            case '04':
320
                $bandeira = self::BANDEIRA_SOROCRED;
321
                break;
322 40
            case '99':
323
                $bandeira = self::BANDEIRA_OUTROS;
324
                break;
325
        }
326 40
        $this->bandeira = $bandeira;
327 40
        return $this;
328
    }
329
330
    /**
331
     * Informa se o pagamento é em cartão
332
     */
333 37
    public function isCartao()
334
    {
335 37
        return in_array($this->getForma(), [self::FORMA_CREDITO, self::FORMA_DEBITO]);
336
    }
337
338 9
    public function toArray($recursive = false)
0 ignored issues
show
Unused Code introduced by
The parameter $recursive is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
339
    {
340 9
        $pagamento = [];
341 9
        $pagamento['indicador'] = $this->getIndicador();
342 9
        $pagamento['forma'] = $this->getForma();
343 9
        $pagamento['valor'] = $this->getValor();
344 9
        $pagamento['integrado'] = $this->getIntegrado();
345 9
        $pagamento['credenciadora'] = $this->getCredenciadora();
346 9
        $pagamento['autorizacao'] = $this->getAutorizacao();
347 9
        $pagamento['bandeira'] = $this->getBandeira();
348 9
        return $pagamento;
349
    }
350
351 40
    public function fromArray($pagamento = [])
0 ignored issues
show
Complexity introduced by
This operation has 384 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...
352
    {
353 40
        if ($pagamento instanceof Pagamento) {
354 9
            $pagamento = $pagamento->toArray();
355 40
        } elseif (!is_array($pagamento)) {
356 9
            return $this;
357
        }
358 40
        if (isset($pagamento['indicador'])) {
359
            $this->setIndicador($pagamento['indicador']);
360
        } else {
361 40
            $this->setIndicador(null);
362
        }
363 40
        if (isset($pagamento['forma'])) {
364 9
            $this->setForma($pagamento['forma']);
365
        } else {
366 40
            $this->setForma(null);
367
        }
368 40
        if (isset($pagamento['valor'])) {
369 9
            $this->setValor($pagamento['valor']);
370
        } else {
371 40
            $this->setValor(null);
372
        }
373 40
        if (!isset($pagamento['integrado'])) {
374 40
            $this->setIntegrado('N');
375
        } else {
376 9
            $this->setIntegrado($pagamento['integrado']);
377
        }
378 40
        if (isset($pagamento['credenciadora'])) {
379 1
            $this->setCredenciadora($pagamento['credenciadora']);
380
        } else {
381 40
            $this->setCredenciadora(null);
382
        }
383 40
        if (isset($pagamento['autorizacao'])) {
384 1
            $this->setAutorizacao($pagamento['autorizacao']);
385
        } else {
386 40
            $this->setAutorizacao(null);
387
        }
388 40
        if (isset($pagamento['bandeira'])) {
389 7
            $this->setBandeira($pagamento['bandeira']);
390
        } else {
391 40
            $this->setBandeira(null);
392
        }
393 40
        return $this;
394
    }
395
396 39
    public function getNode($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...
397
    {
398 39
        $dom = new \DOMDocument('1.0', 'UTF-8');
399 39
        if ($this->getValor() < 0) {
400 4
            $element = $dom->createElement(is_null($name)?'vTroco':$name);
401 4
            $this->setValor(-$this->getValor());
402 4
            $element->appendChild($dom->createTextNode($this->getValor(true)));
403 4
            $this->setValor(-$this->getValor());
404 4
            return $element;
405
        }
406 37
        $element = $dom->createElement(is_null($name)?'detPag':$name);
407 37
        if (!is_null($this->getIndicador())) {
408
            Util::appendNode($element, 'indPag', $this->getIndicador(true));
409
        }
410 37
        Util::appendNode($element, 'tPag', $this->getForma(true));
411 37
        Util::appendNode($element, 'vPag', $this->getValor(true));
412 37
        if (!$this->isCartao()) {
413 33
            return $element;
414
        }
415 35
        $cartao = $dom->createElement('card');
416 35
        Util::appendNode($cartao, 'tpIntegra', $this->getIntegrado(true));
417 35
        if ($this->isIntegrado()) {
418 2
            Util::appendNode($cartao, 'CNPJ', $this->getCredenciadora(true));
419
        }
420 35
        Util::appendNode($cartao, 'tBand', $this->getBandeira(true));
421 35
        if ($this->isIntegrado()) {
422 2
            Util::appendNode($cartao, 'cAut', $this->getAutorizacao(true));
423
        }
424 35
        $element->appendChild($cartao);
425 35
        return $element;
426
    }
427
428 30
    public function loadNode($element, $name = null)
0 ignored issues
show
Complexity introduced by
This operation has 1440 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...
429
    {
430 30
        $name = is_null($name)?'detPag':$name;
431 30
        if ($name == 'vTroco') {
432 2
            $this->setValor(
433 2
                '-'.Util::loadNode(
434 2
                    $element,
435 2
                    'vTroco',
436 2
                    'Tag "vTroco" do campo "Valor" não encontrada'
437
                )
438
            );
439 2
            return $element;
440
        }
441 29
        if ($element->nodeName != $name) {
442
            $_fields = $element->getElementsByTagName($name);
443
            if ($_fields->length == 0) {
444
                throw new \Exception('Tag "'.$name.'" não encontrada', 404);
445
            }
446
            $element = $_fields->item(0);
447
        }
448 29
        $this->setIndicador(
449 29
            Util::loadNode(
450 29
                $element,
451 29
                'indPag'
452
            )
453
        );
454 29
        $this->setForma(
455 29
            Util::loadNode(
456 29
                $element,
457 29
                'tPag',
458 29
                'Tag "tPag" do campo "Forma" não encontrada'
459
            )
460
        );
461 29
        $this->setValor(
462 29
            Util::loadNode(
463 29
                $element,
464 29
                'vPag',
465 29
                'Tag "vPag" do campo "Valor" não encontrada'
466
            )
467
        );
468 29
        $integrado = Util::loadNode($element, 'tpIntegra');
469 29
        if (is_null($integrado) && $this->isCartao()) {
470
            throw new \Exception('Tag "tpIntegra" do campo "Integrado" não encontrada', 404);
471
        }
472 29
        $this->setIntegrado($integrado);
473 29
        $this->setCredenciadora(Util::loadNode($element, 'CNPJ'));
474 29
        $autorizacao = Util::loadNode($element, 'cAut');
475 29
        if (is_null($autorizacao) && $this->isCartao() && is_numeric($this->getCredenciadora())) {
476
            throw new \Exception('Tag "cAut" do campo "Autorizacao" não encontrada', 404);
477
        }
478 29
        $this->setAutorizacao($autorizacao);
479 29
        $bandeira = Util::loadNode($element, 'tBand');
480 29
        if (is_null($bandeira) && $this->isCartao() && is_numeric($this->getCredenciadora())) {
481
            throw new \Exception('Tag "tBand" do campo "Bandeira" não encontrada', 404);
482
        }
483 29
        $this->setBandeira($bandeira);
484 29
        return $element;
485
    }
486
}
487