Completed
Push — master ( bcf360...98d48e )
by Abdala
03:37
created

PersistRuleChain::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CustomerGauge\Password;
6
7
use CustomerGauge\Password\Exception\InvalidPassword;
8
use function count;
9
10
final class PersistRuleChain
11
{
12
    /** @var callable[] */
13
    private $rules;
14
15
    /** @var InvalidPassword[] */
16
    private $exceptions = [];
17
18 2
    public function __construct(callable ...$rules)
19
    {
20 2
        $this->rules = $rules;
21 2
    }
22
23
    /**
24
     * @return InvalidPassword[]
25
     */
26
    public function exceptions() : array
27
    {
28
        return $this->exceptions;
29
    }
30
31 2
    public function __invoke(string $password) : bool
32
    {
33 2
        foreach ($this->rules as $validate) {
34
            try {
35 2
                $validate($password);
36 1
            } catch (InvalidPassword $e) {
37 2
                $this->exceptions[] = $e;
38
            }
39
        }
40
41 2
        if (count($this->exceptions)) {
42 1
            return false;
43
        }
44
45 1
        return true;
46
    }
47
}
48