Post::addComment()   A
last analyzed

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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog\Entity;
6
7
use App\Blog\Post\PostRepository;
8
use App\Blog\Post\Scope\PublicScope;
9
use App\User\User;
10
use Cycle\Annotated\Annotation\Column;
0 ignored issues
show
Bug introduced by
The type Cycle\Annotated\Annotation\Column was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Cycle\Annotated\Annotation\Entity;
0 ignored issues
show
Bug introduced by
The type Cycle\Annotated\Annotation\Entity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Cycle\Annotated\Annotation\Relation\BelongsTo;
0 ignored issues
show
Bug introduced by
The type Cycle\Annotated\Annotation\Relation\BelongsTo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Cycle\Annotated\Annotation\Relation\HasMany;
0 ignored issues
show
Bug introduced by
The type Cycle\Annotated\Annotation\Relation\HasMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Cycle\Annotated\Annotation\Relation\ManyToMany;
0 ignored issues
show
Bug introduced by
The type Cycle\Annotated\Annotation\Relation\ManyToMany was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Cycle\Annotated\Annotation\Table\Index;
0 ignored issues
show
Bug introduced by
The type Cycle\Annotated\Annotation\Table\Index was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Cycle\ORM\Collection\Pivoted\PivotedCollection;
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\Collection\Pivoted\PivotedCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Cycle\ORM\Entity\Behavior;
0 ignored issues
show
Bug introduced by
The type Cycle\ORM\Entity\Behavior was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use DateTimeImmutable;
19
use Doctrine\Common\Collections\ArrayCollection;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Collections\ArrayCollection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Yiisoft\Security\Random;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Security\Random was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
22
#[Entity(
23
    repository: PostRepository::class,
24
    scope: PublicScope::class
25
)]
26
#[Index(columns: ['public', 'published_at'])]
27
#[Behavior\CreatedAt(field: 'created_at', column: 'created_at')]
28
#[Behavior\UpdatedAt(field: 'updated_at', column: 'updated_at')]
29
#[Behavior\SoftDelete(field: 'deleted_at', column: 'deleted_at')]
30
class Post
31
{
32
    #[Column(type: 'primary')]
33
    private ?int $id = null;
34
35
    #[Column(type: 'string(128)')]
36
    private string $slug;
37
38
    #[Column(type: 'string(191)', default: '')]
39
    private string $title = '';
40
41
    #[Column(type: 'bool', default: 'false', typecast: 'bool')]
42
    private bool $public = false;
43
44
    #[Column(type: 'text')]
45
    private string $content = '';
46
47
    #[Column(type: 'datetime')]
48
    private DateTimeImmutable $created_at;
49
50
    #[Column(type: 'datetime')]
51
    private DateTimeImmutable $updated_at;
52
53
    #[Column(type: 'datetime', nullable: true)]
54
    private ?DateTimeImmutable $published_at = null;
55
56
    #[Column(type: 'datetime', nullable: true)]
57
    private ?DateTimeImmutable $deleted_at = null;
58
59
    #[BelongsTo(target: User::class, nullable: false)]
60
    private ?User $user = null;
61
    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...
62
63
    /**
64
     * @var PivotedCollection<array-key, Tag, PostTag>
65
     */
66
    #[ManyToMany(target: Tag::class, though: PostTag::class, fkAction: 'CASCADE')]
67
    private PivotedCollection $tags;
68
69
    /**
70
     * @var ArrayCollection<array-key, Comment>
71
     */
72
    #[HasMany(target: Comment::class)]
73
    private ArrayCollection $comments;
74
75
    public function __construct(string $title = '', string $content = '')
76
    {
77
        $this->title = $title;
78
        $this->content = $content;
79
        $this->created_at = new DateTimeImmutable();
80
        $this->updated_at = new DateTimeImmutable();
81
        $this->tags = new PivotedCollection();
82
        $this->comments = new ArrayCollection();
83
        $this->resetSlug();
84
    }
85
86
    public function getId(): ?int
87
    {
88
        return $this->id;
89
    }
90
91
    public function getSlug(): ?string
92
    {
93
        return $this->slug;
94
    }
95
96
    public function resetSlug(): void
97
    {
98
        $this->slug = Random::string(128);
99
    }
100
101
    public function getTitle(): string
102
    {
103
        return $this->title;
104
    }
105
106
    public function setTitle(string $title): void
107
    {
108
        $this->title = $title;
109
    }
110
111
    public function getContent(): string
112
    {
113
        return $this->content;
114
    }
115
116
    public function setContent(string $content): void
117
    {
118
        $this->content = $content;
119
    }
120
121
    public function isPublic(): bool
122
    {
123
        return $this->public;
124
    }
125
126
    public function setPublic(bool $public): void
127
    {
128
        $this->public = $public;
129
    }
130
131
    public function getCreatedAt(): DateTimeImmutable
132
    {
133
        return $this->created_at;
134
    }
135
136
    public function getUpdatedAt(): DateTimeImmutable
137
    {
138
        return $this->updated_at;
139
    }
140
141
    public function getDeletedAt(): ?DateTimeImmutable
142
    {
143
        return $this->deleted_at;
144
    }
145
146
    public function setUser(User $user): void
147
    {
148
        $this->user = $user;
149
    }
150
151
    public function getUser(): ?User
152
    {
153
        return $this->user;
154
    }
155
156
    public function getPublishedAt(): ?DateTimeImmutable
157
    {
158
        return $this->published_at;
159
    }
160
161
    public function setPublishedAt(?DateTimeImmutable $published_at): void
162
    {
163
        $this->published_at = $published_at;
164
    }
165
166
    /**
167
     * @return Comment[]
168
     */
169
    public function getComments(): array
170
    {
171
        return $this->comments->toArray();
172
    }
173
174
    public function addComment(Comment $post): void
175
    {
176
        $this->comments->add($post);
177
    }
178
179
    /**
180
     * @return Tag[]
181
     */
182
    public function getTags(): array
183
    {
184
        return $this->tags->toArray();
185
    }
186
187
    public function addTag(Tag $post): void
188
    {
189
        $this->tags->add($post);
190
    }
191
192
    public function resetTags(): void
193
    {
194
        $this->tags->clear();
195
    }
196
197
    public function isNewRecord(): bool
198
    {
199
        return $this->getId() === null;
200
    }
201
}
202