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

CheckDigitValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 39
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
processNumber() 0 1 ?
A __construct() 0 4 1
A validate() 0 6 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