Completed
Pull Request — master (#17)
by Antonio Oertel
02:49
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 8
Bugs 0 Features 1
Metric Value
wmc 3
c 8
b 0
f 1
lcom 0
cbo 2
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10

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 implements DocumentInterface
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 12
    public function __construct($cnpj)
19
    {
20 12
        $cnpj = preg_replace('/\D/', '', $cnpj);
21 12
        parent::__construct($cnpj, self::LENGTH, 2, self::LABEL);
22 5
    }
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 7
    public function calculateDigit($baseNumber)
36
    {
37 7
        $calculator = new DigitCalculator($baseNumber);
38 7
        $calculator->useComplementaryInsteadOfModule();
39 7
        $calculator->replaceWhen('0', 10, 11);
40 7
        $calculator->withModule(DigitCalculator::MODULE_11);
41 7
        $firstDigit = $calculator->calculate();
42 7
        $calculator->addDigit($firstDigit);
43 7
        $secondDigit = $calculator->calculate();
44
45 7
        return "{$firstDigit}{$secondDigit}";
46
    }
47
}
48