Passed
Push — master ( 8eded5...da6942 )
by Angel Fernando Quiroz
12:19 queued 04:53
created

Message::getUpdateDate()   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\CoreBundle\Entity;
8
9
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter;
10
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
11
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
12
use ApiPlatform\Metadata\ApiFilter;
13
use ApiPlatform\Metadata\ApiResource;
14
use ApiPlatform\Metadata\Delete;
15
use ApiPlatform\Metadata\Get;
16
use ApiPlatform\Metadata\GetCollection;
17
use ApiPlatform\Metadata\Post;
18
use ApiPlatform\Metadata\Put;
19
use Chamilo\CoreBundle\Entity\Listener\MessageListener;
20
use Chamilo\CoreBundle\Filter\SearchOrFilter;
21
use Chamilo\CoreBundle\Repository\MessageRepository;
22
use Chamilo\CoreBundle\State\MessageByGroupStateProvider;
23
use Chamilo\CoreBundle\State\MessageProcessor;
24
use DateTime;
25
use Doctrine\Common\Collections\ArrayCollection;
26
use Doctrine\Common\Collections\Collection;
27
use Doctrine\Common\Collections\Criteria;
28
use Doctrine\ORM\Mapping as ORM;
29
use Gedmo\Mapping\Annotation as Gedmo;
30
use Symfony\Component\Serializer\Annotation\Groups;
31
use Symfony\Component\Validator\Constraints as Assert;
32
33
#[ORM\Table(name: 'message')]
34
#[ORM\Index(columns: ['user_sender_id'], name: 'idx_message_user_sender')]
35
#[ORM\Index(columns: ['group_id'], name: 'idx_message_group')]
36
#[ORM\Index(columns: ['msg_type'], name: 'idx_message_type')]
37
#[ORM\Entity(repositoryClass: MessageRepository::class)]
38
#[ORM\EntityListeners([MessageListener::class])]
39
#[ApiResource(
40
    operations: [
41
        new Get(security: "is_granted('VIEW', object)"),
42
        new Put(security: "is_granted('EDIT', object)"),
43
        new Delete(security: "is_granted('DELETE', object)"),
44
        new GetCollection(
45
            uriTemplate: '/messages',
46
            security: "is_granted('ROLE_USER')",
47
            name: 'get_all_messages'
48
        ),
49
        new GetCollection(
50
            uriTemplate: '/messages/by-group/list',
51
            security: "is_granted('ROLE_USER')",
52
            name: 'get_messages_by_social_group',
53
            provider: MessageByGroupStateProvider::class
54
        ),
55
        new Post(securityPostDenormalize: "is_granted('CREATE', object)"),
56
    ],
57
    normalizationContext: [
58
        'groups' => ['message:read'],
59
    ],
60
    denormalizationContext: [
61
        'groups' => ['message:write'],
62
    ],
63
    security: "is_granted('ROLE_USER')",
64
    processor: MessageProcessor::class,
65
)]
66
#[ApiFilter(filterClass: OrderFilter::class, properties: ['title', 'sendDate'])]
67
#[ApiFilter(SearchFilter::class, properties: [
68
    'msgType' => 'exact',
69
    'status' => 'exact',
70
    'sender' => 'exact',
71
    'receivers.receiver' => 'exact',
72
    'receivers.receiverType' => 'exact',
73
    'receivers.tags.tag' => 'exact',
74
    'parent' => 'exact',
75
])]
76
#[ApiFilter(
77
    BooleanFilter::class,
78
    properties: ['receivers.read']
79
)]
80
#[ApiFilter(SearchOrFilter::class, properties: ['title', 'content'])]
81
class Message
82
{
83
    public const MESSAGE_TYPE_INBOX = 1;
84
    public const MESSAGE_TYPE_GROUP = 5;
85
    public const MESSAGE_TYPE_INVITATION = 6;
86
    public const MESSAGE_TYPE_CONVERSATION = 7;
87
    // status
88
    public const MESSAGE_STATUS_DELETED = 3;
89
    public const MESSAGE_STATUS_DRAFT = 4;
90
    public const MESSAGE_STATUS_INVITATION_PENDING = 5;
91
    public const MESSAGE_STATUS_INVITATION_ACCEPTED = 6;
92
    public const MESSAGE_STATUS_INVITATION_DENIED = 7;
93
94
    #[ORM\Column(name: 'id', type: 'integer')]
95
    #[ORM\Id]
96
    #[ORM\GeneratedValue]
97
    #[Groups(['message:read'])]
98
    protected ?int $id = null;
99
100
    #[Assert\NotBlank]
101
    #[Groups(['message:read', 'message:write'])]
102
    #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'sentMessages')]
103
    #[ORM\JoinColumn(name: 'user_sender_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
104
    protected ?User $sender = null;
105
106
    /**
107
     * @var Collection<int, MessageRelUser>
108
     */
109
    #[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageRelUser::class, cascade: ['persist', 'remove'])]
110
    #[Groups(['message:write'])]
111
    protected Collection $receivers;
112
113
    #[Assert\NotBlank]
114
    #[Groups(['message:read', 'message:write'])]
115
    #[ORM\Column(name: 'msg_type', type: 'smallint', nullable: false)]
116
    protected int $msgType;
117
118
    #[Assert\NotBlank]
119
    #[Groups(['message:read', 'message:write'])]
120
    #[ORM\Column(name: 'status', type: 'smallint', nullable: false)]
121
    protected int $status;
122
123
    #[Groups(['message:read'])]
124
    #[ORM\Column(name: 'send_date', type: 'datetime', nullable: false)]
125
    protected DateTime $sendDate;
126
127
    #[Assert\NotBlank]
128
    #[Groups(['message:read', 'message:write'])]
129
    #[ORM\Column(name: 'title', type: 'string', length: 255, nullable: false)]
130
    protected string $title;
131
132
    #[Assert\NotBlank]
133
    #[Groups(['message:read', 'message:write'])]
134
    #[ORM\Column(name: 'content', type: 'text', nullable: false)]
135
    protected string $content;
136
137
    #[Groups(['message:read', 'message:write'])]
138
    #[ORM\ManyToOne(targetEntity: Usergroup::class)]
139
    #[ORM\JoinColumn(name: 'group_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
140
    protected ?Usergroup $group = null;
141
142
    /**
143
     * @var Collection<int, Message>
144
     */
145
    #[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
146
    protected Collection $children;
147
148
    #[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
149
    #[ORM\JoinColumn(name: 'parent_id', referencedColumnName: 'id')]
150
    protected ?Message $parent = null;
151
152
    #[Gedmo\Timestampable(on: 'update')]
153
    #[ORM\Column(name: 'update_date', type: 'datetime', nullable: true)]
154
    protected ?DateTime $updateDate;
155
156
    #[ORM\Column(name: 'votes', type: 'integer', nullable: true)]
157
    protected ?int $votes;
158
159
    /**
160
     * @var Collection<int, MessageAttachment>
161
     */
162
    #[Assert\Valid]
163
    #[Groups(['message:read', 'message:write'])]
164
    #[ORM\OneToMany(
165
        mappedBy: 'message',
166
        targetEntity: MessageAttachment::class,
167
        cascade: ['persist'],
168
        orphanRemoval: true,
169
    )]
170
    protected Collection $attachments;
171
172
    #[ORM\OneToMany(mappedBy: 'message', targetEntity: MessageFeedback::class, orphanRemoval: true)]
173
    protected Collection $likes;
174
175
    public function __construct()
176
    {
177
        $this->sendDate = new DateTime('now');
178
        $this->updateDate = $this->sendDate;
179
        $this->msgType = self::MESSAGE_TYPE_INBOX;
180
        $this->content = '';
181
        $this->attachments = new ArrayCollection();
182
        $this->children = new ArrayCollection();
183
        $this->receivers = new ArrayCollection();
184
        $this->likes = new ArrayCollection();
185
        $this->votes = 0;
186
        $this->status = 0;
187
    }
188
189
    /**
190
     * @return Collection<int, MessageRelUser>
191
     */
192
    public function getReceivers(): Collection
193
    {
194
        return $this->receivers;
195
    }
196
197
    public function setReceiversFromArray(array $receivers): self
198
    {
199
        $this->receivers = new ArrayCollection($receivers);
200
201
        return $this;
202
    }
203
204
    #[Groups(['message:read'])]
205
    public function getReceiversTo(): array
206
    {
207
        return $this->receivers
208
            ->filter(
209
                fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_TO === $messageRelUser->getReceiverType()
210
            )->getValues()
211
        ;
212
    }
213
214
    #[Groups(['message:read'])]
215
    public function getReceiversCc(): array
216
    {
217
        return $this->receivers
218
            ->filter(
219
                fn (MessageRelUser $messageRelUser) => MessageRelUser::TYPE_CC === $messageRelUser->getReceiverType()
220
            )
221
            ->getValues()
222
        ;
223
    }
224
225
    #[Groups(['message:read'])]
226
    public function getFirstReceiver(): ?MessageRelUser
227
    {
228
        if ($this->receivers->count() > 0) {
229
            return $this->receivers->first();
230
        }
231
232
        return null;
233
    }
234
235
    public function hasUserReceiver(User $receiver): bool
236
    {
237
        if ($this->receivers->count()) {
238
            $criteria = Criteria::create()
239
                ->where(
240
                    Criteria::expr()->eq('receiver', $receiver)
241
                )
242
                ->andWhere(
243
                    Criteria::expr()->eq('message', $this)
244
                )
245
            ;
246
247
            return $this->receivers->matching($criteria)->count() > 0;
248
        }
249
250
        return false;
251
    }
252
253
    public function addReceiverTo(User $receiver): self
254
    {
255
        $messageRelUser = (new MessageRelUser())
256
            ->setReceiver($receiver)
257
            ->setReceiverType(MessageRelUser::TYPE_TO)
258
        ;
259
260
        $this->addReceiver($messageRelUser);
261
262
        return $this;
263
    }
264
265
    public function addReceiver(MessageRelUser $messageRelUser): self
266
    {
267
        if (!$this->receivers->contains($messageRelUser)) {
268
            $this->receivers->add($messageRelUser);
269
270
            $messageRelUser->setMessage($this);
271
        }
272
273
        return $this;
274
    }
275
276
    public function addReceiverCc(User $receiver): self
277
    {
278
        $messageRelUser = (new MessageRelUser())
279
            ->setReceiver($receiver)
280
            ->setReceiverType(MessageRelUser::TYPE_CC)
281
        ;
282
283
        $this->addReceiver($messageRelUser);
284
285
        return $this;
286
    }
287
288
    public function removeReceiver(MessageRelUser $messageRelUser): self
289
    {
290
        $this->receivers->removeElement($messageRelUser);
291
292
        return $this;
293
    }
294
295
    public function getSender(): ?User
296
    {
297
        return $this->sender;
298
    }
299
300
    public function setSender(?User $sender): self
301
    {
302
        $this->sender = $sender;
303
304
        return $this;
305
    }
306
307
    public function getMsgType(): int
308
    {
309
        return $this->msgType;
310
    }
311
312
    public function setMsgType(int $msgType): self
313
    {
314
        $this->msgType = $msgType;
315
316
        return $this;
317
    }
318
319
    public function getSendDate(): DateTime
320
    {
321
        return $this->sendDate;
322
    }
323
324
    public function setSendDate(DateTime $sendDate): self
325
    {
326
        $this->sendDate = $sendDate;
327
328
        return $this;
329
    }
330
331
    public function getTitle(): string
332
    {
333
        return $this->title;
334
    }
335
336
    public function setTitle(string $title): self
337
    {
338
        $this->title = $title;
339
340
        return $this;
341
    }
342
343
    public function getContent(): string
344
    {
345
        return $this->content;
346
    }
347
348
    public function setContent(string $content): self
349
    {
350
        $this->content = $content;
351
352
        return $this;
353
    }
354
355
    public function getUpdateDate(): ?DateTime
356
    {
357
        return $this->updateDate;
358
    }
359
360
    public function setUpdateDate(DateTime $updateDate): self
361
    {
362
        $this->updateDate = $updateDate;
363
364
        return $this;
365
    }
366
367
    public function getId(): ?int
368
    {
369
        return $this->id;
370
    }
371
372
    public function getVotes(): int
373
    {
374
        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...
375
    }
376
377
    public function setVotes(int $votes): self
378
    {
379
        $this->votes = $votes;
380
381
        return $this;
382
    }
383
384
    /**
385
     * @return Collection<int, MessageAttachment>
386
     */
387
    public function getAttachments(): Collection
388
    {
389
        return $this->attachments;
390
    }
391
392
    public function addAttachment(MessageAttachment $attachment): static
393
    {
394
        if (!$this->attachments->contains($attachment)) {
395
            $this->attachments->add($attachment);
396
            $attachment
397
                ->setMessage($this)
398
                ->setParent($this->sender)
399
                ->setCreator($this->sender)
400
            ;
401
        }
402
403
        return $this;
404
    }
405
406
    public function removeAttachment(MessageAttachment $attachment): static
407
    {
408
        if ($this->attachments->removeElement($attachment)) {
409
            if ($attachment->getMessage() === $this) {
410
                $attachment->setMessage(null);
411
            }
412
        }
413
414
        return $this;
415
    }
416
417
    public function getParent(): ?self
418
    {
419
        return $this->parent;
420
    }
421
422
    public function setParent(?self $parent): self
423
    {
424
        $this->parent = $parent;
425
426
        return $this;
427
    }
428
429
    /**
430
     * @return Collection<int, Message>
431
     */
432
    public function getChildren(): Collection
433
    {
434
        return $this->children;
435
    }
436
437
    public function addChild(self $child): self
438
    {
439
        $this->children[] = $child;
440
        $child->setParent($this);
441
442
        return $this;
443
    }
444
445
    public function getGroup(): ?Usergroup
446
    {
447
        return $this->group;
448
    }
449
450
    public function setGroup(?Usergroup $group): self
451
    {
452
        //        $this->msgType = self::MESSAGE_TYPE_GROUP;
453
        $this->group = $group;
454
455
        return $this;
456
    }
457
458
    public function getStatus(): int
459
    {
460
        return $this->status;
461
    }
462
463
    public function setStatus(int $status): self
464
    {
465
        $this->status = $status;
466
467
        return $this;
468
    }
469
470
    public function getLikes(): Collection
471
    {
472
        return $this->likes;
473
    }
474
}
475