CheckDigitValidator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
wmc 4
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
calculateCheckDigit() 0 1 ?
A validate() 0 14 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking\Validator;
6
7
use byrokrat\banking\AccountNumber;
8
9
/**
10
 * Validate check digits
11
 */
12
abstract class CheckDigitValidator implements ValidatorInterface
13
{
14 187
    public function validate(AccountNumber $number): ResultInterface
15
    {
16 187
        if ($number->getCheckDigit() === '' || $number->getSerialNumber() === '') {
17 1
            return new Failure('Unable to validate checkdigit, not enough digits');
18
        }
19
20 187
        $expected = $this->calculateCheckDigit($number);
21
22 187
        if ($number->getCheckDigit() == $expected) {
23 146
            return new Success("Valid check digit $expected");
24
        }
25
26 116
        return new Failure("Invalid check digit {$number->getCheckDigit()}, expected $expected");
27
    }
28
29
    /**
30
     * Calculate the expected checkdigit
31
     */
32
    abstract protected function calculateCheckDigit(AccountNumber $number): string;
33
}
34