Completed
Branch v2.0.0 (e47d62)
by Alexander
03:40
created

User   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 3
cbo 3
dl 0
loc 93
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A buildPasswordHash() 0 7 1
A doesPasswordMatch() 0 6 1
A setUserId() 0 12 2
A setPassword() 0 6 1
A setName() 0 6 1
A setEmail() 0 6 1
A getUserID() 0 4 1
A getEmail() 0 4 1
A getName() 0 4 1
A passwordDefined() 0 4 2
A generatePasswordHash() 0 6 1
A generateSalt() 0 6 1
1
<?php
2
/**
3
 * @author Alexander Torosh <[email protected]>
4
 */
5
6
namespace Domain\User;
7
8
use Domain\User\Exceptions\UserException;
9
use Domain\User\Specifications\UserPasswordSpecification;
10
use Domain\User\Specifications\UserSpecification;
11
12
class User
13
{
14
    private $userID = 0;
15
    private $email = '';
16
    private $name = '';
17
    private $password = '';
18
19
    public function buildPasswordHash(): string
20
    {
21
        $passwordSpecification = new UserPasswordSpecification($this->password);
22
        $passwordSpecification->validate();
23
24
        return $this->generatePasswordHash();
25
    }
26
27
    /**
28
     * @throws DomainException
29
     */
30
    public function doesPasswordMatch(int $userID, string $inputPassword): bool
31
    {
32
        $user = $this->repository->fetchUser($userID);
0 ignored issues
show
Bug introduced by
The property repository does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
33
34
        return password_verify($inputPassword, $user->password_hash);
35
    }
36
37
    public function setUserId(int $userID): User
38
    {
39
        if ($this->userID > 0) {
40
            throw new UserException('Identifier `userID` is already defined.');
41
        }
42
        $this->userID = $userID;
43
44
        $specification = new UserSpecification($this);
45
        $specification->validateIdentifier();
46
47
        return $this;
48
    }
49
50
    public function setPassword(string $password): User
51
    {
52
        $this->password = $password;
53
54
        return $this;
55
    }
56
57
    public function setName(string $name): User
58
    {
59
        $this->name = $name;
60
61
        return $this;
62
    }
63
64
    public function setEmail(string $email): User
65
    {
66
        $this->email = $email;
67
68
        return $this;
69
    }
70
71
    public function getUserID(): int
72
    {
73
        return $this->userID;
74
    }
75
76
    public function getEmail(): string
77
    {
78
        return $this->email;
79
    }
80
81
    public function getName(): string
82
    {
83
        return $this->name;
84
    }
85
86
    public function passwordDefined(): bool
87
    {
88
        return $this->password ? true : false;
89
    }
90
91
    private function generatePasswordHash(): string
92
    {
93
        return password_hash($this->password, PASSWORD_ARGON2I, [
94
            'salt' => $this->generateSalt(),
95
        ]);
96
    }
97
98
    private function generateSalt(): string
99
    {
100
        $length = random_int(16, 32);
101
102
        return substr(md5($this->email.'_'.microtime()), 0, $length);
103
    }
104
}
105