Passed
Push — master ( 000227...5ee16c )
by Antonio Oertel
32s
created

Cnh   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 0
cbo 2
dl 0
loc 74
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A calculateDigit() 0 7 1
A format() 0 4 1
A calculateFirstDigit() 0 10 1
A calculateSecondDigit() 0 10 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Cnh extends AbstractDocument implements DocumentInterface
6
{
7
    const LENGTH = 11;
8
9
    const LABEL = 'CNH';
10
11
    const REGEX = '/^([\d]{3})([\d]{3})([\d]{4})([\d]{1})$/';
12
13
    /**
14
     * Cnh constructor.
15
     *
16
     * @param string $cnh Only accept numbers
17
     */
18 9
    public function __construct($cnh)
19
    {
20 9
        $cnh = preg_replace('/\D/', '', $cnh);
21 9
        parent::__construct($cnh, self::LENGTH, 2, self::LABEL);
22 3
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 4
    public function calculateDigit($baseNumber)
28
    {
29 4
        $firstDigit = $this->calculateFirstDigit($baseNumber);
30 4
        $secondDigit = $this->calculateSecondDigit($baseNumber);
31
32 4
        return "{$firstDigit}{$secondDigit}";
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function format()
39
    {
40 1
        return "{$this}";
41
    }
42
43
    /**
44
     * Calculate check digit from base number.
45
     *
46
     * @param string $baseNumber Base numeric section to be calculate your digit.
47
     *
48
     * @return string Returns a calculated checker digit.
49
     */
50 4
    private function calculateFirstDigit($baseNumber)
51
    {
52 4
        $calculator = new DigitCalculator($baseNumber);
53 4
        $calculator->withMultipliersInterval(1, 9);
54 4
        $calculator->replaceWhen('0', 10, 11);
55 4
        $calculator->withModule(DigitCalculator::MODULE_11);
56 4
        $firstDigit = $calculator->calculate();
57
58 4
        return "{$firstDigit}";
59
    }
60
61
    /**
62
     * Calculate check digit from base number.
63
     *
64
     * @param string $baseNumber Base numeric section to be calculate your digit.
65
     *
66
     * @return string Returns a calculated checker digit.
67
     */
68 4
    private function calculateSecondDigit($baseNumber)
69
    {
70 4
        $calculator = new DigitCalculator($baseNumber);
71 4
        $calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2, 1]);
72 4
        $calculator->replaceWhen('0', 10, 11);
73 4
        $calculator->withModule(DigitCalculator::MODULE_11);
74 4
        $secondDigit = $calculator->calculate();
75
76 4
        return "{$secondDigit}";
77
    }
78
}
79