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

Urban   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 77
ccs 29
cts 29
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A extractBaseNumber() 0 4 1
A extractCheckerDigit() 0 4 1
A calculateDigit() 0 12 1
A calculateFirstDigit() 0 11 1
A calculateSecondDigit() 0 11 1
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration\SaoPaulo;
4
5
use Brazanation\Documents\DigitCalculator;
6
use Brazanation\Documents\StateRegistration\State;
7
8
class Urban extends State
9
{
10
    const LABEL = 'SaoPaulo';
11
12
    const REGEX = '/^(\d{3})(\d{3})(\d{3})(\d{3})$/';
13
14
    const FORMAT = '$1.$2.$3.$4';
15
16
    const LENGTH = 12;
17
18
    const DIGITS_COUNT = 1;
19
20 7
    public function __construct()
21
    {
22 7
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
23 7
    }
24
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * No extract any digit
29
     */
30 2
    public function extractBaseNumber($number)
31
    {
32 2
        return $number;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 5
    public function extractCheckerDigit($number)
39
    {
40 5
        return substr($number, self::LENGTH - 4, 1) . substr($number, -1);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     *
46
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_SP.html
47
     */
48 2
    public function calculateDigit($baseNumber)
49
    {
50 2
        $firstBaseNumber = substr($baseNumber, 0, 8);
51
52 2
        $firstDigit = $this->calculateFirstDigit($firstBaseNumber);
53
54 2
        $secondBaseNumber = "{$firstBaseNumber}{$firstDigit}" . substr($baseNumber, -3, 2);
55
56 2
        $secondDigit = $this->calculateSecondDigit($secondBaseNumber);
57
58 2
        return "{$firstDigit}{$secondDigit}";
59
    }
60
61 2
    private function calculateFirstDigit($baseNumber)
62
    {
63 2
        $calculator = new DigitCalculator($baseNumber);
64 2
        $calculator->withMultipliers([10, 8, 7, 6, 5, 4, 3, 1]);
65 2
        $calculator->replaceWhen('0', 10);
66 2
        $calculator->replaceWhen('1', 11);
67 2
        $calculator->withModule(DigitCalculator::MODULE_11);
68 2
        $digit = $calculator->calculate();
69
70 2
        return "{$digit}";
71
    }
72
73 2
    private function calculateSecondDigit($baseNumber)
74
    {
75 2
        $calculator = new DigitCalculator($baseNumber);
76 2
        $calculator->withMultipliersInterval(2, 10);
77 2
        $calculator->replaceWhen('0', 10);
78 2
        $calculator->replaceWhen('1', 11);
79 2
        $calculator->withModule(DigitCalculator::MODULE_11);
80 2
        $digit = $calculator->calculate();
81
82 2
        return "{$digit}";
83
    }
84
}
85