Completed
Push — master ( 70433e...08bb6a )
by Alexander
19s queued 13s
created

User   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
c 3
b 0
f 0
dl 0
loc 69
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setLogin() 0 3 1
A __construct() 0 4 2
A getLogin() 0 3 1
A getToken() 0 3 1
A resetToken() 0 3 1
A getId() 0 3 1
A validatePassword() 0 6 2
A setPassword() 0 3 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