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

PersonalIdValidator::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
crap 12
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\banking\Exception\InvalidAccountNumberException;
9
use byrokrat\id\PersonalId;
10
11
/**
12
 * Validate personal ids
13
 */
14
class PersonalIdValidator implements Validator
15
{
16
    /**
17
     * Validate that number is a swedish personal id
18
     *
19
     * @param  AccountNumber $number
20
     * @return null
21
     * @throws InvalidCheckDigitException    If checkdigit is not valid
22
     * @throws InvalidAccountNumberException If number is not a valid personal id
23
     */
24
    public function validate(AccountNumber $number)
25
    {
26
        try {
27
            new PersonalId($number->getSerialNumber() . $number->getCheckDigit());
28
        } catch (\byrokrat\id\Exception\InvalidCheckDigitException $e) {
29
            throw new InvalidCheckDigitException("Invalid check digit {$number->getCheckDigit()} in $number", 0, $e);
30
        } catch (\byrokrat\id\Exception\RuntimeException $e) {
31
            throw new InvalidAccountNumberException("Account number $number is not a valid personal id number", 0, $e);
32
        }
33
    }
34
}
35