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

Password   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 55
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 3 1
A toNative() 0 3 1
A verifies() 0 3 1
A fromHash() 0 6 1
A __construct() 0 2 1
A fromPlainText() 0 14 2
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