Passed
Push — master ( 44b866...954b89 )
by Julito
07:40
created

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