Passed
Pull Request — master (#186)
by Alexander
04:24
created

User::getCreatedAt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\User;
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\Auth\IdentityInterface;
17
use Yiisoft\Security\PasswordHasher;
18
19
/**
20
 * @Entity(repository="App\User\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
     *
57
     * @var ArrayCollection|Post[]
58
     */
59
    private $posts;
60
61
    /**
62
     * @HasMany(target="App\Blog\Entity\Comment")
63
     *
64
     * @var ArrayCollection|Comment[]
65
     */
66
    private $comments;
67
68
    public function __construct(string $login, string $password)
69
    {
70
        $this->login = $login;
71
        $this->created_at = new DateTimeImmutable();
72
        $this->updated_at = new DateTimeImmutable();
73
        $this->setPassword($password);
74
        $this->posts = new ArrayCollection();
75
        $this->comments = new ArrayCollection();
76
    }
77
78
    public function getId(): ?string
79
    {
80
        return $this->id === null ? null : (string)$this->id;
81
    }
82
83
    public function getLogin(): string
84
    {
85
        return $this->login;
86
    }
87
88
    public function setLogin(string $login): void
89
    {
90
        $this->login = $login;
91
    }
92
93
    public function validatePassword(string $password): bool
94
    {
95
        return (new PasswordHasher())->validate($password, $this->passwordHash);
96
    }
97
98
    public function setPassword(string $password): void
99
    {
100
        $this->passwordHash = (new PasswordHasher())->hash($password);
101
    }
102
103
    public function getCreatedAt(): DateTimeImmutable
104
    {
105
        return $this->created_at;
106
    }
107
108
    public function getUpdatedAt(): DateTimeImmutable
109
    {
110
        return $this->updated_at;
111
    }
112
113
    /**
114
     * @return Post[]
115
     */
116
    public function getPosts(): array
117
    {
118
        return $this->posts->toArray();
119
    }
120
121
    public function addPost(Post $post): void
122
    {
123
        $this->posts->add($post);
124
    }
125
126
    /**
127
     * @return Comment[]
128
     */
129
    public function getComments(): array
130
    {
131
        return $this->comments->toArray();
132
    }
133
134
    public function addComment(Comment $post): void
135
    {
136
        $this->comments->add($post);
137
    }
138
}
139