Password::getFormerPasswords()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice;
6
7
use DateTimeInterface;
8
use StableSort\StableSort;
9
10
final class Password
11
{
12
    /**
13
     * @var string Password.
14
     */
15
    private $password;
16
17
    /**
18
     * @var array<string|DateTimeInterface> Guessable data.
19
     */
20
    private $guessableData;
21
22
    /**
23
     * @var array<FormerPassword> Former passwords, ordered by recentness.
24
     */
25
    private $formerPasswords = [];
26
27
    /**
28
     * @param string $password Password.
29
     * @param array<string|DateTimeInterface> $guessableData Guessable data.
30
     * @param array<FormerPassword> $formerPasswords Former passwords. Can be unordered.
31
     */
32 10
    public function __construct(string $password, array $guessableData = [], array $formerPasswords = [])
33
    {
34 10
        $this->password = $password;
35 10
        $this->guessableData = $guessableData;
36 10
        $this->addFormerPasswords(...$formerPasswords);
37 10
    }
38
39
    /**
40
     * @return string Password.
41
     */
42 1
    public function __toString(): string
43
    {
44 1
        return $this->password;
45
    }
46
47
    /**
48
     * @return string Password.
49
     */
50 1
    public function getPassword(): string
51
    {
52 1
        return $this->password;
53
    }
54
55
    /**
56
     * @param string|DateTimeInterface ...$guessableData Guessable data.
57
     */
58 1
    public function addGuessableData(...$guessableData): void
59
    {
60 1
        $this->guessableData = array_merge($this->guessableData, $guessableData);
61 1
    }
62
63
    /**
64
     * @return array<string|DateTimeInterface> Guessable data.
65
     */
66 1
    public function getGuessableData(): array
67
    {
68 1
        return $this->guessableData;
69
    }
70
71 1
    public function clearGuessableData(): void
72
    {
73 1
        $this->guessableData = [];
74 1
    }
75
76
    /**
77
     * @param FormerPassword ...$formerPasswords Former passwords. Can be unordered.
78
     */
79 1
    public function addFormerPasswords(FormerPassword ...$formerPasswords): void
80
    {
81 1
        $this->formerPasswords = array_merge($this->formerPasswords, $formerPasswords);
82
83
        StableSort::usort($this->formerPasswords, static function (FormerPassword $a, FormerPassword $b): int {
84 1
            return $b->getDate() <=> $a->getDate();
85 1
        });
86 1
    }
87
88
    /**
89
     * @return array<FormerPassword> Former passwords, ordered by recentness.
90
     */
91 3
    public function getFormerPasswords(): array
92
    {
93 3
        return $this->formerPasswords;
94
    }
95
96 1
    public function clearFormerPasswords(): void
97
    {
98 1
        $this->formerPasswords = [];
99 1
    }
100
}
101