Completed
Push — master ( 83241a...8e03fe )
by Anthony
03:35
created

PasswordValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 48
c 0
b 0
f 0
ccs 0
cts 21
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addValidator() 0 4 1
A getValidators() 0 4 1
A validate() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Porthou\Password;
5
6
class PasswordValidator
7
{
8
    /** @var Validator[] $validators */
9
    private $validators = [];
10
11
    public function __construct(iterable $validators = [])
12
    {
13
        foreach ($validators as $validator) {
14
            $this->addValidator($validator);
15
        }
16
    }
17
18
    /**
19
     * Adds a validator to the current PasswordValidator instance
20
     *
21
     * @param Validator $validator
22
     */
23
    public function addValidator(Validator $validator): void
24
    {
25
        $this->validators[] = $validator;
26
    }
27
28
    /**
29
     * Gets an array of the currently applied Validators
30
     *
31
     * @return Validator[]
32
     */
33
    public function getValidators(): array
34
    {
35
        return $this->validators;
36
    }
37
38
    /**
39
     * Validates the given password against the current rule set.
40
     *
41
     * @param string $password
42
     * @return bool True if the password is valid
43
     * @throws PasswordException If the password fails a validation attempt.
44
     */
45
    public function validate(string $password): bool
46
    {
47
        foreach ($this->validators as $validator) {
48
            $validator->validate($password);
49
        }
50
51
        return true;
52
    }
53
}
54