Passed
Push — master ( 18033a...39d46d )
by Antonio Oertel
53s
created

Cnpj   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A format() 0 4 1
A calculateDigit() 0 12 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