Passed
Push — master ( 727abb...ceefce )
by Yannick
07:55 queued 14s
created

CForumPost::__toString()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CourseBundle\Entity;
8
9
use Chamilo\CoreBundle\Entity\AbstractResource;
10
use Chamilo\CoreBundle\Entity\ResourceInterface;
11
use Chamilo\CoreBundle\Entity\User;
12
use Chamilo\CourseBundle\Repository\CForumPostRepository;
13
use DateTime;
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\ORM\Mapping as ORM;
17
use Stringable;
18
use Symfony\Component\Uid\Uuid;
19
use Symfony\Component\Validator\Constraints as Assert;
20
21
/**
22
 * CForumPost.
23
 */
24
#[ORM\Table(name: 'c_forum_post')]
25
#[ORM\Index(name: 'forum_id', columns: ['forum_id'])]
26
#[ORM\Index(name: 'idx_forum_post_thread_id', columns: ['thread_id'])]
27
#[ORM\Index(name: 'idx_forum_post_visible', columns: ['visible'])]
28
#[ORM\Entity(repositoryClass: CForumPostRepository::class)]
29
class CForumPost extends AbstractResource implements ResourceInterface, Stringable
30
{
31
    public const STATUS_VALIDATED = 1;
32
    public const STATUS_WAITING_MODERATION = 2;
33
    public const STATUS_REJECTED = 3;
34
35
    #[ORM\Column(name: 'iid', type: 'integer')]
36
    #[ORM\Id]
37
    #[ORM\GeneratedValue]
38
    protected ?int $iid = null;
39
40
    #[Assert\NotBlank]
41
    #[ORM\Column(name: 'title', type: 'string', length: 250, nullable: false)]
42
    protected string $title;
43
44
    #[ORM\Column(name: 'post_text', type: 'text', nullable: true)]
45
    protected ?string $postText = null;
46
47
    #[Assert\NotBlank]
48
    #[ORM\Column(name: 'post_date', type: 'datetime', nullable: false)]
49
    protected DateTime $postDate;
50
51
    #[ORM\Column(name: 'post_notification', type: 'boolean', nullable: true)]
52
    protected ?bool $postNotification = null;
53
54
    #[Assert\NotNull]
55
    #[ORM\Column(name: 'visible', type: 'boolean', nullable: false)]
56
    protected bool $visible;
57
58
    #[ORM\Column(name: 'status', type: 'integer', nullable: true)]
59
    protected ?int $status = null;
60
61
    #[ORM\ManyToOne(targetEntity: CForumThread::class, inversedBy: 'posts')]
62
    #[ORM\JoinColumn(name: 'thread_id', referencedColumnName: 'iid', nullable: true, onDelete: 'SET NULL')]
63
    protected ?CForumThread $thread = null;
64
65
    #[ORM\ManyToOne(targetEntity: CForum::class, inversedBy: 'posts')]
66
    #[ORM\JoinColumn(name: 'forum_id', referencedColumnName: 'iid', nullable: true, onDelete: 'SET NULL')]
67
    protected ?CForum $forum = null;
68
69
    #[Assert\NotBlank]
70
    #[ORM\ManyToOne(targetEntity: User::class)]
71
    #[ORM\JoinColumn(name: 'poster_id', referencedColumnName: 'id')]
72
    protected ?User $user = null;
73
74
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
75
    #[ORM\JoinColumn(name: 'post_parent_id', referencedColumnName: 'iid', onDelete: 'SET NULL')]
76
    protected ?CForumPost $postParent = null;
77
78
    /**
79
     * @var Collection|CForumPost[]
80
     */
81
    #[ORM\OneToMany(targetEntity: self::class, mappedBy: 'postParent')]
82
    protected Collection $children;
83
84
    /**
85
     * @var Collection|CForumAttachment[]
86
     */
87
    #[ORM\OneToMany(targetEntity: CForumAttachment::class, mappedBy: 'post', cascade: ['persist', 'remove'], orphanRemoval: true)]
88
    protected Collection $attachments;
89
90
    public function __construct()
91
    {
92
        $this->postDate = new DateTime();
93
        $this->visible = false;
94
        $this->attachments = new ArrayCollection();
95
        $this->children = new ArrayCollection();
96
    }
97
98
    public function __toString(): string
99
    {
100
        return $this->getTitle();
101
    }
102
103
    public function setTitle(string $title): self
104
    {
105
        $this->title = $title;
106
107
        return $this;
108
    }
109
110
    public function getTitle(): string
111
    {
112
        return $this->title;
113
    }
114
115
    public function setPostText(string $postText): self
116
    {
117
        $this->postText = $postText;
118
119
        return $this;
120
    }
121
122
    public function getPostText(): ?string
123
    {
124
        return $this->postText;
125
    }
126
127
    public function setThread(CForumThread $thread = null): self
128
    {
129
        if (null !== $thread) {
130
            $thread->getPosts()->add($this);
131
        }
132
        $this->thread = $thread;
133
134
        return $this;
135
    }
136
137
    public function getThread(): CForumThread
138
    {
139
        return $this->thread;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->thread could return the type null which is incompatible with the type-hinted return Chamilo\CourseBundle\Entity\CForumThread. Consider adding an additional type-check to rule them out.
Loading history...
140
    }
141
142
    public function setPostDate(DateTime $postDate): self
143
    {
144
        $this->postDate = $postDate;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Get postDate.
151
     *
152
     * @return DateTime
153
     */
154
    public function getPostDate()
155
    {
156
        return $this->postDate;
157
    }
158
159
    public function setPostNotification(bool $postNotification): self
160
    {
161
        $this->postNotification = $postNotification;
162
163
        return $this;
164
    }
165
166
    /**
167
     * Get postNotification.
168
     *
169
     * @return bool
170
     */
171
    public function getPostNotification()
172
    {
173
        return $this->postNotification;
174
    }
175
176
    public function setVisible(bool $visible): self
177
    {
178
        $this->visible = $visible;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Get visible.
185
     *
186
     * @return bool
187
     */
188
    public function getVisible()
189
    {
190
        return $this->visible;
191
    }
192
193
    /**
194
     * @return int
195
     */
196
    public function getStatus()
197
    {
198
        return $this->status;
199
    }
200
201
    public function setStatus(int $status): self
202
    {
203
        $this->status = $status;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Get iid.
210
     */
211
    public function getIid(): ?int
212
    {
213
        return $this->iid;
214
    }
215
216
    public function getAttachments(): Collection
217
    {
218
        return $this->attachments;
219
    }
220
221
    public function removeAttachment(CForumAttachment $attachment): void
222
    {
223
        $this->attachments->removeElement($attachment);
224
    }
225
226
    public function getForum(): ?CForum
227
    {
228
        return $this->forum;
229
    }
230
231
    public function setForum(?CForum $forum): self
232
    {
233
        $forum->getPosts()->add($this);
0 ignored issues
show
Bug introduced by
The method getPosts() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
        $forum->/** @scrutinizer ignore-call */ 
234
                getPosts()->add($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
234
235
        $this->forum = $forum;
236
237
        return $this;
238
    }
239
240
    public function getUser(): User
241
    {
242
        return $this->user;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->user could return the type null which is incompatible with the type-hinted return Chamilo\CoreBundle\Entity\User. Consider adding an additional type-check to rule them out.
Loading history...
243
    }
244
245
    public function setUser(User $user): self
246
    {
247
        $this->user = $user;
248
249
        return $this;
250
    }
251
252
    public function getPostParent(): ?self
253
    {
254
        return $this->postParent;
255
    }
256
257
    public function setPostParent(?self $postParent): self
258
    {
259
        $this->postParent = $postParent;
260
261
        return $this;
262
    }
263
264
    /**
265
     * @return CForumPost[]|Collection
266
     */
267
    public function getChildren(): array|Collection
268
    {
269
        return $this->children;
270
    }
271
272
    /**
273
     * @param CForumPost[]|Collection $children
274
     */
275
    public function setChildren(array|Collection $children): self
276
    {
277
        $this->children = $children;
278
279
        return $this;
280
    }
281
282
    public function getResourceIdentifier(): int|Uuid
283
    {
284
        return $this->getIid();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getIid() could return the type null which is incompatible with the type-hinted return Symfony\Component\Uid\Uuid|integer. Consider adding an additional type-check to rule them out.
Loading history...
285
    }
286
287
    public function getResourceName(): string
288
    {
289
        return $this->getTitle();
290
    }
291
292
    public function setResourceName(string $name): self
293
    {
294
        return $this->setTitle($name);
295
    }
296
}
297