Completed
Push — master ( 7049ea...a58db8 )
by Magnar Ovedal
03:09
created

NoReuse::getHashFunction()   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 0
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\Rule;
6
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
use RuntimeException;
10
use Stadly\PasswordPolice\Password;
11
use Stadly\PasswordPolice\Policy;
12
use Stadly\PasswordPolice\HashFunction\HashFunctionInterface;
13
use Stadly\PasswordPolice\WordList\WordListInterface;
14
15
final class NoReuse implements RuleInterface
16
{
17
    /**
18
     * @var HashFunctionInterface Hash function.
19
     */
20
    private $hashFunction;
21
22
    /**
23
     * @var int First former password to consider.
24
     */
25
    private $first;
26
27
    /**
28
     * @var int|null Number of former passwords to consider.
29
     */
30
    private $count;
31
32
    /**
33
     * @param int|null $count Number of former passwords to consider.
34
     * @param int $first First former password to consider.
35
     */
36 7
    public function __construct(HashFunctionInterface $hashFunction, ?int $count = null, int $first = 1)
37
    {
38 7
        if ($first < 1) {
39 2
            throw new InvalidArgumentException('First must be positive.');
40
        }
41 5
        if ($count !== null && $count < 1) {
42 2
            throw new InvalidArgumentException('Count must be positive.');
43
        }
44
45 3
        $this->hashFunction = $hashFunction;
46 3
        $this->first = $first;
47 3
        $this->count = $count;
48 3
    }
49
50
    /**
51
     * @return HashFunctionInterface Hash function.
52
     */
53 1
    public function getHashFunction(): HashFunctionInterface
54
    {
55 1
        return $this->hashFunction;
56
    }
57
58
    /**
59
     * @return int First former password to consider.
60
     */
61 1
    public function getFirst(): int
62
    {
63 1
        return $this->first;
64
    }
65
66
    /**
67
     * @return int|null Number of former passwords to consider.
68
     */
69 1
    public function getCount(): ?int
70
    {
71 1
        return $this->count;
72
    }
73
74
    /**
75
     * Check whether a password adheres to the rule.
76
     *
77
     * @param Password|string $password Password to check.
78
     * @return bool Whether the password adheres to the rule.
79
     */
80 5
    public function test($password): bool
81
    {
82 5
        if ($password instanceof Password) {
83 4
            $formerPasswords = $password->getFormerPasswords();
84
85 4
            $start = $this->first-1;
86 4
            $end = count($formerPasswords);
87 4
            if ($this->count !== null) {
88 2
                $end = min($end, $start+$this->count);
89
            }
90
91 4
            for ($i = $start; $i < $end; ++$i) {
92 4
                if ($this->hashFunction->compare((string)$password, (string)$formerPasswords[$i])) {
93 2
                    return false;
94
                }
95
            }
96
        }
97 3
        return true;
98
    }
99
100
    /**
101
     * Enforce that a password adheres to the rule.
102
     *
103
     * @param Password|string $password Password that must adhere to the rule.
104
     * @throws RuleException If the password does not adhrere to the rule.
105
     */
106 2
    public function enforce($password): void
107
    {
108 2
        if (!$this->test($password)) {
109 1
            throw new RuleException($this, $this->getMessage());
110
        }
111 1
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116 1
    public function getMessage(): string
117
    {
118 1
        $translator = Policy::getTranslator();
119
120 1
        return $translator->trans(
121 1
            'Cannot reuse former passwords.'
122
        );
123
    }
124
}
125