Completed
Pull Request — master (#13)
by
unknown
02:23
created

CheckDigitValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace byrokrat\banking\Validator;
4
5
use byrokrat\banking\Validator;
6
use byrokrat\banking\AccountNumber;
7
use byrokrat\banking\Exception\InvalidCheckDigitException;
8
use byrokrat\checkdigit\Calculator;
9
10
/**
11
 * Validate check digits
12
 */
13
abstract class CheckDigitValidator implements Validator
14
{
15
    /**
16
     * @var Calculator Checksum calculator
17
     */
18
    private $checksum;
19
20
    /**
21
     * Load checksum calculator
22
     *
23
     * @param Calculator $checksum
24
     */
25
    public function __construct(Calculator $checksum)
26
    {
27
        $this->checksum = $checksum;
28
    }
29
30
    /**
31
     * Validate check digit
32
     *
33
     * @param  AccountNumber $number
34
     * @return null
35
     * @throws InvalidCheckDigitException If check digit is not valid
36
     */
37
    public function validate(AccountNumber $number)
38
    {
39
        if (!$this->checksum->isValid($this->processNumber($number))) {
40
            throw new InvalidCheckDigitException("Invalid check digit in $number");
41
        }
42
    }
43
44
    /**
45
     * Get number to validate
46
     *
47
     * @param  AccountNumber $number
48
     * @return string
49
     */
50
    abstract protected function processNumber(AccountNumber $number);
51
}
52