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

ClearingCheckDigitValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 35
ccs 0
cts 12
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A validate() 0 8 3
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