Passed
Push — master ( 5843d6...122102 )
by Viktor
13:03
created

User   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 25
c 5
b 0
f 0
dl 0
loc 108
rs 10
wmc 13

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPosts() 0 3 1
A setLogin() 0 3 1
A __construct() 0 8 1
A addComment() 0 3 1
A validatePassword() 0 3 1
A setPassword() 0 3 1
A getCreatedAt() 0 3 1
A getLogin() 0 3 1
A getId() 0 3 2
A addPost() 0 3 1
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\Auth\IdentityInterface;
18
19
/**
20
 * @Entity(repository="App\Repository\UserRepository", mapper="Yiisoft\Yii\Cycle\Mapper\TimestampedMapper")
21
 * @Table(
22
 *     indexes={
23
 *         @Index(columns={"login"}, unique=true),
24
 *     }
25
 * )
26
 */
27
class User implements IdentityInterface
28
{
29
    /**
30
     * @Column(type="primary")
31
     */
32
    private ?int $id = null;
33
34
    /**
35
     * @Column(type="string(48)")
36
     */
37
    private string $login;
38
39
    /**
40
     * @Column(type="string")
41
     */
42
    private string $passwordHash;
43
44
    /**
45
     * Annotations for this field placed in a mapper class
46
     */
47
    private DateTimeImmutable $created_at;
48
49
    /**
50
     * Annotations for this field placed in a mapper class
51
     */
52
    private DateTimeImmutable $updated_at;
53
54
    /**
55
     * @HasMany(target="App\Blog\Entity\Post")
56
     * @var Post[]|ArrayCollection
57
     */
58
    private $posts;
59
60
    /**
61
     * @HasMany(target="App\Blog\Entity\Comment")
62
     * @var Comment[]|ArrayCollection
63
     */
64
    private $comments;
65
66
    public function __construct(string $login, string $password)
67
    {
68
        $this->login = $login;
69
        $this->created_at = new DateTimeImmutable();
70
        $this->updated_at = new DateTimeImmutable();
71
        $this->setPassword($password);
72
        $this->posts = new ArrayCollection();
73
        $this->comments = new ArrayCollection();
74
    }
75
76
    public function getId(): ?string
77
    {
78
        return $this->id === null ? null : (string)$this->id;
79
    }
80
81
    public function getLogin(): string
82
    {
83
        return $this->login;
84
    }
85
86
    public function setLogin(string $login): void
87
    {
88
        $this->login = $login;
89
    }
90
91
    public function validatePassword(string $password): bool
92
    {
93
        return (new PasswordHasher())->validate($password, $this->passwordHash);
94
    }
95
96
    public function setPassword(string $password): void
97
    {
98
        $this->passwordHash = (new PasswordHasher())->hash($password);
99
    }
100
101
    public function getCreatedAt(): DateTimeImmutable
102
    {
103
        return $this->created_at;
104
    }
105
106
    public function getUpdatedAt(): DateTimeImmutable
107
    {
108
        return $this->updated_at;
109
    }
110
111
    /**
112
     * @return Post[]
113
     */
114
    public function getPosts(): array
115
    {
116
        return $this->posts->toArray();
117
    }
118
119
    public function addPost(Post $post): void
120
    {
121
        $this->posts->add($post);
122
    }
123
124
    /**
125
     * @return Comment[]
126
     */
127
    public function getComments(): array
128
    {
129
        return $this->comments->toArray();
130
    }
131
132
    public function addComment(Comment $post): void
133
    {
134
        $this->comments->add($post);
135
    }
136
}
137