1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Entity; |
6
|
|
|
|
7
|
|
|
use App\Blog\Entity\Comment; |
8
|
|
|
use App\Blog\Entity\Post; |
9
|
|
|
use Cycle\Annotated\Annotation\Column; |
10
|
|
|
use Cycle\Annotated\Annotation\Entity; |
11
|
|
|
use Cycle\Annotated\Annotation\Relation\HasMany; |
12
|
|
|
use Cycle\Annotated\Annotation\Table; |
13
|
|
|
use Cycle\Annotated\Annotation\Table\Index; |
14
|
|
|
use DateTimeImmutable; |
15
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
16
|
|
|
use Yiisoft\Security\PasswordHasher; |
17
|
|
|
use Yiisoft\Security\Random; |
18
|
|
|
use Yiisoft\Auth\IdentityInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @Entity(repository="App\Repository\UserRepository", mapper="Yiisoft\Yii\Cycle\Mapper\TimestampedMapper") |
22
|
|
|
* @Table( |
23
|
|
|
* indexes={ |
24
|
|
|
* @Index(columns={"login"}, unique=true), |
25
|
|
|
* @Index(columns={"token"}, unique=true) |
26
|
|
|
* } |
27
|
|
|
* ) |
28
|
|
|
*/ |
29
|
|
|
class User implements IdentityInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @Column(type="primary") |
33
|
|
|
*/ |
34
|
|
|
private ?int $id = null; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @Column(type="string(128)") |
38
|
|
|
*/ |
39
|
|
|
private string $token; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @Column(type="string(48)") |
43
|
|
|
*/ |
44
|
|
|
private string $login; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @Column(type="string") |
48
|
|
|
*/ |
49
|
|
|
private string $passwordHash; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Annotations for this field placed in a mapper class |
53
|
|
|
*/ |
54
|
|
|
private DateTimeImmutable $created_at; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Annotations for this field placed in a mapper class |
58
|
|
|
*/ |
59
|
|
|
private DateTimeImmutable $updated_at; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @HasMany(target="App\Blog\Entity\Post") |
63
|
|
|
* @var Post[]|ArrayCollection |
64
|
|
|
*/ |
65
|
|
|
private $posts; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @HasMany(target="App\Blog\Entity\Comment") |
69
|
|
|
* @var Comment[]|ArrayCollection |
70
|
|
|
*/ |
71
|
|
|
private $comments; |
72
|
|
|
|
73
|
|
|
public function __construct(string $login, string $password) |
74
|
|
|
{ |
75
|
|
|
$this->login = $login; |
76
|
|
|
$this->created_at = new DateTimeImmutable(); |
77
|
|
|
$this->updated_at = new DateTimeImmutable(); |
78
|
|
|
$this->setPassword($password); |
79
|
|
|
$this->resetToken(); |
80
|
|
|
$this->posts = new ArrayCollection(); |
81
|
|
|
$this->comments = new ArrayCollection(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getId(): ?string |
85
|
|
|
{ |
86
|
|
|
return $this->id === null ? null : (string)$this->id; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getToken(): ?string |
90
|
|
|
{ |
91
|
|
|
return $this->token; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function resetToken(): void |
95
|
|
|
{ |
96
|
|
|
$this->token = Random::string(128); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getLogin(): string |
100
|
|
|
{ |
101
|
|
|
return $this->login; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function setLogin(string $login): void |
105
|
|
|
{ |
106
|
|
|
$this->login = $login; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function validatePassword(string $password): bool |
110
|
|
|
{ |
111
|
|
|
return (new PasswordHasher())->validate($password, $this->passwordHash); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function setPassword(string $password): void |
115
|
|
|
{ |
116
|
|
|
$this->passwordHash = (new PasswordHasher())->hash($password); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getCreatedAt(): DateTimeImmutable |
120
|
|
|
{ |
121
|
|
|
return $this->created_at; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getUpdatedAt(): DateTimeImmutable |
125
|
|
|
{ |
126
|
|
|
return $this->updated_at; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return ArrayCollection|Post[] |
131
|
|
|
*/ |
132
|
|
|
public function getPosts(): ArrayCollection |
133
|
|
|
{ |
134
|
|
|
return $this->posts; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function addPost(Post $post): void |
138
|
|
|
{ |
139
|
|
|
$this->posts->add($post); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return ArrayCollection|Comment[] |
144
|
|
|
*/ |
145
|
|
|
public function getComments(): ArrayCollection |
146
|
|
|
{ |
147
|
|
|
return $this->comments; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function addComment(Comment $post): void |
151
|
|
|
{ |
152
|
|
|
$this->comments->add($post); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|