Test Failed
Push — master ( f8eb03...8feb3a )
by Francimar
06:03
created

Pagamento   F

Complexity

Total Complexity 94

Size/Duplication

Total Lines 454
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 79.6%

Importance

Changes 0
Metric Value
wmc 94
lcom 1
cbo 1
dl 0
loc 454
ccs 199
cts 250
cp 0.796
rs 2
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C getForma() 0 29 12
B setForma() 0 37 11
A getValor() 0 7 2
A setValor() 0 6 1
A getIntegrado() 0 7 3
A isIntegrado() 0 4 1
A setIntegrado() 0 8 4
A getCredenciadora() 0 7 2
A setCredenciadora() 0 5 1
A getAutorizacao() 0 7 2
A setAutorizacao() 0 5 1
B getBandeira() 0 19 7
B setBandeira() 0 22 6
A isCartao() 0 4 1
A toArray() 0 12 1
C fromArray() 0 44 10
B getNode() 0 31 8
C loadNode() 0 58 13
A getIndicador() 0 13 4
A setIndicador() 0 13 3

How to fix   Complexity   

Complex Class

Complex classes like Pagamento often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Pagamento, and based on these observations, apply Extract Interface, too.

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 41
    public function __construct($pagamento = [])
81
    {
82 41
        $this->fromArray($pagamento);
83 41
    }
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 39
    public function getIndicador($normalize = false)
92
    {
93 39
        if (!$normalize) {
94 39
            return $this->indicador;
95
        }
96 1
        switch ($this->indicador) {
97 1
            case self::INDICADOR_AVISTA:
98 1
                return '0';
99 1
            case self::INDICADOR_APRAZO:
100 1
                return '1';
101
        }
102 1
        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 41
    public function setIndicador($indicador)
111
    {
112
        switch ($indicador) {
113 41
            case '0':
114 1
                $indicador = self::INDICADOR_AVISTA;
115 1
                break;
116 41
            case '1':
117 1
                $indicador = self::INDICADOR_APRAZO;
118 1
                break;
119
        }
120 41
        $this->indicador = $indicador;
121 41
        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 41
    public function setForma($forma)
160
    {
161
        switch ($forma) {
162 41
            case '01':
163 28
                $forma = self::FORMA_DINHEIRO;
164 28
                break;
165 41
            case '02':
166
                $forma = self::FORMA_CHEQUE;
167
                break;
168 41
            case '03':
169 28
                $forma = self::FORMA_CREDITO;
170 28
                break;
171 41
            case '04':
172 1
                $forma = self::FORMA_DEBITO;
173 1
                break;
174 41
            case '05':
175
                $forma = self::FORMA_CREDIARIO;
176
                break;
177 41
            case '10':
178
                $forma = self::FORMA_ALIMENTACAO;
179
                break;
180 41
            case '11':
181
                $forma = self::FORMA_REFEICAO;
182
                break;
183 41
            case '12':
184
                $forma = self::FORMA_PRESENTE;
185
                break;
186 41
            case '13':
187
                $forma = self::FORMA_COMBUSTIVEL;
188
                break;
189 41
            case '99':
190
                $forma = self::FORMA_OUTROS;
191
                break;
192
        }
193 41
        $this->forma = $forma;
194 41
        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 41
    public function setValor($valor)
209
    {
210 41
        $valor = floatval($valor);
211 41
        $this->valor = $valor;
212 41
        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 10
            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 41
    public function setIntegrado($integrado)
241
    {
242 41
        if (is_bool($integrado)) {
243
            $integrado = $integrado ? 'Y': 'N';
244
        }
245 41
        $this->integrado = in_array($integrado, ['Y', '1']) ? 'Y' : 'N';
246 41
        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 41
    public function setCredenciadora($credenciadora)
261
    {
262 41
        $this->credenciadora = $credenciadora;
263 41
        return $this;
264
    }
265
266
    /**
267
     * Número de autorização da operação cartão de crédito/débito
268
     */
269 11
    public function getAutorizacao($normalize = false)
270
    {
271 11
        if (!$normalize) {
272 10
            return $this->autorizacao;
273
        }
274 2
        return $this->autorizacao;
275
    }
276
277 41
    public function setAutorizacao($autorizacao)
278
    {
279 41
        $this->autorizacao = $autorizacao;
280 41
        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 10
            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 41
    public function setBandeira($bandeira)
308
    {
309
        switch ($bandeira) {
310 41
            case '01':
311 1
                $bandeira = self::BANDEIRA_VISA;
312 1
                break;
313 41
            case '02':
314 28
                $bandeira = self::BANDEIRA_MASTERCARD;
315 28
                break;
316 41
            case '03':
317
                $bandeira = self::BANDEIRA_AMEX;
318
                break;
319 41
            case '04':
320
                $bandeira = self::BANDEIRA_SOROCRED;
321
                break;
322 41
            case '99':
323
                $bandeira = self::BANDEIRA_OUTROS;
324
                break;
325
        }
326 41
        $this->bandeira = $bandeira;
327 41
        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 10
    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 10
        $pagamento = [];
341 10
        $pagamento['indicador'] = $this->getIndicador();
342 10
        $pagamento['forma'] = $this->getForma();
343 10
        $pagamento['valor'] = $this->getValor();
344 10
        $pagamento['integrado'] = $this->getIntegrado();
345 10
        $pagamento['credenciadora'] = $this->getCredenciadora();
346 10
        $pagamento['autorizacao'] = $this->getAutorizacao();
347 10
        $pagamento['bandeira'] = $this->getBandeira();
348 10
        return $pagamento;
349
    }
350
351 41
    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 41
        if ($pagamento instanceof Pagamento) {
354 9
            $pagamento = $pagamento->toArray();
355 41
        } elseif (!is_array($pagamento)) {
356 9
            return $this;
357
        }
358 41
        if (isset($pagamento['indicador'])) {
359
            $this->setIndicador($pagamento['indicador']);
360
        } else {
361 41
            $this->setIndicador(null);
362
        }
363 41
        if (isset($pagamento['forma'])) {
364 9
            $this->setForma($pagamento['forma']);
365
        } else {
366 41
            $this->setForma(null);
367
        }
368 41
        if (isset($pagamento['valor'])) {
369 9
            $this->setValor($pagamento['valor']);
370
        } else {
371 41
            $this->setValor(null);
372
        }
373 41
        if (!isset($pagamento['integrado'])) {
374 41
            $this->setIntegrado('N');
375
        } else {
376 9
            $this->setIntegrado($pagamento['integrado']);
377
        }
378 41
        if (isset($pagamento['credenciadora'])) {
379 1
            $this->setCredenciadora($pagamento['credenciadora']);
380
        } else {
381 41
            $this->setCredenciadora(null);
382
        }
383 41
        if (isset($pagamento['autorizacao'])) {
384 1
            $this->setAutorizacao($pagamento['autorizacao']);
385
        } else {
386 41
            $this->setAutorizacao(null);
387
        }
388 41
        if (isset($pagamento['bandeira'])) {
389 7
            $this->setBandeira($pagamento['bandeira']);
390
        } else {
391 41
            $this->setBandeira(null);
392
        }
393 41
        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 31
    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 31
        $name = is_null($name)?'detPag':$name;
431 31
        if ($name == 'vTroco') {
432 3
            $this->setValor(
433 3
                '-'.Util::loadNode(
434 3
                    $element,
435 3
                    'vTroco',
436 3
                    'Tag "vTroco" do campo "Valor" não encontrada'
437
                )
438
            );
439 3
            return $element;
440
        }
441 30
        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 30
        $this->setIndicador(
449 30
            Util::loadNode(
450 30
                $element,
451 30
                'indPag'
452
            )
453
        );
454 30
        $this->setForma(
455 30
            Util::loadNode(
456 30
                $element,
457 30
                'tPag',
458 30
                'Tag "tPag" do campo "Forma" não encontrada'
459
            )
460
        );
461 30
        $this->setValor(
462 30
            Util::loadNode(
463 30
                $element,
464 30
                'vPag',
465 30
                'Tag "vPag" do campo "Valor" não encontrada'
466
            )
467
        );
468 30
        $integrado = Util::loadNode($element, 'tpIntegra');
469 30
        if (is_null($integrado) && $this->isCartao()) {
470
            throw new \Exception('Tag "tpIntegra" do campo "Integrado" não encontrada', 404);
471
        }
472 30
        $this->setIntegrado($integrado);
473 30
        $this->setCredenciadora(Util::loadNode($element, 'CNPJ'));
474 30
        $autorizacao = Util::loadNode($element, 'cAut');
475 30
        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 30
        $this->setAutorizacao($autorizacao);
479 30
        $bandeira = Util::loadNode($element, 'tBand');
480 30
        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 30
        $this->setBandeira($bandeira);
484 30
        return $element;
485
    }
486
}
487