Passed
Push — master ( 6af486...6842d1 )
by Magnar Ovedal
02:23
created

Policy::setTranslator()   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
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice;
6
7
use Stadly\PasswordPolice\Rule\RuleException;
8
use Stadly\PasswordPolice\Rule\RuleInterface;
9
use Stadly\PasswordPolice\Rule\TestException;
10
use Symfony\Component\Translation\Translator;
11
use Symfony\Component\Translation\TranslatorInterface;
12
13
final class Policy
14
{
15
    /**
16
     * @var RuleInterface[] Policy rules.
17
     */
18
    private $rules = [];
19
20
    /**
21
     * @var TranslatorInterface|null Translator for translating messages.
22
     */
23
    private static $translator;
24
25
    /**
26
     * @param RuleInterface... $rules Policy rules
0 ignored issues
show
Documentation Bug introduced by
The doc comment RuleInterface... at position 0 could not be parsed: Unknown type name 'RuleInterface...' at position 0 in RuleInterface....
Loading history...
27
     */
28 3
    public function __construct(RuleInterface... $rules)
29
    {
30 3
        $this->addRules(...$rules);
31 3
    }
32
33
    /**
34
     * @param RuleInterface... $rules Policy rules
0 ignored issues
show
Documentation Bug introduced by
The doc comment RuleInterface... at position 0 could not be parsed: Unknown type name 'RuleInterface...' at position 0 in RuleInterface....
Loading history...
35
     */
36 3
    public function addRules(RuleInterface... $rules): void
37
    {
38 3
        foreach ($rules as $rule) {
39 3
            $this->rules[] = $rule;
40
        }
41 3
    }
42
43
    /**
44
     * Check whether a password adheres to the policy.
45
     *
46
     * @param string $password Password to check.
47
     * @return bool Whether the password adheres to the policy.
48
     * @throws TestException If an error occurred while checking the password.
49
     */
50 5
    public function test(string $password): bool
51
    {
52 5
        foreach ($this->rules as $rule) {
53 4
            if (!$rule->test($password)) {
54 4
                return false;
55
            }
56
        }
57
58 3
        return true;
59
    }
60
61
    /**
62
     * Enforce that a password adheres to the policy.
63
     *
64
     * @param string $password Password that must adhere to the policy.
65
     * @throws PolicyException If the password does not adhrere to the policy.
66
     * @throws TestException If an error occurred while checking the password.
67
     */
68 2
    public function enforce(string $password): void
69
    {
70 2
        $exceptions = [];
71
72 2
        foreach ($this->rules as $rule) {
73
            try {
74 1
                $rule->enforce($password);
75 1
            } catch (RuleException $exception) {
76 1
                $exceptions[] = $exception;
77
            }
78
        }
79
80 2
        if ($exceptions !== []) {
81 1
            throw new PolicyException($this, $exceptions);
82
        }
83 1
    }
84
85
    /**
86
     * @param TranslatorInterface|null $translator Translator for translating messages.
87
     */
88 2
    public static function setTranslator(?TranslatorInterface $translator): void
89
    {
90 2
        self::$translator = $translator;
91 2
    }
92
93
    /**
94
     * @return TranslatorInterface Translator for translating messages.
95
     */
96 2
    public static function getTranslator(): TranslatorInterface
97
    {
98 2
        if (null === self::$translator) {
99 1
            self::$translator = new Translator('en_US');
100
        }
101 2
        return self::$translator;
102
    }
103
}
104