Passed
Push — master ( bb9802...b428f1 )
by Julito
09:11
created

MessageRelUser::isRead()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
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\SearchFilter;
12
use Doctrine\Common\Collections\ArrayCollection;
13
use Doctrine\Common\Collections\Collection;
14
use Doctrine\ORM\Mapping as ORM;
15
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
16
use Symfony\Component\Serializer\Annotation\Groups;
17
use Symfony\Component\Validator\Constraints as Assert;
18
19
/**
20
 * @ORM\Table(name="message_rel_user", indexes={
21
 * },
22
 * uniqueConstraints={
23
 *     @ORM\UniqueConstraint(name="message_receiver", columns={"message_id", "user_id"})
24
 *  },
25
 * )
26
 * @ORM\Entity()
27
 */
28
#[UniqueEntity(
29
    fields: ['message', 'receiver'],
30
    errorPath: 'message',
31
    message: 'This message-receiver relation is already used.',
32
)]
33
// @todo add security checks.
34
#[ApiResource]
35
#[ApiFilter(SearchFilter::class, properties: [
36
    'star' => 'exact',
37
    'receiver' => 'exact',
38
    'read' => 'exact',
39
    'starred' => 'exact',
40
    'tags.tag' => 'exact',
41
])]
42
class MessageRelUser
43
{
44
    /**
45
     * @ORM\Column(name="id", type="bigint")
46
     * @ORM\Id
47
     * @ORM\GeneratedValue()
48
     */
49
    #[Groups(['message:read', 'message:write'])]
50
    protected ?int $id = null;
51
52
    /**
53
     * @ORM\ManyToOne(targetEntity="Message", inversedBy="receivers", cascade={"persist"})
54
     * @ORM\JoinColumn(name="message_id", referencedColumnName="id", nullable=false)
55
     */
56
    protected Message $message;
57
58
    /**
59
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\User", cascade={"persist"})
60
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
61
     */
62
    #[Assert\NotNull]
63
    #[Groups(['message:read', 'message:write'])]
64
    protected User $receiver;
65
66
    /**
67
     * @ORM\Column(name="msg_read", type="boolean", nullable=false)
68
     */
69
    #[Groups(['message:read', 'message:write'])]
70
    protected bool $read;
71
72
    /**
73
     * @ORM\Column(name="starred", type="boolean", nullable=false)
74
     */
75
    #[Groups(['message:read', 'message:write'])]
76
    protected bool $starred;
77
78
    /**
79
     * @var Collection|MessageTag[]
80
     *
81
     * @ORM\ManyToMany(targetEntity="MessageTag", inversedBy="messageRelUsers", cascade={"persist"})
82
     * @ORM\JoinTable(name="message_rel_user_rel_tags")
83
     */
84
    #[Assert\Valid]
85
    #[Groups(['message:read', 'message:write'])]
86
    protected Collection $tags;
87
88
    public function __construct()
89
    {
90
        $this->tags = new ArrayCollection();
91
        $this->read = false;
92
        $this->starred = false;
93
    }
94
95
    public function getId(): ?int
96
    {
97
        return $this->id;
98
    }
99
100
    /**
101
     * @return Collection|MessageTag[]
102
     */
103
    public function getTags()
104
    {
105
        return $this->tags;
106
    }
107
108
    public function addTag(MessageTag $tag): self
109
    {
110
        if (!$this->tags->contains($tag)) {
111
            $this->tags->add($tag);
112
        }
113
114
        return $this;
115
    }
116
117
    public function removeTag(MessageTag $tag): self
118
    {
119
        if ($this->tags->contains($tag)) {
120
            $this->tags->removeElement($tag);
121
        }
122
123
        return $this;
124
    }
125
126
    public function isRead(): bool
127
    {
128
        return $this->read;
129
    }
130
131
    public function setRead(bool $read): self
132
    {
133
        $this->read = $read;
134
135
        return $this;
136
    }
137
138
    public function isStarred(): bool
139
    {
140
        return $this->starred;
141
    }
142
143
    public function setStarred(bool $starred): self
144
    {
145
        $this->starred = $starred;
146
147
        return $this;
148
    }
149
150
    public function getMessage(): Message
151
    {
152
        return $this->message;
153
    }
154
155
    public function setMessage(Message $message): self
156
    {
157
        $this->message = $message;
158
159
        return $this;
160
    }
161
162
    public function getReceiver(): User
163
    {
164
        return $this->receiver;
165
    }
166
167
    public function setReceiver(User $receiver): self
168
    {
169
        $this->receiver = $receiver;
170
171
        return $this;
172
    }
173
}
174