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

ClearingCheckDigitValidator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 6
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\Modulo10;
9
10
/**
11
 * Validate clearing number check digits
12
 */
13
class ClearingCheckDigitValidator implements Validator
14
{
15
    /**
16
     * @var Modulo10 Checksum calculator
17
     */
18
    private $checksum;
19
20
    /**
21
     * Load checksum calculator
22
     *
23
     * @param Modulo10 $checksum
24
     */
25
    public function __construct(Modulo10 $checksum = null)
26
    {
27
        $this->checksum = $checksum ?: new Modulo10;
28
    }
29
30
    /**
31
     * Validate clearing number check digit
32
     *
33
     * If no clearing check digit exists number is considered valid.
34
     *
35
     * @param  AccountNumber $number
36
     * @return null
37
     * @throws InvalidCheckDigitException If check digit is not valid
38
     */
39
    public function validate(AccountNumber $number)
40
    {
41
        if ($checkDigit = $number->getClearingCheckDigit()) {
42
            if (!$this->checksum->isValid($number->getClearingNumber() . $checkDigit)) {
43
                throw new InvalidCheckDigitException("Invalid clearing number check digit in $number");
44
            }
45
        }
46
    }
47
}
48