Passed
Pull Request — master (#49)
by
unknown
13:49
created

Post::getTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Entity;
6
7
use App\Entity\User;
8
use Cycle\Annotated\Annotation\Column;
9
use Cycle\Annotated\Annotation\Entity;
10
use Cycle\Annotated\Annotation\Relation\BelongsTo;
11
use Cycle\Annotated\Annotation\Relation\HasMany;
12
use Cycle\Annotated\Annotation\Relation\ManyToMany;
13
use Cycle\Annotated\Annotation\Table;
14
use Cycle\Annotated\Annotation\Table\Index;
15
use Cycle\ORM\Relation\Pivoted\PivotedCollection;
16
use DateTimeImmutable;
17
use Doctrine\Common\Collections\ArrayCollection;
18
use Yiisoft\Security\Random;
19
20
/**
21
 * @Entity(
22
 *     repository="App\Blog\Post\PostRepository",
23
 *     mapper="App\Blog\Post\PostMapper",
24
 *     constrain="App\Blog\Post\Scope\PublicScope"
25
 * )
26
 * @Table(
27
 *     indexes={
28
 *         @Index(columns={"public","published_at"}),
29
 *     }
30
 * )
31
 */
32
class Post
33
{
34
    /**
35
     * @Column(type="primary")
36
     */
37
    private ?int $id = null;
38
39
    /**
40
     * @Column(type="string(128)")
41
     */
42
    private string $slug;
43
44
    /**
45
     * @Column(type="string(255)", default="")
46
     */
47
    private string $title = '';
48
49
    /**
50
     * @Column(type="bool", default="false")
51
     */
52
    private bool $public = false;
53
54
    /**
55
     * @Column(type="text")
56
     */
57
    private string $content;
58
59
    /**
60
     * @Column(type="datetime")
61
     */
62
    private DateTimeImmutable $created_at;
63
64
    /**
65
     * @Column(type="datetime")
66
     */
67
    private DateTimeImmutable $updated_at;
68
69
    /**
70
     * @Column(type="datetime", nullable=true)
71
     */
72
    private ?DateTimeImmutable $published_at = null;
73
74
    /**
75
     * @Column(type="datetime", nullable=true)
76
     */
77
    private ?DateTimeImmutable $deleted_at = null;
78
79
    /**
80
     * @BelongsTo(target="App\Entity\User", nullable=false)
81
     * @var User|\Cycle\ORM\Promise\Reference
82
     */
83
    private $user = null;
84
    private ?int $user_id = null;
0 ignored issues
show
introduced by
The private property $user_id is not used, and could be removed.
Loading history...
85
86
    /**
87
     * @ManyToMany(target="App\Blog\Entity\Tag", though="PostTag", fkAction="CASCADE")
88
     * @var Tag[]|PivotedCollection
89
     */
90
    private $tags;
91
    private ?int $tag_id = null;
0 ignored issues
show
introduced by
The private property $tag_id is not used, and could be removed.
Loading history...
92
93
    /**
94
     * @HasMany(target="App\Blog\Entity\Comment")
95
     * @var Comment[]|ArrayCollection
96
     */
97
    private $comments;
98
99
    public function __construct(string $title, string $content)
100
    {
101
        $this->title = $title;
102
        $this->content = $content;
103
        $this->created_at = new DateTimeImmutable();
104
        $this->updated_at = new DateTimeImmutable();
105
        $this->tags = new PivotedCollection();
106
        $this->comments = new ArrayCollection();
107
        $this->resetSlug();
108
    }
109
110
    public function getId(): ?int
111
    {
112
        return $this->id;
113
    }
114
115
    public function getSlug(): ?string
116
    {
117
        return $this->slug;
118
    }
119
120
    public function resetSlug(): void
121
    {
122
        $this->slug = Random::string(128);
123
    }
124
125
    public function getTitle(): string
126
    {
127
        return $this->title;
128
    }
129
130
    public function setTitle(string $title): void
131
    {
132
        $this->title = $title;
133
    }
134
135
    public function getContent(): string
136
    {
137
        return $this->content;
138
    }
139
140
    public function setContent(string $content): void
141
    {
142
        $this->content = $content;
143
    }
144
145
    public function isPublic(): bool
146
    {
147
        return $this->public;
148
    }
149
150
    public function setPublic(bool $public): void
151
    {
152
        $this->public = $public;
153
    }
154
155
    public function getCreatedAt(): DateTimeImmutable
156
    {
157
        return $this->created_at;
158
    }
159
160
    public function getUpdatedAt(): DateTimeImmutable
161
    {
162
        return $this->updated_at;
163
    }
164
165
    public function getDeletedAt(): ?DateTimeImmutable
166
    {
167
        return $this->deleted_at;
168
    }
169
170
    public function setUser(User $user): void
171
    {
172
        $this->user = $user;
173
    }
174
175
    public function getUser(): ?User
176
    {
177
        return $this->user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->user could return the type Cycle\ORM\Promise\Reference which is incompatible with the type-hinted return App\Entity\User|null. Consider adding an additional type-check to rule them out.
Loading history...
178
    }
179
180
    public function getPublishedAt(): ?DateTimeImmutable
181
    {
182
        return $this->published_at;
183
    }
184
185
    public function setPublishedAt(?DateTimeImmutable $published_at): void
186
    {
187
        $this->published_at = $published_at;
188
    }
189
190
    /**
191
     * @return ArrayCollection|Comment[]
192
     */
193
    public function getComments()
194
    {
195
        return $this->comments;
196
    }
197
198
    public function addComment(Comment $post): void
199
    {
200
        $this->comments->add($post);
201
    }
202
203
    /**
204
     * @return ArrayCollection|Tag[]
205
     */
206
    public function getTags()
207
    {
208
        return $this->tags;
209
    }
210
211
    public function addTag(Tag $post): void
212
    {
213
        $this->tags->add($post);
214
    }
215
}
216