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

Bahia   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 95.65%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 8
c 2
b 1
f 1
lcom 0
cbo 2
dl 0
loc 61
ccs 22
cts 23
cp 0.9565
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalizeNumber() 0 8 2
A calculateDigit() 0 13 1
A discoverModule() 0 12 4
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