Passed
Push — master ( e157d2...b7d807 )
by Julito
12:22
created

Message::setTitle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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\CoreBundle\Entity;
8
9
use ApiPlatform\Core\Annotation\ApiFilter;
10
use ApiPlatform\Core\Annotation\ApiProperty;
11
use ApiPlatform\Core\Annotation\ApiResource;
12
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
13
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
14
use Chamilo\CourseBundle\Entity\CGroup;
15
use DateTime;
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Doctrine\ORM\Mapping as ORM;
19
use Gedmo\Mapping\Annotation as Gedmo;
20
use Symfony\Component\Serializer\Annotation\Groups;
21
use Symfony\Component\Validator\Constraints as Assert;
22
23
/**
24
 * Message.
25
 *
26
 * @ORM\Table(name="message", indexes={
27
 *     @ORM\Index(name="idx_message_user_sender", columns={"user_sender_id"}),
28
 *     @ORM\Index(name="idx_message_user_receiver", columns={"user_receiver_id"}),
29
 *     @ORM\Index(name="idx_message_user_sender_user_receiver", columns={"user_sender_id", "user_receiver_id"}),
30
 *     @ORM\Index(name="idx_message_user_receiver_type", columns={"user_receiver_id", "msg_type"}),
31
 *     @ORM\Index(name="idx_message_receiver_type_send_date", columns={"user_receiver_id", "msg_type", "send_date"}),
32
 *     @ORM\Index(name="idx_message_group", columns={"group_id"}),
33
 *     @ORM\Index(name="idx_message_type", columns={"msg_type"})
34
 * })
35
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\MessageRepository")
36
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\MessageListener"})
37
 */
38
#[ApiResource(
39
    collectionOperations: [
40
        'get' => [],
41
        'post' => [
42
            'security' => "is_granted('ROLE_USER')",
43
            //            'deserialize' => false,
44
            //            'controller' => Create::class,
45
            //            'openapi_context' => [
46
            //                'requestBody' => [
47
            //                    'content' => [
48
            //                        'multipart/form-data' => [
49
            //                            'schema' => [
50
            //                                'type' => 'object',
51
            //                                'properties' => [
52
            //                                    'title' => [
53
            //                                        'type' => 'string',
54
            //                                    ],
55
            //                                    'content' => [
56
            //                                        'type' => 'string',
57
            //                                    ],
58
            //                                ],
59
            //                            ],
60
            //                        ],
61
            //                    ],
62
            //                ],
63
            //            ],
64
        ],
65
    ],
66
    itemOperations: [
67
        'get' => [
68
            'security' => "is_granted('VIEW', object)",
69
        ],
70
        'put' => [
71
            'security' => "is_granted('EDIT', object)",
72
        ],
73
        'delete' => [
74
            'security' => "is_granted('DELETE', object)",
75
        ],
76
    ],
77
    attributes: [
78
        'security' => "is_granted('ROLE_USER')",
79
    ],
80
    denormalizationContext: [
81
        'groups' => ['message:write'],
82
    ],
83
    normalizationContext: [
84
        'groups' => ['message:read'],
85
    ],
86
)]
87
#[ApiFilter(OrderFilter::class, properties: ['title', 'sendDate'])]
88
#[ApiFilter(SearchFilter::class, properties: [
89
    'read' => 'exact',
90
    'msgType' => 'exact',
91
    'userSender' => 'exact',
92
    'userReceiver' => 'exact',
93
    'tags' => 'exact',
94
])]
95
class Message
96
{
97
    public const MESSAGE_TYPE_INBOX = 1;
98
    public const MESSAGE_TYPE_OUTBOX = 2;
99
    public const MESSAGE_TYPE_PROMOTED = 3;
100
101
    /**
102
     * @ORM\Column(name="id", type="bigint")
103
     * @ORM\Id
104
     * @ORM\GeneratedValue()
105
     */
106
    #[Groups(['message:read'])]
107
    protected int $id;
108
109
    /**
110
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="sentMessages")
111
     * @ORM\JoinColumn(name="user_sender_id", referencedColumnName="id", nullable=false)
112
     */
113
    #[Assert\NotBlank]
114
    #[Groups(['message:read', 'message:write'])]
115
    protected User $userSender;
116
117
    /**
118
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="receivedMessages")
119
     * @ORM\JoinColumn(name="user_receiver_id", referencedColumnName="id", nullable=true)
120
     */
121
    #[Groups(['message:read', 'message:write'])]
122
    protected User $userReceiver;
123
124
    /**
125
     * @ORM\Column(name="msg_type", type="smallint", nullable=false)
126
     */
127
    #[Assert\NotBlank]
128
    #[Assert\Choice([
129
        self::MESSAGE_TYPE_INBOX,
130
        self::MESSAGE_TYPE_OUTBOX,
131
        self::MESSAGE_TYPE_PROMOTED,
132
    ])]
133
    /*#[ApiProperty(attributes: [
134
        'openapi_context' => [
135
            'type' => 'int',
136
            'enum' => [self::MESSAGE_TYPE_INBOX, self::MESSAGE_TYPE_OUTBOX],
137
        ],
138
    ])]*/
139
    #[Groups(['message:read', 'message:write'])]
140
    protected int $msgType;
141
142
    /**
143
     * @ORM\Column(name="msg_read", type="boolean", nullable=false)
144
     */
145
    #[Assert\NotNull]
146
    #[Groups(['message:read', 'message:write'])]
147
    protected bool $read;
148
149
    /**
150
     * @ORM\Column(name="send_date", type="datetime", nullable=false)
151
     */
152
    #[Groups(['message:read'])]
153
    protected DateTime $sendDate;
154
155
    /**
156
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
157
     */
158
    #[Assert\NotBlank]
159
    #[Groups(['message:read', 'message:write'])]
160
    protected string $title;
161
162
    /**
163
     * @ORM\Column(name="content", type="text", nullable=false)
164
     */
165
    #[Assert\NotBlank]
166
    #[Groups(['message:read', 'message:write'])]
167
    protected string $content;
168
169
    /**
170
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroup")
171
     * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true, onDelete="CASCADE")
172
     */
173
    protected ?CGroup $group = null;
174
175
    /**
176
     * @var Collection|Message[]
177
     * @ORM\OneToMany(targetEntity="Message", mappedBy="parent")
178
     */
179
    protected Collection $children;
180
181
    /**
182
     * @ORM\ManyToOne(targetEntity="Message", inversedBy="children")
183
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
184
     */
185
    protected ?Message $parent = null;
186
187
    /**
188
     * @Gedmo\Timestampable(on="update")
189
     * @ORM\Column(name="update_date", type="datetime", nullable=true)
190
     */
191
    protected ?DateTime $updateDate;
192
193
    /**
194
     * @ORM\Column(name="votes", type="integer", nullable=true)
195
     */
196
    protected ?int $votes;
197
198
    /**
199
     * @var Collection|MessageAttachment[]
200
     *
201
     * @ORM\OneToMany(targetEntity="MessageAttachment", mappedBy="message")
202
     */
203
    protected Collection $attachments;
204
205
    /**
206
     * @var Collection|MessageFeedback[]
207
     *
208
     * @ORM\OneToMany(targetEntity="MessageFeedback", mappedBy="message", orphanRemoval=true)
209
     */
210
    protected Collection $likes;
211
212
    /**
213
     * @var Collection|MessageTag[]
214
     *
215
     * @ORM\ManyToMany(targetEntity="Chamilo\CoreBundle\Entity\MessageTag", inversedBy="messages", cascade={"persist"})
216
     * @ORM\JoinTable(name="message_rel_tags")
217
     */
218
    #[Groups(['message:read', 'message:write'])]
219
    protected Collection $tags;
220
221
    public function __construct()
222
    {
223
        $this->sendDate = new DateTime('now');
224
        $this->updateDate = $this->sendDate;
225
        $this->content = '';
226
        $this->attachments = new ArrayCollection();
227
        $this->children = new ArrayCollection();
228
        $this->tags = new ArrayCollection();
229
        $this->likes = new ArrayCollection();
230
        $this->votes = 0;
231
        $this->read = false;
232
    }
233
234
    /**
235
     * @return Collection|MessageTag[]
236
     */
237
    public function getTags()
238
    {
239
        return $this->tags;
240
    }
241
242
    public function addTag(MessageTag $tag): self
243
    {
244
        if (!$this->tags->contains($tag)) {
245
            $this->tags->add($tag);
246
        }
247
248
        return $this;
249
    }
250
251
    public function removeTag(MessageTag $tag): self
252
    {
253
        if ($this->tags->contains($tag)) {
254
            $this->tags->removeElement($tag);
255
        }
256
257
        return $this;
258
    }
259
260
    public function setUserSender(User $userSender): self
261
    {
262
        $this->userSender = $userSender;
263
264
        return $this;
265
    }
266
267
    public function getUserSender(): User
268
    {
269
        return $this->userSender;
270
    }
271
272
    public function setUserReceiver(User $userReceiver): self
273
    {
274
        $this->userReceiver = $userReceiver;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Get userReceiver.
281
     *
282
     * @return User
283
     */
284
    public function getUserReceiver()
285
    {
286
        return $this->userReceiver;
287
    }
288
289
    public function setMsgType(int $msgType): self
290
    {
291
        $this->msgType = $msgType;
292
293
        return $this;
294
    }
295
296
    public function getMsgType(): int
297
    {
298
        return $this->msgType;
299
    }
300
301
    public function setSendDate(DateTime $sendDate): self
302
    {
303
        $this->sendDate = $sendDate;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Get sendDate.
310
     *
311
     * @return DateTime
312
     */
313
    public function getSendDate()
314
    {
315
        return $this->sendDate;
316
    }
317
318
    public function setTitle(string $title): self
319
    {
320
        $this->title = $title;
321
322
        return $this;
323
    }
324
325
    public function getTitle(): string
326
    {
327
        return $this->title;
328
    }
329
330
    public function setContent(string $content): self
331
    {
332
        $this->content = $content;
333
334
        return $this;
335
    }
336
337
    /**
338
     * Get content.
339
     *
340
     * @return string
341
     */
342
    public function getContent()
343
    {
344
        return $this->content;
345
    }
346
347
    public function setUpdateDate(DateTime $updateDate): self
348
    {
349
        $this->updateDate = $updateDate;
350
351
        return $this;
352
    }
353
354
    /**
355
     * Get updateDate.
356
     *
357
     * @return DateTime
358
     */
359
    public function getUpdateDate()
360
    {
361
        return $this->updateDate;
362
    }
363
364
    /**
365
     * Get id.
366
     *
367
     * @return int
368
     */
369
    public function getId()
370
    {
371
        return $this->id;
372
    }
373
374
    public function setVotes(int $votes): self
375
    {
376
        $this->votes = $votes;
377
378
        return $this;
379
    }
380
381
    public function getVotes(): int
382
    {
383
        return $this->votes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->votes could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
384
    }
385
386
    /**
387
     * Get attachments.
388
     *
389
     * @return Collection|MessageAttachment[]
390
     */
391
    public function getAttachments()
392
    {
393
        return $this->attachments;
394
    }
395
396
    public function addAttachment(MessageAttachment $attachment): self
397
    {
398
        $this->attachments->add($attachment);
399
        $attachment->setMessage($this);
400
401
        return $this;
402
    }
403
404
    public function getParent(): ?self
405
    {
406
        return $this->parent;
407
    }
408
409
    /**
410
     * @return Collection|Message[]
411
     */
412
    public function getChildren()
413
    {
414
        return $this->children;
415
    }
416
417
    public function addChild(self $child): self
418
    {
419
        $this->children[] = $child;
420
        $child->setParent($this);
421
422
        return $this;
423
    }
424
425
    public function setParent(self $parent = null): self
426
    {
427
        $this->parent = $parent;
428
429
        return $this;
430
    }
431
432
    /**
433
     * @return MessageFeedback[]|Collection
434
     */
435
    public function getLikes()
436
    {
437
        return $this->likes;
438
    }
439
440
    public function getGroup(): ?CGroup
441
    {
442
        return $this->group;
443
    }
444
445
    public function setGroup(?CGroup $group): self
446
    {
447
        $this->group = $group;
448
449
        return $this;
450
    }
451
452
    public function isRead(): bool
453
    {
454
        return $this->read;
455
    }
456
457
    public function setRead(bool $read): self
458
    {
459
        $this->read = $read;
460
461
        return $this;
462
    }
463
}
464