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

Bahia::normalizeNumber()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration;
4
5
use Brazanation\Documents\DigitCalculator;
6
7
final class Bahia extends State
8
{
9
    const LABEL = 'Bahia';
10
11
    const REGEX = '/^([\d]{6,7})(\d{2})$/';
12
13
    const FORMAT = '$1-$2';
14
15
    const LENGTH = 9;
16
17
    const DIGITS_COUNT = 2;
18
19 11
    public function __construct()
20
    {
21 11
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
22 11
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 11
    public function normalizeNumber($number)
28
    {
29 11
        if (!empty($number)) {
30 10
            return str_pad(parent::normalizeNumber($number), $this->getLength(), '0', STR_PAD_LEFT);
31
        }
32
33 1
        return $number;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     *
39
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_BA.html
40
     */
41 10
    public function calculateDigit($baseNumber)
42
    {
43 10
        $calculator = new DigitCalculator($baseNumber);
44 10
        $calculator->useComplementaryInsteadOfModule();
45 10
        $calculator->replaceWhen('0', 10, 11);
46 10
        $calculator->withModule($this->discoverModule($baseNumber));
47
48 10
        $firstDigit = $calculator->calculate();
49 10
        $calculator->addDigit($firstDigit);
50 10
        $secondDigit = $calculator->calculate();
51
52 10
        return "{$secondDigit}{$firstDigit}";
53
    }
54
55 10
    private function discoverModule($baseNumber)
56
    {
57 10
        $charToCheck = substr($baseNumber, 1, 1);
58 10
        if (strlen($baseNumber) == 6) {
59
            $charToCheck = substr($baseNumber, 0, 1);
60
        }
61 10
        if (6 <= $charToCheck && $charToCheck <= 9) {
62 3
            return DigitCalculator::MODULE_11;
63
        }
64
65 7
        return DigitCalculator::MODULE_10;
66
    }
67
}
68