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

SerialLengthValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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