Passed
Push — master ( 3d68ab...428d4b )
by Abdala
05:31
created

PersistRuleChain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 0
dl 0
loc 38
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getExceptions() 0 4 1
A __invoke() 0 16 4
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
    public function __construct(callable ...$rules)
19
    {
20
        $this->rules = $rules;
21
    }
22
23
    /**
24
     * @return InvalidPassword[]
25
     */
26
    public function getExceptions() : array
27
    {
28
        return $this->exceptions;
29
    }
30
31
    public function __invoke(string $password) : bool
32
    {
33
        foreach ($this->rules as $validate) {
34
            try {
35
                $validate($password);
36
            } catch (InvalidPassword $e) {
37
                $this->exceptions[] = $e;
38
            }
39
        }
40
41
        if (count($this->exceptions)) {
42
            return false;
43
        }
44
45
        return true;
46
    }
47
}
48