Passed
Push — master ( 4ed902...760dff )
by Julito
11:06 queued 11s
created

Message::setRead()   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
102
    /**
103
     * @ORM\Column(name="id", type="bigint")
104
     * @ORM\Id
105
     * @ORM\GeneratedValue()
106
     */
107
    #[Groups(['message:read'])]
108
    protected int $id;
109
110
    /**
111
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="sentMessages")
112
     * @ORM\JoinColumn(name="user_sender_id", referencedColumnName="id", nullable=false)
113
     */
114
    #[Assert\NotBlank]
115
    #[Groups(['message:read', 'message:write'])]
116
    protected User $userSender;
117
118
    /**
119
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", inversedBy="receivedMessages")
120
     * @ORM\JoinColumn(name="user_receiver_id", referencedColumnName="id", nullable=true)
121
     */
122
    #[Groups(['message:read', 'message:write'])]
123
    protected User $userReceiver;
124
125
    /**
126
     * @ORM\Column(name="msg_type", type="smallint", nullable=false)
127
     */
128
    #[Assert\NotBlank]
129
    #[Assert\Choice([
130
        self::MESSAGE_TYPE_INBOX,
131
        self::MESSAGE_TYPE_OUTBOX,
132
        self::MESSAGE_TYPE_PROMOTED,
133
    ])]
134
    /*#[ApiProperty(attributes: [
135
        'openapi_context' => [
136
            'type' => 'int',
137
            'enum' => [self::MESSAGE_TYPE_INBOX, self::MESSAGE_TYPE_OUTBOX],
138
        ],
139
    ])]*/
140
    #[Groups(['message:read', 'message:write'])]
141
    protected int $msgType;
142
143
    /**
144
     * @ORM\Column(name="msg_read", type="boolean", nullable=false)
145
     */
146
    #[Assert\NotNull]
147
    #[Groups(['message:read', 'message:write'])]
148
    protected bool $read;
149
150
    /**
151
     * @ORM\Column(name="starred", type="boolean", nullable=false)
152
     */
153
    #[Assert\NotNull]
154
    #[Groups(['message:read', 'message:write'])]
155
    protected bool $starred;
156
157
    /**
158
     * @ORM\Column(name="send_date", type="datetime", nullable=false)
159
     */
160
    #[Groups(['message:read'])]
161
    protected DateTime $sendDate;
162
163
    /**
164
     * @ORM\Column(name="title", type="string", length=255, nullable=false)
165
     */
166
    #[Assert\NotBlank]
167
    #[Groups(['message:read', 'message:write'])]
168
    protected string $title;
169
170
    /**
171
     * @ORM\Column(name="content", type="text", nullable=false)
172
     */
173
    #[Assert\NotBlank]
174
    #[Groups(['message:read', 'message:write'])]
175
    protected string $content;
176
177
    /**
178
     * @ORM\ManyToOne(targetEntity="Chamilo\CourseBundle\Entity\CGroup")
179
     * @ORM\JoinColumn(name="group_id", referencedColumnName="iid", nullable=true, onDelete="CASCADE")
180
     */
181
    protected ?CGroup $group = null;
182
183
    /**
184
     * @var Collection|Message[]
185
     * @ORM\OneToMany(targetEntity="Message", mappedBy="parent")
186
     */
187
    protected Collection $children;
188
189
    /**
190
     * @ORM\ManyToOne(targetEntity="Message", inversedBy="children")
191
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
192
     */
193
    protected ?Message $parent = null;
194
195
    /**
196
     * @Gedmo\Timestampable(on="update")
197
     * @ORM\Column(name="update_date", type="datetime", nullable=true)
198
     */
199
    protected ?DateTime $updateDate;
200
201
    /**
202
     * @ORM\Column(name="votes", type="integer", nullable=true)
203
     */
204
    protected ?int $votes;
205
206
    /**
207
     * @var Collection|MessageAttachment[]
208
     *
209
     * @ORM\OneToMany(targetEntity="MessageAttachment", mappedBy="message")
210
     */
211
    protected Collection $attachments;
212
213
    /**
214
     * @var Collection|MessageFeedback[]
215
     *
216
     * @ORM\OneToMany(targetEntity="MessageFeedback", mappedBy="message", orphanRemoval=true)
217
     */
218
    protected Collection $likes;
219
220
    /**
221
     * @var Collection|MessageTag[]
222
     *
223
     * @ORM\ManyToMany(targetEntity="Chamilo\CoreBundle\Entity\MessageTag", inversedBy="messages", cascade={"persist"})
224
     * @ORM\JoinTable(name="message_rel_tags")
225
     */
226
    #[Groups(['message:read', 'message:write'])]
227
    protected Collection $tags;
228
229
    public function __construct()
230
    {
231
        $this->sendDate = new DateTime('now');
232
        $this->updateDate = $this->sendDate;
233
        $this->content = '';
234
        $this->attachments = new ArrayCollection();
235
        $this->children = new ArrayCollection();
236
        $this->tags = new ArrayCollection();
237
        $this->likes = new ArrayCollection();
238
        $this->votes = 0;
239
        $this->read = false;
240
        $this->starred = false;
241
    }
242
243
    /**
244
     * @return Collection|MessageTag[]
245
     */
246
    public function getTags()
247
    {
248
        return $this->tags;
249
    }
250
251
    public function addTag(MessageTag $tag): self
252
    {
253
        if (!$this->tags->contains($tag)) {
254
            $this->tags->add($tag);
255
        }
256
257
        return $this;
258
    }
259
260
    public function removeTag(MessageTag $tag): self
261
    {
262
        if ($this->tags->contains($tag)) {
263
            $this->tags->removeElement($tag);
264
        }
265
266
        return $this;
267
    }
268
269
    public function setUserSender(User $userSender): self
270
    {
271
        $this->userSender = $userSender;
272
273
        return $this;
274
    }
275
276
    public function getUserSender(): User
277
    {
278
        return $this->userSender;
279
    }
280
281
    public function setUserReceiver(User $userReceiver): self
282
    {
283
        $this->userReceiver = $userReceiver;
284
285
        return $this;
286
    }
287
288
    /**
289
     * Get userReceiver.
290
     *
291
     * @return User
292
     */
293
    public function getUserReceiver()
294
    {
295
        return $this->userReceiver;
296
    }
297
298
    public function setMsgType(int $msgType): self
299
    {
300
        $this->msgType = $msgType;
301
302
        return $this;
303
    }
304
305
    public function getMsgType(): int
306
    {
307
        return $this->msgType;
308
    }
309
310
    public function setSendDate(DateTime $sendDate): self
311
    {
312
        $this->sendDate = $sendDate;
313
314
        return $this;
315
    }
316
317
    /**
318
     * Get sendDate.
319
     *
320
     * @return DateTime
321
     */
322
    public function getSendDate()
323
    {
324
        return $this->sendDate;
325
    }
326
327
    public function setTitle(string $title): self
328
    {
329
        $this->title = $title;
330
331
        return $this;
332
    }
333
334
    public function getTitle(): string
335
    {
336
        return $this->title;
337
    }
338
339
    public function setContent(string $content): self
340
    {
341
        $this->content = $content;
342
343
        return $this;
344
    }
345
346
    /**
347
     * Get content.
348
     *
349
     * @return string
350
     */
351
    public function getContent()
352
    {
353
        return $this->content;
354
    }
355
356
    public function setUpdateDate(DateTime $updateDate): self
357
    {
358
        $this->updateDate = $updateDate;
359
360
        return $this;
361
    }
362
363
    /**
364
     * Get updateDate.
365
     *
366
     * @return DateTime
367
     */
368
    public function getUpdateDate()
369
    {
370
        return $this->updateDate;
371
    }
372
373
    /**
374
     * Get id.
375
     *
376
     * @return int
377
     */
378
    public function getId()
379
    {
380
        return $this->id;
381
    }
382
383
    public function setVotes(int $votes): self
384
    {
385
        $this->votes = $votes;
386
387
        return $this;
388
    }
389
390
    public function getVotes(): int
391
    {
392
        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...
393
    }
394
395
    /**
396
     * Get attachments.
397
     *
398
     * @return Collection|MessageAttachment[]
399
     */
400
    public function getAttachments()
401
    {
402
        return $this->attachments;
403
    }
404
405
    public function addAttachment(MessageAttachment $attachment): self
406
    {
407
        $this->attachments->add($attachment);
408
        $attachment->setMessage($this);
409
410
        return $this;
411
    }
412
413
    public function getParent(): ?self
414
    {
415
        return $this->parent;
416
    }
417
418
    /**
419
     * @return Collection|Message[]
420
     */
421
    public function getChildren()
422
    {
423
        return $this->children;
424
    }
425
426
    public function addChild(self $child): self
427
    {
428
        $this->children[] = $child;
429
        $child->setParent($this);
430
431
        return $this;
432
    }
433
434
    public function setParent(self $parent = null): self
435
    {
436
        $this->parent = $parent;
437
438
        return $this;
439
    }
440
441
    /**
442
     * @return MessageFeedback[]|Collection
443
     */
444
    public function getLikes()
445
    {
446
        return $this->likes;
447
    }
448
449
    public function getGroup(): ?CGroup
450
    {
451
        return $this->group;
452
    }
453
454
    public function setGroup(?CGroup $group): self
455
    {
456
        $this->group = $group;
457
458
        return $this;
459
    }
460
461
    public function isRead(): bool
462
    {
463
        return $this->read;
464
    }
465
466
    public function setRead(bool $read): self
467
    {
468
        $this->read = $read;
469
470
        return $this;
471
    }
472
473
    public function isStarred(): bool
474
    {
475
        return $this->starred;
476
    }
477
478
    public function setStarred(bool $starred): self
479
    {
480
        $this->starred = $starred;
481
482
        return $this;
483
    }
484
}
485