Completed
Push — master ( 5d18c7...cabcf3 )
by Hannes
16s queued 13s
created

ClearingCheckDigitValidator::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking\Validator;
6
7
use byrokrat\banking\AccountNumber;
8
9
/**
10
 * Validate clearing number check digits
11
 */
12
class ClearingCheckDigitValidator implements ValidatorInterface
13
{
14
    /**
15
     * Validate clearing number check digit
16
     *
17
     * If no clearing check digit exists number is considered valid.
18
     */
19 38
    public function validate(AccountNumber $number): ResultInterface
20
    {
21 38
        $checkDigit = $number->getClearingCheckDigit();
22
23 38
        if ($checkDigit != '') {
24 36
            $expected = Modulo10::calculateCheckDigit($number->getClearingNumber());
25
26 36
            if ($expected != $checkDigit) {
27 28
                return new Failure("Invalid clearing number check digit $checkDigit, expected $expected");
28
            }
29
30 11
            return new Success("Valid clearing number check digit $checkDigit");
31
        }
32
33 32
        return new Success("Skipped validation of clearing number check digit");
34
    }
35
}
36