Completed
Pull Request — master (#24)
by Antonio Oertel
02:30
created

Voter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 101
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A format() 0 4 1
A calculateDigit() 0 7 1
A calculateFirstDigit() 0 9 1
A calculateSecondDigit() 0 9 1
A getSection() 0 4 1
A getZone() 0 4 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
final class Voter extends AbstractDocument
6
{
7
    const LENGTH = 12;
8
9
    const LABEL = 'Voter';
10
11
    private $section;
12
13
    private $zone;
14
15
    /**
16
     * Voter constructor.
17
     *
18
     * @param string $number
19
     * @param string $section [optional]
20
     * @param string $zone    [optional]
21
     */
22 13
    public function __construct($number, $section = null, $zone = null)
23
    {
24 13
        $number = preg_replace('/\D/', '', $number);
25 13
        parent::__construct($number, self::LENGTH, 2, self::LABEL);
26
27 7
        $this->section = str_pad($section, 4, '0', STR_PAD_LEFT);
28 7
        $this->zone = str_pad($zone, 3, '0', STR_PAD_LEFT);
29 7
    }
30
31
    /**
32
     * Gets section.
33
     *
34
     * @return string Returns section.
35
     */
36
    public function getSection()
37
    {
38
        return $this->section;
39
    }
40
41
    /**
42
     * Gets zone.
43
     *
44
     * @return string Returns zone.
45
     */
46
    public function getZone()
47
    {
48
        return $this->zone;
49
    }
50
51
    /**
52
     * Voter does not has a specific format.
53
     *
54
     * @return string Returns only numbers.
55
     */
56 1
    public function format()
57
    {
58 1
        return "{$this}";
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 9
    public function calculateDigit($baseNumber)
65
    {
66 9
        $firstDigit = $this->calculateFirstDigit($baseNumber);
67 9
        $secondDigit = $this->calculateSecondDigit("{$baseNumber}{$firstDigit}");
68
69 9
        return "{$firstDigit}{$secondDigit}";
70
    }
71
72
    /**
73
     * Calculate check digit from base number.
74
     *
75
     * @param string $baseNumber Base numeric section to be calculate your digit.
76
     *
77
     * @return string Returns the checker digit.
78
     */
79 9
    private function calculateFirstDigit($baseNumber)
80
    {
81 9
        $calculator = new DigitCalculator(substr($baseNumber, 0, -2));
82 9
        $calculator->withMultipliers([9, 8, 7, 6, 5, 4, 3, 2]);
83 9
        $calculator->withModule(DigitCalculator::MODULE_11);
84 9
        $digit = $calculator->calculate();
85
86 9
        return "{$digit}";
87
    }
88
89
    /**
90
     * Calculate check digit from base number.
91
     *
92
     * @param string $baseNumber Base numeric section to be calculate your digit.
93
     *
94
     * @return string Returns the checker digit.
95
     */
96 9
    private function calculateSecondDigit($baseNumber)
97
    {
98 9
        $calculator = new DigitCalculator(substr($baseNumber, -3));
99 9
        $calculator->withMultipliers([9, 8, 7]);
100 9
        $calculator->withModule(DigitCalculator::MODULE_11);
101 9
        $digit = $calculator->calculate();
102
103 9
        return "{$digit}";
104
    }
105
}
106