Completed
Pull Request — master (#19)
by Antonio Oertel
03:14
created

RioGrandeDoNorte   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 99
ccs 30
cts 30
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A normalizeNumber() 0 8 1
A getFormat() 0 4 1
A getLength() 0 4 1
A getRegex() 0 4 1
A calculateDigit() 0 12 1
A applyForNineDigits() 0 8 2
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration;
4
5
use Brazanation\Documents\DigitCalculator;
6
7
class RioGrandeDoNorte extends State
8
{
9
    const LABEL = 'RioGrandeDoNorte';
10
11
    const REGEX = '/^(20)(\d)?(\d{3})(\d{3})(\d{1})$/';
12
13
    const REGEX_FOR_NINE_NUMBERS = '/^(20)(\d{3})(\d{3})(\d{1})$/';
14
15
    const FORMAT = '$1.$2.$3.$4-$5';
16
17
    const FORMAT_FOR_NINE_NUMBERS = '$1.$2.$3-$4';
18
19
    const LENGTH = 10;
20
21
    const DIGITS_COUNT = 1;
22
23
    /**
24
     * @var int
25
     */
26
    private $length;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
28
    /**
29
     * @var string
30
     */
31
    private $format;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
32
33
    /**
34
     * @var string
35
     */
36
    private $regex;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
37
38 13
    public function __construct()
39
    {
40 13
        $this->length = self::LENGTH;
41 13
        $this->format = self::FORMAT;
42 13
        $this->regex = self::REGEX;
43 13
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
44 13
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 13
    public function normalizeNumber($number)
50
    {
51 13
        $number = parent::normalizeNumber($number);
52
53 13
        $this->applyForNineDigits($number);
54
55 13
        return $number;
56
    }
57
58 2
    public function getFormat()
59
    {
60 2
        return $this->format;
61
    }
62
63 13
    public function getLength()
64
    {
65 13
        return $this->length;
66
    }
67
68
    /**
69
     * @return string
70
     */
71 2
    public function getRegex()
72
    {
73 2
        return $this->regex;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     *
79
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_RN.html
80
     */
81 10
    public function calculateDigit($baseNumber)
82
    {
83 10
        $calculator = new DigitCalculator($baseNumber);
84 10
        $calculator->useComplementaryInsteadOfModule();
85 10
        $calculator->withMultipliersInterval(2, 10);
86 10
        $calculator->replaceWhen('0', 10, 11);
87 10
        $calculator->withModule(DigitCalculator::MODULE_11);
88
89 10
        $digit = $calculator->calculate();
90
91 10
        return "{$digit}";
92
    }
93
94
    /**
95
     * @param string $number
96
     */
97 13
    private function applyForNineDigits($number)
98
    {
99 13
        if (strlen($number) != self::LENGTH) {
100 5
            $this->format = self::FORMAT_FOR_NINE_NUMBERS;
101 5
            $this->regex = self::REGEX_FOR_NINE_NUMBERS;
102 5
            $this->length = 9;
103
        }
104 13
    }
105
}
106