Passed
Pull Request — master (#6838)
by
unknown
08:18
created

CBlogComment::getPost()   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 ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
11
use ApiPlatform\Metadata\ApiFilter;
12
use ApiPlatform\Metadata\ApiResource;
13
use Chamilo\CoreBundle\Entity\User;
14
use DateTime;
15
use Doctrine\ORM\Mapping as ORM;
16
use Symfony\Component\Serializer\Annotation\Groups;
17
18
#[ApiResource(
19
    normalizationContext: ['groups' => ['blog_comment:read']],
20
    denormalizationContext: ['groups' => ['blog_comment:write']],
21
    paginationEnabled: true
22
)]
23
#[ApiFilter(SearchFilter::class, properties: [
24
    'post' => 'exact',
25
])]
26
#[ApiFilter(OrderFilter::class, properties: [
27
    'dateCreation' => 'DESC',
28
])]
29
#[ORM\Table(name: 'c_blog_comment')]
30
#[ORM\Entity]
31
#[ORM\HasLifecycleCallbacks]
32
class CBlogComment
33
{
34
    #[Groups(['blog_comment:read'])]
35
    #[ORM\Column(name: 'iid', type: 'integer')]
36
    #[ORM\Id]
37
    #[ORM\GeneratedValue]
38
    protected ?int $iid = null;
39
40
    /**
41
     * LEGACY: required non-null column kept for backwards compatibility.
42
     * We set 0 by default so inserts don't fail.
43
     */
44
    #[Groups(['blog_comment:read'])]
45
    #[ORM\Column(name: 'comment_id', type: 'integer', nullable: false, options: ['default' => 0])]
46
    protected int $commentId = 0;
47
48
    #[Groups(['blog_comment:read','blog_comment:write'])]
49
    #[ORM\Column(name: 'title', type: 'string', length: 250, nullable: false)]
50
    protected string $title = '';
51
52
    #[Groups(['blog_comment:read','blog_comment:write'])]
53
    #[ORM\Column(name: 'comment', type: 'text', nullable: false)]
54
    protected string $comment;
55
56
    #[Groups(['blog_comment:read','blog_comment:write'])]
57
    #[ORM\ManyToOne(targetEntity: User::class)]
58
    #[ORM\JoinColumn(name: 'author_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
59
    protected ?User $author = null;
60
61
    #[Groups(['blog_comment:read'])]
62
    #[ORM\Column(name: 'date_creation', type: 'datetime', nullable: false)]
63
    protected DateTime $dateCreation;
64
65
    #[Groups(['blog_comment:read','blog_comment:write'])]
66
    #[ORM\ManyToOne(targetEntity: CBlog::class)]
67
    #[ORM\JoinColumn(name: 'blog_id', referencedColumnName: 'iid', onDelete: 'CASCADE')]
68
    protected ?CBlog $blog = null;
69
70
    /** Real relation to the blog post (keeps DB column name post_id). */
71
    #[Groups(['blog_comment:read','blog_comment:write'])]
72
    #[ORM\ManyToOne(targetEntity: CBlogPost::class)]
73
    #[ORM\JoinColumn(name: 'post_id', referencedColumnName: 'iid', nullable: false, onDelete: 'CASCADE')]
74
    protected ?CBlogPost $post = null;
75
76
    /** Optional: parent comment (threading). Kept nullable, matches parent_comment_id column. */
77
    #[Groups(['blog_comment:read','blog_comment:write'])]
78
    #[ORM\ManyToOne(targetEntity: self::class)]
79
    #[ORM\JoinColumn(name: 'parent_comment_id', referencedColumnName: 'iid', nullable: true, onDelete: 'CASCADE')]
80
    protected ?CBlogComment $parentComment = null;
81
82
    public function __construct()
83
    {
84
        $this->dateCreation = new DateTime();
85
        // $this->title already defaults to ''
86
        // $this->commentId already defaults to 0 (legacy)
87
    }
88
89
    #[ORM\PrePersist]
90
    public function ensureRequiredFields(): void
91
    {
92
        if (!isset($this->commentId)) {
93
            $this->commentId = 0; // legacy default
94
        }
95
        if (!isset($this->dateCreation)) {
96
            $this->dateCreation = new DateTime();
97
        }
98
        if (!isset($this->title)) {
99
            $this->title = '';
100
        }
101
        if ($this->blog === null && $this->post instanceof CBlogPost) {
102
            $this->blog = $this->post->getBlog();
103
        }
104
    }
105
106
    public function getIid(): ?int
107
    {
108
        return $this->iid;
109
    }
110
111
    public function getCommentId(): int
112
    {
113
        return $this->commentId;
114
    }
115
116
    public function setCommentId(int $commentId): self
117
    {
118
        $this->commentId = $commentId;
119
120
        return $this;
121
    }
122
123
    public function getTitle(): string
124
    {
125
        return $this->title;
126
    }
127
128
    public function setTitle(string $title): self
129
    {
130
        $this->title = $title;
131
132
        return $this;
133
    }
134
135
    public function getComment(): string
136
    {
137
        return $this->comment;
138
    }
139
140
    public function setComment(string $comment): self
141
    {
142
        $this->comment = $comment;
143
144
        return $this;
145
    }
146
147
    public function getAuthor(): ?User
148
    {
149
        return $this->author;
150
    }
151
152
    public function setAuthor(?User $author): self
153
    {
154
        $this->author = $author;
155
156
        return $this;
157
    }
158
159
    public function getDateCreation(): DateTime
160
    {
161
        return $this->dateCreation;
162
    }
163
164
    public function setDateCreation(DateTime $dateCreation): self
165
    {
166
        $this->dateCreation = $dateCreation;
167
168
        return $this;
169
    }
170
171
    public function getBlog(): ?CBlog
172
    {
173
        return $this->blog;
174
    }
175
176
    public function setBlog(?CBlog $blog): self
177
    {
178
        $this->blog = $blog;
179
180
        return $this;
181
    }
182
183
    public function getPost(): ?CBlogPost
184
    {
185
        return $this->post;
186
    }
187
188
    public function setPost(?CBlogPost $post): self
189
    {
190
        $this->post = $post;
191
192
        return $this;
193
    }
194
195
    public function getParentComment(): ?CBlogComment
196
    {
197
        return $this->parentComment;
198
    }
199
200
    public function setParentComment(?CBlogComment $parentComment): self
201
    {
202
        $this->parentComment = $parentComment;
203
204
        return $this;
205
    }
206
}
207