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

Amapa   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 103
ccs 30
cts 32
cp 0.9375
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A calculateDigit() 0 17 1
A discoverLastDigit() 0 12 3
A discoverDigitToReplace() 0 9 2
A isBetweenFirstSlice() 0 4 2
A isBetweenSecondSlice() 0 4 2
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 DIGITS_COUNT = 1;
20
21 6
    public function __construct()
22
    {
23 6
        parent::__construct(self::LONG_NAME, self::LENGTH, self::DIGITS_COUNT, self::REGEX, self::FORMAT);
24 6
    }
25
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @see http://www.sintegra.gov.br/Cad_Estados/cad_AP.html
30
     */
31 4
    public function calculateDigit($baseNumber)
32
    {
33 4
        $number = intval($baseNumber);
34 4
        $lastDigit = $this->discoverLastDigit($number);
35 4
        $digitToReplace = $this->discoverDigitToReplace($number);
36
37 4
        $calculator = new DigitCalculator($baseNumber);
38 4
        $calculator->addDigit($lastDigit);
39 4
        $calculator->useComplementaryInsteadOfModule();
40 4
        $calculator->withMultipliersInterval(1, 9);
41 4
        $calculator->replaceWhen('0', 10);
42 4
        $calculator->replaceWhen($digitToReplace, 11);
43 4
        $calculator->withModule(DigitCalculator::MODULE_11);
44 4
        $digit = $calculator->calculate();
45
46 4
        return "{$digit}";
47
    }
48
49
    /**
50
     * Discover last digit base on number threshold.
51
     *
52
     * @param int $number
53
     *
54
     * @return string
55
     */
56 4
    private function discoverLastDigit($number)
57
    {
58 4
        $lastDigit = '0';
59 4
        if ($this->isBetweenFirstSlice($number)) {
60 4
            $lastDigit = '5';
61
        }
62 4
        if ($this->isBetweenSecondSlice($number)) {
63
            $lastDigit = '9';
64
        }
65
66 4
        return $lastDigit;
67
    }
68
69
    /**
70
     * Discover digit to be replaced based on number threshold.
71
     *
72
     * @param $number
73
     *
74
     * @return string
75
     */
76 4
    private function discoverDigitToReplace($number)
77
    {
78 4
        $replaceDigit = '0';
79 4
        if ($this->isBetweenSecondSlice($number)) {
80
            $replaceDigit = '1';
81
        }
82
83 4
        return $replaceDigit;
84
    }
85
86
    /**
87
     * Is number between 3017001 and 3019022.
88
     *
89
     * @param int $number
90
     *
91
     * @return bool
92
     */
93 4
    private function isBetweenFirstSlice($number)
94
    {
95 4
        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 4
    private function isBetweenSecondSlice($number)
106
    {
107 4
        return (3017001 <= $number && $number <= 3019022);
108
    }
109
}
110