Passed
Push — master ( 193ce8...245a1c )
by Antonio Oertel
05:00 queued 02:20
created

Cns::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Brazanation\Documents;
4
5
use Brazanation\Documents\Cns\CnsCalculator;
6
use Brazanation\Documents\Cns\TemporaryCalculator;
7
8
final class Cns extends AbstractDocument
9
{
10
    const LENGTH = 15;
11
12
    const LABEL = 'CNS';
13
14
    const REGEX = '/^([\d]{3})([\d]{4})([\d]{4})([\d]{4})$/';
15
16
    const FORMAT = '$1 $2 $3 $4';
17
18
    const DIGIT_COUNT = 1;
19
20
    /**
21
     * CNS constructor.
22
     *
23
     * @param string $number
24
     */
25 15
    public function __construct($number)
26
    {
27 15
        $number = preg_replace('/\D/', '', $number);
28 15
        parent::__construct($number, self::LENGTH, self::DIGIT_COUNT, self::LABEL);
29 9
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function format()
35
    {
36 1
        return preg_replace(self::REGEX, self::FORMAT, "{$this}");
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * Based on given number, it will decide what kind of calculator will use.
43
     *
44
     * For numbers starting with 7, 8 or 9 will use TemporaryCalculator,
45
     * otherwise CnsCalculator.
46
     */
47 11
    public function calculateDigit($baseNumber)
48
    {
49 11
        $calculator = new CnsCalculator();
50
51 11
        if (in_array(substr($baseNumber, 0, 1), [7, 8, 9])) {
52 1
            $calculator = new TemporaryCalculator();
53
        }
54
55 11
        $digit = $calculator->calculateDigit($baseNumber);
56
57 11
        return "{$digit}";
58
    }
59
}
60