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

SerialLengthValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 18 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace byrokrat\banking\Validator;
6
7
use byrokrat\banking\AccountNumber;
8
9
/**
10
 * Validate length of serial number
11
 */
12
class SerialLengthValidator implements ValidatorInterface
13
{
14
    /**
15
     * @var integer
16
     */
17
    private $min;
18
19
    /**
20
     * @var integer
21
     */
22
    private $max;
23
24 92
    public function __construct(int $min, int $max)
25
    {
26 92
        $this->min = $min;
27 92
        $this->max = $max;
28 92
    }
29
30 190
    public function validate(AccountNumber $number): ResultInterface
31
    {
32 190
        $len = strlen($number->getSerialNumber());
33
34 190
        if ($len < $this->min) {
35 45
            return new Failure(
36 45
                "Invalid serial length $len, must be at least {$this->min}"
37
            );
38
        }
39
40 182
        if ($len > $this->max) {
41 130
            return new Failure(
42 130
                "Invalid serial length $len, must not be longer than {$this->max}"
43
            );
44
        }
45
46 178
        return new Success("Valid serial length $len");
47
    }
48
}
49