Passed
Branch main (274970)
by Antonio Oertel
03:05
created

Amapa::calculateDigit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 16
ccs 13
cts 13
cp 1
crap 1
rs 9.8666
c 1
b 0
f 1
1
<?php
2
3
namespace Brazanation\Documents\StateRegistration;
4
5
use Brazanation\Documents\DigitCalculator;
6
7
final class Amapa extends State
8
{
9
    const LONG_NAME = 'Amapa';
10
11
    const SHORT_NAME = 'AP';
12
13
    const REGEX = '/^(03)(\d{3})(\d{3})(\d{1})$/';
14
15
    const FORMAT = '$1.$2.$3-$4';
16
17
    const LENGTH = 9;
18
19
    const NUMBER_OF_DIGITS = 1;
20
21 10
    public function __construct()
22
    {
23 10
        parent::__construct(self::LONG_NAME, self::LENGTH, self::NUMBER_OF_DIGITS, self::REGEX, self::FORMAT);
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_AP.html
30
     */
31 8
    public function calculateDigit(string $baseNumber) : string
32
    {
33 8
        $number = (int) $baseNumber;
34 8
        $lastDigit = $this->discoverLastDigit($number);
35 8
        $digitToReplace = $this->discoverDigitToReplace($number);
36
37 8
        $calculator = new DigitCalculator($baseNumber);
38 8
        $calculator->addDigit($lastDigit);
39 8
        $calculator->useComplementaryInsteadOfModule();
40 8
        $calculator->withMultipliersInterval(1, 9);
41 8
        $calculator->replaceWhen('0', 10);
42 8
        $calculator->replaceWhen($digitToReplace, 11);
43 8
        $calculator->withModule(DigitCalculator::MODULE_11);
44 8
        $digit = $calculator->calculate();
45
46 8
        return "{$digit}";
47
    }
48
49
    /**
50
     * Discover last digit base on number threshold.
51
     *
52
     * @param int $number
53
     *
54
     * @return string
55
     */
56 8
    private function discoverLastDigit(int $number) : string
57
    {
58 8
        $lastDigit = '0';
59 8
        if ($this->isBetweenFirstSlice($number)) {
60 8
            $lastDigit = '5';
61
        }
62 8
        if ($this->isBetweenSecondSlice($number)) {
63
            $lastDigit = '9';
64
        }
65
66 8
        return $lastDigit;
67
    }
68
69
    /**
70
     * Discover digit to be replaced based on number threshold.
71
     *
72
     * @param int $number
73
     *
74
     * @return string
75
     */
76 8
    private function discoverDigitToReplace(int $number) : string
77
    {
78 8
        $replaceDigit = '0';
79 8
        if ($this->isBetweenSecondSlice($number)) {
80
            $replaceDigit = '1';
81
        }
82
83 8
        return $replaceDigit;
84
    }
85
86
    /**
87
     * Is number between 3017001 and 3019022.
88
     *
89
     * @param int $number
90
     *
91
     * @return bool
92
     */
93 8
    private function isBetweenFirstSlice(int $number) : bool
94
    {
95 8
        return (3000001 <= $number && $number <= 3017000);
96
    }
97
98
    /**
99
     * Is number between 3017001 and 3019022.
100
     *
101
     * @param int $number
102
     *
103
     * @return bool
104
     */
105 8
    private function isBetweenSecondSlice(int $number) : bool
106
    {
107 8
        return (3017001 <= $number && $number <= 3019022);
108
    }
109
}
110