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

Keys::testaChave()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
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