Passed
Pull Request — master (#32)
by Antonio Oertel
04:01
created

Goias::normalizeNumber()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration;
4
5
use Brazanation\Documents\DigitCalculator;
6
7
final class Goias extends State
8
{
9
    const LONG_NAME = 'Goias';
10
11
    const REGEX = '/^(1[015])(\d{3})(\d{3})(\d{1})$/';
12
13
    const FORMAT = '$1.$2.$3-$4';
14
15
    const LENGTH = 9;
16
17
    const DIGITS_COUNT = 1;
18
19
    const SHORT_NAME = 'GO';
20
21
    /**
22
     * @var string
23
     */
24
    private $digit;
25
26 7
    public function __construct()
27
    {
28 7
        parent::__construct(self::LONG_NAME, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
29 7
    }
30
31
    /**
32
     *
33
     * @param string $digit
34
     *
35
     * @return Goias
36
     */
37 7
    private function setCurrentDigit($digit)
38
    {
39 7
        $this->digit = $digit;
40
41 7
        return $this;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     *
47
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_GO.html
48
     */
49 5
    public function calculateDigit($baseNumber)
50
    {
51 5
        $digitToReplace = $this->discoverDigitToReplace(intval($baseNumber));
52
53 5
        $calculator = new DigitCalculator($baseNumber);
54 5
        $calculator->useComplementaryInsteadOfModule();
55 5
        $calculator->replaceWhen($digitToReplace, 10);
56 5
        $calculator->replaceWhen('0', 11);
57 5
        $calculator->withModule(DigitCalculator::MODULE_11);
58
59 5
        $digit = $calculator->calculate();
60
61 5
        if ($this->isMagicNumber($baseNumber)) {
62 2
            return $this->fixedDigitForMagicNumber($digit);
63
        }
64
65 3
        return "{$digit}";
66
    }
67
68
    /**
69
     * A bizarre rule for this number(11094402).
70
     *
71
     * Default digit is 0, but could be 1 too :(
72
     *
73
     * @param string $number A magic base number.
74
     *
75
     * @return bool Returns true if number is magic, otherwise false.
76
     */
77 5
    private function isMagicNumber($number)
78
    {
79 5
        return $number == '11094402';
80
    }
81
82
    /**
83
     * A big workaround for StateRegistration(11094402)
84
     *
85
     * @param string $digit
86
     *
87
     * @return string
88
     */
89 2
    private function fixedDigitForMagicNumber($digit)
90
    {
91 2
        if ($digit == $this->digit && $digit == '0') {
92 1
            return '0';
93
        }
94
95 1
        return '1';
96
    }
97
98 5
    private function discoverDigitToReplace($number)
99
    {
100 5
        $digitToReplace = '0';
101 5
        if ((10103105 <= $number) && ($number <= 10119997)) {
102
            $digitToReplace = '1';
103
        }
104
105 5
        return $digitToReplace;
106
    }
107
108 7
    public function normalizeNumber($number)
109
    {
110 7
        $number = parent::normalizeNumber($number);
111 7
        $this->setCurrentDigit(substr($number, -($this->getNumberOfDigits())));
112
113 7
        return $number;
114
    }
115
}
116