CheckDigitValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 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