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

Rural   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 47
ccs 15
cts 15
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A extractBaseNumber() 0 4 1
A extractCheckerDigit() 0 4 1
A calculateDigit() 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 Rural extends State
9
{
10
    const LABEL = 'SaoPaulo';
11
12
    const REGEX = '/^(\d{8})(\d{1})(\d{3})$/';
13
14
    const FORMAT = 'P-$1.$2/$3';
15
16
    const LENGTH = 12;
17
18
    const DIGITS_COUNT = 1;
19
20 2
    public function __construct()
21
    {
22 2
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
23 2
    }
24
25 2
    public function extractBaseNumber($number)
26
    {
27 2
        return substr($number, 0, self::LENGTH - 4);
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    public function extractCheckerDigit($number)
34
    {
35 2
        return substr($number, self::LENGTH - 4, 1);
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     *
41
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_SP.html
42
     */
43 2
    public function calculateDigit($baseNumber)
44
    {
45 2
        $calculator = new DigitCalculator($baseNumber);
46 2
        $calculator->withMultipliers([10, 8, 7, 6, 5, 4, 3, 1]);
47 2
        $calculator->replaceWhen('0', 10);
48 2
        $calculator->replaceWhen('1', 11);
49 2
        $calculator->withModule(DigitCalculator::MODULE_11);
50 2
        $digit = $calculator->calculate();
51
52 2
        return "{$digit}";
53
    }
54
}
55