Passed
Push — master ( f10e67...52beba )
by Julito
07:56
created

Message::setParent()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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_group", columns={"group_id"}),
29
 *     @ORM\Index(name="idx_message_type", columns={"msg_type"})
30
 * })
31
 * @ORM\Entity(repositoryClass="Chamilo\CoreBundle\Repository\MessageRepository")
32
 * @ORM\EntityListeners({"Chamilo\CoreBundle\Entity\Listener\MessageListener"})
33
 */
34
#[ApiResource(
35
    collectionOperations: [
36
        'get' => [
37
            'security' => "is_granted('ROLE_USER')",  // the get collection is also filtered by MessageExtension.php
38
        ],
39
        'post' => [
40
            //'security' => "is_granted('ROLE_USER')",
41
            /*'messenger' => true,
42
            'output' => false,
43
            'status' => 202,*/
44
            'security_post_denormalize' => "is_granted('CREATE', object)",
45
            //            'deserialize' => false,
46
            //            'controller' => Create::class,
47
            //            'openapi_context' => [
48
            //                'requestBody' => [
49
            //                    'content' => [
50
            //                        'multipart/form-data' => [
51
            //                            'schema' => [
52
            //                                'type' => 'object',
53
            //                                'properties' => [
54
            //                                    'title' => [
55
            //                                        'type' => 'string',
56
            //                                    ],
57
            //                                    'content' => [
58
            //                                        'type' => 'string',
59
            //                                    ],
60
            //                                ],
61
            //                            ],
62
            //                        ],
63
            //                    ],
64
            //                ],
65
            //            ],
66
        ],
67
    ],
68
    itemOperations: [
69
        'get' => [
70
            'security' => "is_granted('VIEW', object)",
71
        ],
72
        'put' => [
73
            'security' => "is_granted('EDIT', object)",
74
        ],
75
        'delete' => [
76
            'security' => "is_granted('DELETE', object)",
77
        ],
78
    ],
79
    attributes: [
80
        'security' => "is_granted('ROLE_USER')",
81
    ],
82
    denormalizationContext: [
83
        'groups' => ['message:write'],
84
    ],
85
    normalizationContext: [
86
        'groups' => ['message:read'],
87
    ],
88
)]
89
#[ApiFilter(OrderFilter::class, properties: ['title', 'sendDate'])]
90
#[ApiFilter(SearchFilter::class, properties: [
91
    'read' => 'exact',
92
    'status' => 'exact',
93
    'msgType' => 'exact',
94
    'sender' => 'exact',
95
    'tags' => 'exact',
96
    'receivers' => 'exact',
97
])]
98
class Message
99
{
100
    public const MESSAGE_TYPE_INBOX = 1;
101
    public const MESSAGE_TYPE_OUTBOX = 2;
102
    public const MESSAGE_TYPE_PROMOTED = 3;
103
    public const MESSAGE_TYPE_WALL = 4;
104
    public const MESSAGE_TYPE_GROUP = 5;
105
    public const MESSAGE_TYPE_INVITATION = 6;
106
    public const MESSAGE_TYPE_CONVERSATION = 7;
107
108
    // status
109
    public const MESSAGE_STATUS_DELETED = 3;
110
    public const MESSAGE_STATUS_DRAFT = 4;
111
112
    public const MESSAGE_STATUS_INVITATION_PENDING = 5;
113
    public const MESSAGE_STATUS_INVITATION_ACCEPTED = 6;
114
    public const MESSAGE_STATUS_INVITATION_DENIED = 7;
115
116
    public const MESSAGE_STATUS_PROMOTED = 13;
117
118
    /**
119
     * @ORM\Column(name="id", type="bigint")
120
     * @ORM\Id
121
     * @ORM\GeneratedValue()
122
     */
123
    #[ApiProperty(identifier: true)]
124
    #[Groups(['message:read'])]
125
    protected int $id;
126
127
    /**
128
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="sentMessages")
129
     * @ORM\JoinColumn(name="user_sender_id", referencedColumnName="id", nullable=false)
130
     */
131
    #[Assert\NotBlank]
132
    #[Groups(['message:read', 'message:write'])]
133
    protected User $sender;
134
135
    /**
136
     * @var Collection<int, User>|User[]
137
     *
138
     * @ORM\ManyToMany(
139
     *     targetEntity="Chamilo\CoreBundle\Entity\User",
140
     *     inversedBy="receivedMessages",
141
     *     cascade={"persist"}
142
     * )
143
     * @ORM\JoinTable(name="message_rel_user")
144
     */
145
    #[Groups(['message:read', 'message:write'])]
146
    protected array | null | Collection $receivers;
147
148
    /**
149
     * @ORM\Column(name="msg_type", type="smallint", nullable=false)
150
     */
151
    #[Assert\NotBlank]
152
    // @todo use enums with PHP 8.1
153
    /*#[Assert\Choice([
154
        self::MESSAGE_TYPE_INBOX,
155
        self::MESSAGE_TYPE_OUTBOX,
156
        self::MESSAGE_TYPE_PROMOTED,
157
    ])]*/
158
    /*#[ApiProperty(attributes: [
159
        'openapi_context' => [
160
            'type' => 'int',
161
            'enum' => [self::MESSAGE_TYPE_INBOX, self::MESSAGE_TYPE_OUTBOX],
162
        ],
163
    ])]*/
164
    #[Groups(['message:read', 'message:write'])]
165
    protected int $msgType;
166
167
    /**
168
     * @ORM\Column(name="status", type="smallint", nullable=false)
169
     */
170
    #[Assert\NotBlank]
171
    #[Groups(['message:read', 'message:write'])]
172
    protected int $status;
173
174
    /**
175
     * @ORM\Column(name="msg_read", type="boolean", nullable=false)
176
     */
177
    #[Assert\NotNull]
178
    #[Groups(['message:read', 'message:write'])]
179
    protected bool $read;
180
181
    /**
182
     * @ORM\Column(name="starred", type="boolean", nullable=false)
183
     */
184
    #[Assert\NotNull]
185
    #[Groups(['message:read', 'message:write'])]
186
    protected bool $starred;
187
188
    /**
189
     * @ORM\Column(name="send_date", type="datetime", nullable=false)
190
     */
191
    #[Groups(['message:read'])]
192
    protected DateTime $sendDate;
193
194
    /**
195
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
196
     */
197
    #[Assert\NotBlank]
198
    #[Groups(['message:read', 'message:write'])]
199
    protected string $title;
200
201
    /**
202
     * @ORM\Column(name="content", type="text", nullable=false)
203
     */
204
    #[Assert\NotBlank]
205
    #[Groups(['message:read', 'message:write'])]
206
    protected string $content;
207
208
    /**
209
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroup")
210
     * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true, onDelete="CASCADE")
211
     */
212
    protected ?CGroup $group = null;
213
214
    /**
215
     * @var Collection|Message[]
216
     * @ORM\OneToMany(targetEntity="Message", mappedBy="parent")
217
     */
218
    protected Collection $children;
219
220
    /**
221
     * @ORM\ManyToOne(targetEntity="Message", inversedBy="children")
222
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
223
     */
224
    protected ?Message $parent = null;
225
226
    /**
227
     * @Gedmo\Timestampable(on="update")
228
     * @ORM\Column(name="update_date", type="datetime", nullable=true)
229
     */
230
    protected ?DateTime $updateDate;
231
232
    /**
233
     * @ORM\Column(name="votes", type="integer", nullable=true)
234
     */
235
    protected ?int $votes;
236
237
    /**
238
     * @var Collection|MessageAttachment[]
239
     *
240
     * @ORM\OneToMany(targetEntity="MessageAttachment", mappedBy="message")
241
     */
242
    protected Collection $attachments;
243
244
    /**
245
     * @var Collection|MessageFeedback[]
246
     *
247
     * @ORM\OneToMany(targetEntity="MessageFeedback", mappedBy="message", orphanRemoval=true)
248
     */
249
    protected Collection $likes;
250
251
    /**
252
     * @var Collection|MessageTag[]
253
     *
254
     * @ORM\ManyToMany(targetEntity="Chamilo\CoreBundle\Entity\MessageTag", inversedBy="messages", cascade={"persist"})
255
     * @ORM\JoinTable(name="message_rel_tags")
256
     */
257
    #[Groups(['message:read', 'message:write'])]
258
    protected Collection $tags;
259
260
    public function __construct()
261
    {
262
        $this->sendDate = new DateTime('now');
263
        $this->updateDate = $this->sendDate;
264
        $this->content = '';
265
        $this->attachments = new ArrayCollection();
266
        $this->children = new ArrayCollection();
267
        $this->tags = new ArrayCollection();
268
        $this->likes = new ArrayCollection();
269
        $this->receivers = new ArrayCollection();
270
        $this->votes = 0;
271
        $this->status = 0;
272
        $this->read = false;
273
        $this->starred = false;
274
    }
275
276
    /**
277
     * @return null|Collection|User[]
278
     */
279
    public function getReceivers()
280
    {
281
        return $this->receivers;
282
    }
283
284
    public function addReceiver(User $receiver): self
285
    {
286
        if (!$this->receivers->contains($receiver)) {
287
            $this->receivers->add($receiver);
288
        }
289
290
        return $this;
291
    }
292
293
    public function setReceivers($receivers): self
294
    {
295
        $this->receivers = $receivers;
296
297
        return $this;
298
    }
299
300
    /**
301
     * @return Collection|MessageTag[]
302
     */
303
    public function getTags()
304
    {
305
        return $this->tags;
306
    }
307
308
    public function addTag(MessageTag $tag): self
309
    {
310
        if (!$this->tags->contains($tag)) {
311
            $this->tags->add($tag);
312
        }
313
314
        return $this;
315
    }
316
317
    public function removeTag(MessageTag $tag): self
318
    {
319
        if ($this->tags->contains($tag)) {
320
            $this->tags->removeElement($tag);
321
        }
322
323
        return $this;
324
    }
325
326
    public function setSender(User $sender): self
327
    {
328
        $this->sender = $sender;
329
330
        return $this;
331
    }
332
333
    public function getSender(): User
334
    {
335
        return $this->sender;
336
    }
337
338
    public function setMsgType(int $msgType): self
339
    {
340
        $this->msgType = $msgType;
341
342
        return $this;
343
    }
344
345
    public function getMsgType(): int
346
    {
347
        return $this->msgType;
348
    }
349
350
    public function setSendDate(DateTime $sendDate): self
351
    {
352
        $this->sendDate = $sendDate;
353
354
        return $this;
355
    }
356
357
    /**
358
     * Get sendDate.
359
     *
360
     * @return DateTime
361
     */
362
    public function getSendDate()
363
    {
364
        return $this->sendDate;
365
    }
366
367
    public function setTitle(string $title): self
368
    {
369
        $this->title = $title;
370
371
        return $this;
372
    }
373
374
    public function getTitle(): string
375
    {
376
        return $this->title;
377
    }
378
379
    public function setContent(string $content): self
380
    {
381
        $this->content = $content;
382
383
        return $this;
384
    }
385
386
    public function getContent(): string
387
    {
388
        return $this->content;
389
    }
390
391
    public function setUpdateDate(DateTime $updateDate): self
392
    {
393
        $this->updateDate = $updateDate;
394
395
        return $this;
396
    }
397
398
    /**
399
     * Get updateDate.
400
     *
401
     * @return DateTime
402
     */
403
    public function getUpdateDate()
404
    {
405
        return $this->updateDate;
406
    }
407
408
    /**
409
     * Get id.
410
     *
411
     * @return int
412
     */
413
    public function getId()
414
    {
415
        return $this->id;
416
    }
417
418
    public function setVotes(int $votes): self
419
    {
420
        $this->votes = $votes;
421
422
        return $this;
423
    }
424
425
    public function getVotes(): int
426
    {
427
        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...
428
    }
429
430
    /**
431
     * Get attachments.
432
     *
433
     * @return Collection|MessageAttachment[]
434
     */
435
    public function getAttachments()
436
    {
437
        return $this->attachments;
438
    }
439
440
    public function addAttachment(MessageAttachment $attachment): self
441
    {
442
        $this->attachments->add($attachment);
443
        $attachment->setMessage($this);
444
445
        return $this;
446
    }
447
448
    public function getParent(): ?self
449
    {
450
        return $this->parent;
451
    }
452
453
    /**
454
     * @return Collection|Message[]
455
     */
456
    public function getChildren()
457
    {
458
        return $this->children;
459
    }
460
461
    public function addChild(self $child): self
462
    {
463
        $this->children[] = $child;
464
        $child->setParent($this);
465
466
        return $this;
467
    }
468
469
    public function setParent(self $parent = null): self
470
    {
471
        $this->parent = $parent;
472
473
        return $this;
474
    }
475
476
    /**
477
     * @return MessageFeedback[]|Collection
478
     */
479
    public function getLikes()
480
    {
481
        return $this->likes;
482
    }
483
484
    public function getGroup(): ?CGroup
485
    {
486
        return $this->group;
487
    }
488
489
    public function setGroup(?CGroup $group): self
490
    {
491
        $this->msgType = self::MESSAGE_TYPE_GROUP;
492
        $this->group = $group;
493
494
        return $this;
495
    }
496
497
    public function isRead(): bool
498
    {
499
        return $this->read;
500
    }
501
502
    public function setRead(bool $read): self
503
    {
504
        $this->read = $read;
505
506
        return $this;
507
    }
508
509
    public function isStarred(): bool
510
    {
511
        return $this->starred;
512
    }
513
514
    public function setStarred(bool $starred): self
515
    {
516
        $this->starred = $starred;
517
518
        return $this;
519
    }
520
521
    public function getStatus(): int
522
    {
523
        return $this->status;
524
    }
525
526
    public function setStatus(int $status): self
527
    {
528
        $this->status = $status;
529
530
        return $this;
531
    }
532
}
533