Passed
Push — master ( 463105...84cd59 )
by Roberto
04:47
created

Keys   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 0
cbo 0
dl 0
loc 89
ccs 27
cts 28
cp 0.9643
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B build() 0 29 2
A isValid() 0 12 3
B verifyingDigit() 0 20 6
1
<?php
2
3
namespace NFePHP\Common;
4
5
/**
6
 * Class to create and validate the identification keys of electronic documents
7
 * from SPED
8
 * @category   NFePHP
9
 * @package    NFePHP\Common\Keys
10
 * @copyright  Copyright (c) 2008-2016
11
 * @license    http://www.gnu.org/licenses/lesser.html LGPL v3
12
 * @author     Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link       http://github.com/nfephp-org/nfephp for the canonical source repository
14
 */
15
16
class Keys
17
{
18
    /**
19
     * Build 44 digits keys to NFe, NFCe, CTe and MDFe
20
     * @param string $cUF UF number
21
     * @param string $ano year
22
     * @param string $mes month
23
     * @param string $cnpj
24
     * @param string $mod model of document 55, 65, 57 etc
25
     * @param string $serie
26
     * @param string $numero document number
27
     * @param string $tpEmis emission type
28
     * @param string $codigo random number or document number
29
     * @return string
30
     */
31 2
    public static function build(
32
        $cUF,
33
        $ano,
34
        $mes,
35
        $cnpj,
36
        $mod,
37
        $serie,
38
        $numero,
39
        $tpEmis,
40
        $codigo = ''
41
    ) {
42 2
        if ($codigo == '') {
43 1
            $codigo = $numero;
44
        }
45 2
        $format = "%02d%02d%02d%s%02d%03d%09d%01d%08d";
46 2
        $key = sprintf(
47
            $format,
48
            $cUF,
49
            $ano,
50
            $mes,
51
            $cnpj,
52
            $mod,
53
            $serie,
54
            $numero,
55
            $tpEmis,
56
            $codigo
57
        );
58 2
        return $key . self::verifyingDigit($key);
59
    }
60
    
61
    /**
62
     * Verifies that the key provided is valid
63
     * @param string $key
64
     * @return boolean
65
     */
66 2
    public static function isValid($key)
67
    {
68 2
        if (strlen($key) != 44) {
69
            return false;
70
        }
71 2
        $cDV = substr($key, -1);
72 2
        $calcDV = self::verifyingDigit(substr($key, 0, 43));
73 2
        if ($cDV === $calcDV) {
74 1
            return true;
75
        }
76 1
        return false;
77
    }
78
    
79
    /**
80
     * This method calculates verifying digit
81
     * @param string $key
82
     * @return string
83
     */
84 6
    public static function verifyingDigit($key)
85
    {
86 6
        if (strlen($key) != 43) {
87 1
            return '';
88
        }
89 5
        $multipliers = [2, 3, 4, 5, 6, 7, 8, 9];
90 5
        $iCount = 42;
91 5
        $weightedSum = 0;
92 5
        while ($iCount >= 0) {
93 5
            for ($mCount = 0; $mCount < 8 && $iCount >= 0; $mCount++) {
94 5
                $weightedSum += (substr($key, $iCount, 1) * $multipliers[$mCount]);
95 5
                $iCount--;
96
            }
97
        }
98 5
        $vdigit = 11 - ($weightedSum % 11);
99 5
        if ($vdigit > 9) {
100 3
            $vdigit = 0;
101
        }
102 5
        return (string) $vdigit;
103
    }
104
}
105