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

Urban   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

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