Passed
Pull Request — master (#23)
by Alexander
12:41
created

User::setPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Entity;
4
5
use Cycle\Annotated\Annotation\Column;
6
use Cycle\Annotated\Annotation\Entity;
7
use Cycle\Annotated\Annotation\Table;
8
use Cycle\Annotated\Annotation\Table\Index;
9
use Yiisoft\Security\PasswordHasher;
10
use Yiisoft\Security\Random;
11
use Yiisoft\Auth\IdentityInterface;
12
13
/**
14
 * @Entity(repository="App\Repository\UserRepository")
15
 * @Table(
16
 *     indexes={
17
 *         @Index(columns={"login"}, unique=true),
18
 *         @Index(columns={"token"}, unique=true)
19
 *     }
20
 * )
21
 */
22
class User implements IdentityInterface
23
{
24
    /**
25
     * @Column(type="primary")
26
     * @var int
27
     */
28
    private $id;
29
30
    /**
31
     * @Column(type="string(128)")
32
     * @var string
33
     */
34
    private $token;
35
36
    /**
37
     * @Column(type="string(48)")
38
     * @var string
39
     */
40
    private $login;
41
42
    /**
43
     * @Column(type="string")
44
     * @var string
45
     */
46
    private $passwordHash;
47
48
    public function __construct()
49
    {
50
        if (!isset($this->token)) {
51
            $this->resetToken();
52
        }
53
    }
54
55
    public function getId(): ?string
56
    {
57
        return $this->id;
58
    }
59
60
    public function getToken(): ?string
61
    {
62
        return $this->token;
63
    }
64
65
    public function resetToken(): void
66
    {
67
        $this->token = Random::string(128);
68
    }
69
70
    public function getLogin(): string
71
    {
72
        return $this->login;
73
    }
74
75
    public function setLogin(string $login): void
76
    {
77
        $this->login = $login;
78
    }
79
80
    public function validatePassword(string $password): bool
81
    {
82
        if ($this->passwordHash === null) {
83
            return false;
84
        }
85
        return (new PasswordHasher())->validate($password, $this->passwordHash);
86
    }
87
88
    public function setPassword(string $password): void
89
    {
90
        $this->passwordHash = (new PasswordHasher())->hash($password);
91
    }
92
}
93