Passed
Push — master ( 89672a...d55cf9 )
by Antonio Oertel
28s
created

NFeAccessKey   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 97.22%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 2
cbo 2
dl 0
loc 98
ccs 35
cts 36
cp 0.9722
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generate() 0 14 1
A format() 0 4 1
A __toString() 0 4 1
A validate() 0 9 3
A isValid() 0 14 3
A calculateDigits() 0 9 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
use Brazanation\Documents\Exception\InvalidDocument as InvalidDocumentException;
6
7
final class NFeAccessKey implements DocumentInterface
8
{
9
    const LABEL = 'NFeAccessKey';
10
11
    const LENGTH = 44;
12
13
    const REGEX = '/([\d]{4})/';
14
15
    const MASK = '$1 ';
16
17
    const MODEL = 55;
18
19
    /**
20
     * @var string
21
     */
22
    private $nfeKey;
23
24
    /**
25
     * NFeAccessKey constructor.
26
     *
27
     * @param $nfeKey
28
     */
29 17
    public function __construct($nfeKey)
30
    {
31 17
        $this->validate($nfeKey);
32 4
        $this->nfeKey = $nfeKey;
33 4
    }
34
35
    /**
36
     * @param int       $state
37
     * @param \DateTime $generatedAt
38
     * @param Cnpj      $cnpj
39
     * @param int       $serie
40
     * @param int       $invoiceNumber
41
     * @param int       $controlNumber
42
     *
43
     * @return NFeAccessKey
44
     */
45 1
    public static function generate($state, \DateTime $generatedAt, Cnpj $cnpj, $serie, $invoiceNumber, $controlNumber)
46
    {
47 1
        $yearMonth = $generatedAt->format('ym');
48 1
        $serie = str_pad($serie, 3, 0, STR_PAD_LEFT);
49 1
        $model = self::MODEL;
50 1
        $invoiceNumber = str_pad($invoiceNumber, 9, 0, STR_PAD_LEFT);
51 1
        $controlNumber = str_pad($controlNumber, 9, 0, STR_PAD_LEFT);
52
53 1
        $baseNumber = "{$state}{$yearMonth}{$cnpj}{$model}{$serie}{$invoiceNumber}{$controlNumber}";
54
55 1
        $digit = self::calculateDigits($baseNumber);
56
57 1
        return new self("{$baseNumber}{$digit}");
58
    }
59
60 1
    public function format()
61
    {
62 1
        return trim(preg_replace(static::REGEX, static::MASK, $this->nfeKey));
63
    }
64
65 3
    public function __toString()
66
    {
67 3
        return $this->nfeKey;
68
    }
69
70 17
    private function validate($number)
71
    {
72 17
        if (empty($number)) {
73 3
            throw InvalidDocumentException::notEmpty(static::LABEL);
74
        }
75 14
        if (!$this->isValid($number)) {
76 10
            throw InvalidDocumentException::isNotValid(static::LABEL, $number);
77
        }
78 4
    }
79
80 14
    private function isValid($number)
81
    {
82 14
        if (strlen($number) != static::LENGTH) {
83
            return false;
84
        }
85
86 14
        if (preg_match("/^{$number[0]}{" . static::LENGTH . '}$/', $number)) {
87 1
            return false;
88
        }
89
90 13
        $digits = static::calculateDigits(substr($number, 0, -1));
0 ignored issues
show
Comprehensibility introduced by
Since Brazanation\Documents\NFeAccessKey is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
91
92 13
        return $digits === substr($number, -1);
93
    }
94
95 13
    private static function calculateDigits($baseNumber)
96
    {
97 13
        $calculator = new DigitCalculator($baseNumber);
98 13
        $calculator->useComplementaryInsteadOfModule();
99 13
        $calculator->withModule(DigitCalculator::MODULE_11);
100 13
        $digit = $calculator->calculate();
101
102 13
        return "{$digit}";
103
    }
104
}
105