Completed
Push — feature/VSVGVQ-24 ( 890c4d...a3069c )
by Luc
05:52 queued 02:46
created

Password::verifies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace VSV\GVQ_API\User\ValueObjects;
4
5
class Password
6
{
7
    /**
8
     * @var string
9
     */
10
    private $value;
11
12
    private function __construct()
13
    {
14
    }
15
16
    public static function fromHash(string $hash)
17
    {
18
        $password = new Password();
19
        $password->setValue($hash);
20
21
        return $password;
22
    }
23
24
    public static function fromPlainText(string $plainTextValue)
25
    {
26
        if (!preg_match('/^(?=[^ ])(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])(.{8,})(?<=\S)$/', $plainTextValue)) {
27
            throw new \InvalidArgumentException(
28
                'Invalid value for password. '.
29
                'Must be at least 8 characters long, contain at least one lowercase, '.
30
                'one uppercase and one non-alphabetical character and must not start or end with a space.'
31
            );
32
        }
33
34
        $password = new Password();
35
        $password->setValue(password_hash($plainTextValue, PASSWORD_DEFAULT));
36
37
        return $password;
38
    }
39
40
    private function setValue(string $value)
41
    {
42
        $this->value = $value;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function toNative(): string
49
    {
50
        return $this->value;
51
    }
52
53
    /**
54
     * @param string $plainTextValue
55
     * @return bool
56
     */
57
    public function verifies(string $plainTextValue): bool
58
    {
59
        return password_verify($plainTextValue, $this->toNative());
60
    }
61
}
62