Passed
Pull Request — master (#32)
by Antonio Oertel
04:01
created

Cnpj::calculateDigit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Cnpj extends AbstractDocument
6
{
7
    const LENGTH = 14;
8
9
    const LABEL = 'CNPJ';
10
11
    const REGEX = '/^([\d]{2})([\d]{3})([\d]{3})([\d]{4})([\d]{2})$/';
12
13
    /**
14
     * Cnpj constructor.
15
     *
16
     * @param string $cnpj Only accept numbers
17
     */
18 28
    public function __construct($cnpj)
19
    {
20 28
        $cnpj = preg_replace('/\D/', '', $cnpj);
21 28
        parent::__construct($cnpj, self::LENGTH, 2, self::LABEL);
22 21
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function format()
28
    {
29 2
        return preg_replace(self::REGEX, '$1.$2.$3/$4-$5', "{$this}");
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 23
    public function calculateDigit($baseNumber)
36
    {
37 23
        $calculator = new DigitCalculator($baseNumber);
38 23
        $calculator->useComplementaryInsteadOfModule();
39 23
        $calculator->replaceWhen('0', 10, 11);
40 23
        $calculator->withModule(DigitCalculator::MODULE_11);
41 23
        $firstDigit = $calculator->calculate();
42 23
        $calculator->addDigit($firstDigit);
43 23
        $secondDigit = $calculator->calculate();
44
45 23
        return "{$firstDigit}{$secondDigit}";
46
    }
47
}
48