Passed
Pull Request — master (#408)
by Wilmer
05:31
created

User::addComment()   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 1
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\Auth\Identity;
8
use App\Blog\Entity\Comment;
9
use App\Blog\Entity\Post;
10
use Cycle\Annotated\Annotation\Column;
11
use Cycle\Annotated\Annotation\Entity;
12
use Cycle\Annotated\Annotation\Relation\HasMany;
13
use Cycle\Annotated\Annotation\Relation\HasOne;
14
use Cycle\Annotated\Annotation\Table;
15
use Cycle\Annotated\Annotation\Table\Index;
16
use DateTimeImmutable;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Yiisoft\Security\PasswordHasher;
19
20
/**
21
 * @Entity(repository="App\User\UserRepository")
22
 * @Table(
23
 *     indexes={
24
 *         @Index(columns={"login"}, unique=true),
25
 *     }
26
 * )
27
 */
28
class User
29
{
30
    /**
31
     * @Column(type="primary")
32
     */
33
    private ?int $id = null;
34
35
    /**
36
     * @Column(type="string(48)")
37
     */
38
    private string $login;
39
40
    /**
41
     * @Column(type="string")
42
     */
43
    private string $passwordHash;
44
45
    /**
46
     * @Column(type="datetime")
47
     */
48
    private DateTimeImmutable $created_at;
49
50
    /**
51
     * @Column(type="datetime")
52
     */
53
    private DateTimeImmutable $updated_at;
54
55
    /**
56
     * @HasOne(target="App\Auth\Identity")
57
     *
58
     * @var \Cycle\ORM\Promise\Reference|Identity
59
     */
60
    private $identity;
61
62
    /**
63
     * @HasMany(target="App\Blog\Entity\Post")
64
     *
65
     * @var ArrayCollection|Post[]
66
     */
67
    private $posts;
68
69
    /**
70
     * @HasMany(target="App\Blog\Entity\Comment")
71
     *
72
     * @var ArrayCollection|Comment[]
73
     */
74
    private $comments;
75
76 1
    public function __construct(string $login, string $password)
77
    {
78 1
        $this->login = $login;
79 1
        $this->created_at = new DateTimeImmutable();
80 1
        $this->updated_at = new DateTimeImmutable();
81 1
        $this->setPassword($password);
82 1
        $this->identity = new Identity();
83 1
        $this->posts = new ArrayCollection();
84 1
        $this->comments = new ArrayCollection();
85 1
    }
86
87 1
    public function getId(): ?string
88
    {
89 1
        return $this->id === null ? null : (string)$this->id;
90
    }
91
92 2
    public function getLogin(): string
93
    {
94 2
        return $this->login;
95
    }
96
97
    public function setLogin(string $login): void
98
    {
99
        $this->login = $login;
100
    }
101
102 1
    public function validatePassword(string $password): bool
103
    {
104 1
        return (new PasswordHasher())->validate($password, $this->passwordHash);
105
    }
106
107 1
    public function setPassword(string $password): void
108
    {
109 1
        $this->passwordHash = (new PasswordHasher())->hash($password);
110 1
    }
111
112 1
    public function getCreatedAt(): DateTimeImmutable
113
    {
114 1
        return $this->created_at;
115
    }
116
117
    public function getUpdatedAt(): DateTimeImmutable
118
    {
119
        return $this->updated_at;
120
    }
121
122 1
    public function getIdentity(): Identity
123
    {
124 1
        return $this->identity;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->identity could return the type Cycle\ORM\Promise\Reference which is incompatible with the type-hinted return App\Auth\Identity. Consider adding an additional type-check to rule them out.
Loading history...
125
    }
126
127
    /**
128
     * @return Post[]
129
     */
130
    public function getPosts(): array
131
    {
132
        return $this->posts->toArray();
133
    }
134
135
    public function addPost(Post $post): void
136
    {
137
        $this->posts->add($post);
138
    }
139
140
    /**
141
     * @return Comment[]
142
     */
143
    public function getComments(): array
144
    {
145
        return $this->comments->toArray();
146
    }
147
148
    public function addComment(Comment $post): void
149
    {
150
        $this->comments->add($post);
151
    }
152
}
153