PersistRuleChain   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 2
b 0
f 0
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A exceptions() 0 3 1
A __invoke() 0 13 3
A __construct() 0 3 1
1
<?php
2
/*
3
    Password Strength Library
4
    Copyright (C) 2019 CustomerGauge
5
    [email protected]
6
7
    This program is free software; you can redistribute it and/or
8
    modify it under the terms of the GNU Lesser General Public
9
    License as published by the Free Software Foundation; either
10
    version 3 of the License, or (at your option) any later version.
11
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
    Lesser General Public License for more details.
16
17
    You should have received a copy of the GNU Lesser General Public License
18
    along with this program; if not, write to the Free Software Foundation,
19
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
*/
21
22
declare(strict_types=1);
23
24
namespace CustomerGauge\Password;
25
26
use CustomerGauge\Password\Exception\InvalidPassword;
27
28
use function count;
29
30
final class PersistRuleChain
31
{
32
    /** @var callable[] */
33
    private array $rules;
34
35
    /** @var InvalidPassword[] */
36
    private array $exceptions = [];
37
38 2
    public function __construct(callable ...$rules)
39
    {
40 2
        $this->rules = $rules;
41 2
    }
42
43
    /**
44
     * @return InvalidPassword[]
45
     */
46 1
    public function exceptions(): array
47
    {
48 1
        return $this->exceptions;
49
    }
50
51 2
    public function __invoke(string $password): bool
52
    {
53 2
        $this->exceptions = [];
54
55 2
        foreach ($this->rules as $validate) {
56
            try {
57 2
                $validate($password);
58 1
            } catch (InvalidPassword $e) {
59 1
                $this->exceptions[] = $e;
60
            }
61
        }
62
63 2
        return count($this->exceptions) === 0;
64
    }
65
}
66