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

Goias   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.88%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 2
dl 0
loc 107
ccs 31
cts 32
cp 0.9688
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setCurrentDigit() 0 6 1
A calculateDigit() 0 18 2
A isMagicNumber() 0 4 1
A fixedDigitForMagicNumber() 0 8 3
A discoverDigitToReplace() 0 9 3
A normalizeNumber() 0 7 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 LABEL = '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
    /**
20
     * @var string
21
     */
22
    private $digit;
23
24 7
    public function __construct()
25
    {
26 7
        parent::__construct(self::LABEL, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
27 7
    }
28
29
    /**
30
     *
31
     * @param string $digit
32
     *
33
     * @return Goias
34
     */
35 7
    private function setCurrentDigit($digit)
36
    {
37 7
        $this->digit = $digit;
38
39 7
        return $this;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_GO.html
46
     */
47 5
    public function calculateDigit($baseNumber)
48
    {
49 5
        $digitToReplace = $this->discoverDigitToReplace(intval($baseNumber));
50
51 5
        $calculator = new DigitCalculator($baseNumber);
52 5
        $calculator->useComplementaryInsteadOfModule();
53 5
        $calculator->replaceWhen($digitToReplace, 10);
54 5
        $calculator->replaceWhen('0', 11);
55 5
        $calculator->withModule(DigitCalculator::MODULE_11);
56
57 5
        $digit = $calculator->calculate();
58
59 5
        if ($this->isMagicNumber($baseNumber)) {
60 2
            return $this->fixedDigitForMagicNumber($digit);
61
        }
62
63 3
        return "{$digit}";
64
    }
65
66
    /**
67
     * A bizarre rule for this number(11094402).
68
     *
69
     * Default digit is 0, but could be 1 too :(
70
     *
71
     * @param string $number A magic base number.
72
     *
73
     * @return bool Returns true if number is magic, otherwise false.
74
     */
75 5
    private function isMagicNumber($number)
76
    {
77 5
        return $number == '11094402';
78
    }
79
80
    /**
81
     * A big workaround for StateRegistration(11094402)
82
     *
83
     * @param string $digit
84
     *
85
     * @return string
86
     */
87 2
    private function fixedDigitForMagicNumber($digit)
88
    {
89 2
        if ($digit == $this->digit && $digit == '0') {
90 1
            return '0';
91
        }
92
93 1
        return '1';
94
    }
95
96 5
    private function discoverDigitToReplace($number)
97
    {
98 5
        $digitToReplace = '0';
99 5
        if ((10103105 <= $number) && ($number <= 10119997)) {
100
            $digitToReplace = '1';
101
        }
102
103 5
        return $digitToReplace;
104
    }
105
106 7
    public function normalizeNumber($number)
107
    {
108 7
        $number = parent::normalizeNumber($number);
109 7
        $this->setCurrentDigit(substr($number, -($this->getNumberOfDigits())));
110
111 7
        return $number;
112
    }
113
}
114