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

Amapa   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 93.75%

Importance

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

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