Completed
Pull Request — master (#46)
by Antonio Oertel
04:53
created

Keys::buildKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 16
nc 2
nop 9
crap 2.0185

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace NFePHP\Common\Keys;
4
5
/**
6
 * Classe auxiliar para criar, listar e testar os diretórios utilizados pela API
7
 * @category   NFePHP
8
 * @package    NFePHP\Common\Keys
9
 * @copyright  Copyright (c) 2008-2015
10
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
11
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
12
 * @link       http://github.com/nfephp-org/nfephp for the canonical source repository
13
 */
14
15
16
class Keys
17
{
18
    /**
19
     * buildKey
20
     * Monta as chaves de 44 digitos para NFe, NFCe, CTe e MDFe
21
     *
22
     * @param string $cUF
23
     * @param string $ano
24
     * @param string $mes
25
     * @param string $cnpj
26
     * @param string $mod
27
     * @param string $serie
28
     * @param string $numero
29
     * @param string $tpEmis
30
     * @param string $codigo
31
     * @return string
32
     */
33 1
    public static function buildKey($cUF, $ano, $mes, $cnpj, $mod, $serie, $numero, $tpEmis, $codigo = '')
34
    {
35 1
        if ($codigo == '') {
36
            $codigo = $numero;
37
        }
38 1
        $forma = "%02d%02d%02d%s%02d%03d%09d%01d%08d";
39 1
        $chave = sprintf(
40
            $forma,
41
            $cUF,
42
            $ano,
43
            $mes,
44
            $cnpj,
45
            $mod,
46
            $serie,
47
            $numero,
48
            $tpEmis,
49
            $codigo
50
        );
51 1
        return $chave.self::calculaDV($chave);
52
    }
53
    
54
    /**
55
     * testaChave
56
     * Testa a chave com o digito verificador no final
57
     *
58
     * @param string $chave
59
     * @return boolean
60
     */
61 1
    public static function testaChave($chave = '')
62
    {
63 1
        if (strlen($chave) != 44) {
64 1
            return false;
65
        }
66 1
        $cDV = substr($chave, -1);
67 1
        $calcDV = self::calculaDV(substr($chave, 0, 43));
68 1
        if ($cDV === $calcDV) {
69 1
            return true;
70
        }
71 1
        return false;
72
    }
73
    
74
    /**
75
     * calculaDV
76
     * Função para o calculo o digito verificador da chave da NFe
77
     *
78
     * @param string $chave43
79
     * @return string
80
     */
81 3
    public static function calculaDV($chave43)
82
    {
83 3
        $multiplicadores = array(2, 3, 4, 5, 6, 7, 8, 9);
84 3
        $iCount = 42;
85 3
        $somaPonderada = 0;
86 3
        while ($iCount >= 0) {
87 3
            for ($mCount = 0; $mCount < count($multiplicadores) && $iCount >= 0; $mCount++) {
88 3
                $num = (int) substr($chave43, $iCount, 1);
89 3
                $peso = (int) $multiplicadores[$mCount];
90 3
                $somaPonderada += $num * $peso;
91 3
                $iCount--;
92
            }
93
        }
94 3
        $resto = $somaPonderada % 11;
95 3
        if ($resto == '0' || $resto == '1') {
96 3
            $cDV = 0;
97
        } else {
98
            $cDV = 11 - $resto;
99
        }
100 3
        return (string) $cDV;
101
    }
102
}
103