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

PersonalIdValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 3
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