Passed
Push — master ( 44cfae...d43138 )
by Magnar Ovedal
02:40
created

Length::enforce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\Rule;
6
7
use InvalidArgumentException;
8
use Symfony\Component\Translation\Translator;
9
10
final class Length implements RuleInterface
11
{
12
    /**
13
     * @var int Minimum password length.
14
     */
15
    private $min;
16
17
    /**
18
     * @var int|null Maximum password length.
19
     */
20
    private $max;
21
22
    /**
23
     * @param int $min Minimum password length.
24
     * @param int|null $max Maximum password length.
25
     */
26 7
    public function __construct(int $min, ?int $max = null)
27
    {
28 7
        if ($min < 0) {
29 1
            throw new InvalidArgumentException('Min cannot be negative.');
30
        }
31 6
        if ($max !== null && $max < $min) {
32 1
            throw new InvalidArgumentException('Max cannot be smaller than min.');
33
        }
34 5
        if ($min === 0 && $max === null) {
35 1
            throw new InvalidArgumentException('Min cannot be zero when max is unconstrained.');
36
        }
37
38 4
        $this->min = $min;
39 4
        $this->max = $max;
40 4
    }
41
42
    /**
43
     * @return int Minimum password length.
44
     */
45 1
    public function getMin(): int
46
    {
47 1
        return $this->min;
48
    }
49
50
    /**
51
     * @return int|null Maximum password length.
52
     */
53 1
    public function getMax(): ?int
54
    {
55 1
        return $this->max;
56
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 5
    public function test(string $password): bool
62
    {
63 5
        if ($this->getCount($password) < $this->min) {
64 1
            return false;
65
        }
66
67 4
        if (null !== $this->max && $this->max < $this->getCount($password)) {
68 1
            return false;
69
        }
70
71 3
        return true;
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77 2
    public function enforce(string $password, Translator $translator): void
78
    {
79 2
        if (!$this->test($password)) {
80 1
            throw new RuleException($this, $this->getMessage($translator));
81
        }
82 1
    }
83
84
    /**
85
     * {@inheritDoc}
86
     */
87 5
    public function getMessage(Translator $translator): string
88
    {
89 5
        if ($this->getMax() === null) {
90 1
            return $translator->transChoice(
91
                'There must be at least one character.|'.
92 1
                'There must be at least %count% characters.',
93 1
                $this->getMin()
94
            );
95
        }
96
97 4
        if ($this->getMax() === 0) {
98 1
            return $translator->trans(
99 1
                'There must be no characters.'
100
            );
101
        }
102
103 3
        if ($this->getMin() === 0) {
104 1
            return $translator->transChoice(
105
                'There must be at most one character.|'.
106 1
                'There must be at most %count% characters.',
107 1
                $this->getMax()
108
            );
109
        }
110
111 2
        if ($this->getMin() === $this->getMax()) {
112 1
            return $translator->transChoice(
113
                'There must be exactly one character.|'.
114 1
                'There must be exactly %count% characters.',
115 1
                $this->getMin()
116
            );
117
        }
118
119 1
        return $translator->trans(
120 1
            'There must be between %min% and %max% characters.',
121 1
            ['%min%' => $this->getMin(), '%max%' => $this->getMax()]
122
        );
123
    }
124
125
    /**
126
     * @param string $password Password to count characters in.
127
     * @return int Number of characters.
128
     */
129 7
    private function getCount(string $password): int
130
    {
131 7
        return mb_strlen($password);
132
    }
133
}
134