1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ProjetNormandie\MessageBundle\Entity; |
4
|
|
|
|
5
|
|
|
use ApiPlatform\Metadata\ApiResource; |
6
|
|
|
use ApiPlatform\Metadata\ApiFilter; |
7
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\BooleanFilter; |
8
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter; |
9
|
|
|
use ApiPlatform\Metadata\Get; |
10
|
|
|
use ApiPlatform\Metadata\GetCollection; |
11
|
|
|
use ApiPlatform\Metadata\Post; |
12
|
|
|
use ApiPlatform\Metadata\Put; |
13
|
|
|
use Doctrine\ORM\Mapping as ORM; |
14
|
|
|
use Gedmo\Timestampable\Traits\TimestampableEntity; |
15
|
|
|
use ProjetNormandie\MessageBundle\Controller\User\GetInboxMessages; |
16
|
|
|
use ProjetNormandie\MessageBundle\Controller\User\GetOutboxMessages; |
17
|
|
|
use ProjetNormandie\MessageBundle\Repository\MessageRepository; |
18
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
19
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
20
|
|
|
|
21
|
|
|
#[ORM\Index(name: "idx_inbox", columns: ["recipient_id", 'is_deleted_recipient'])] |
22
|
|
|
#[ORM\Index(name: "idx_outbox", columns: ["sender_id", 'is_deleted_sender'])] |
23
|
|
|
#[ORM\Index(name: "idx_newMessage", columns: ["recipient_id", 'is_opened'])] |
24
|
|
|
#[ORM\Index(name: "idx_inbox_type", columns: ["recipient_id", "is_deleted_recipient", "type"])] |
25
|
|
|
#[ORM\Index(name: "idx_outbox_type", columns: ["sender_id", "is_deleted_sender", "type"])] |
26
|
|
|
#[ORM\Index(name: "idx_inbox_opened", columns: ["recipient_id", "is_deleted_recipient", "is_opened"])] |
27
|
|
|
#[ORM\Index(name: "idx_outbox_opened", columns: ["sender_id", "is_deleted_sender", "is_opened"])] |
28
|
|
|
#[ORM\Table(name:'pnm_message')] |
29
|
|
|
#[ORM\Entity(repositoryClass: MessageRepository::class)] |
30
|
|
|
#[ORM\EntityListeners(["ProjetNormandie\MessageBundle\EventListener\Entity\MessageListener"])] |
31
|
|
|
#[ApiResource( |
32
|
|
|
order: ['id' => 'DESC'], |
33
|
|
|
operations: [ |
34
|
|
|
new GetCollection( |
35
|
|
|
security: 'is_granted("ROLE_USER")', |
36
|
|
|
paginationItemsPerPage: 10 |
37
|
|
|
), |
38
|
|
|
new GetCollection( |
39
|
|
|
uriTemplate: '/messages/inbox', |
40
|
|
|
controller: GetInboxMessages::class, |
41
|
|
|
read: false, |
42
|
|
|
security: 'is_granted("ROLE_USER")', |
43
|
|
|
paginationItemsPerPage: 10 |
44
|
|
|
), |
45
|
|
|
new GetCollection( |
46
|
|
|
uriTemplate: '/messages/outbox', |
47
|
|
|
controller: GetOutboxMessages::class, |
48
|
|
|
read: false, |
49
|
|
|
security: 'is_granted("ROLE_USER")', |
50
|
|
|
paginationItemsPerPage: 10 |
51
|
|
|
), |
52
|
|
|
new Get( |
53
|
|
|
security: 'is_granted("ROLE_USER") and (object.getSender() == user or object.getRecipient() == user)' |
54
|
|
|
), |
55
|
|
|
new Post( |
56
|
|
|
denormalizationContext: ['groups' => ['message:insert']], |
57
|
|
|
security: 'is_granted("ROLE_USER")' |
58
|
|
|
), |
59
|
|
|
new Put( |
60
|
|
|
denormalizationContext: ['groups' => ['message:update']], |
61
|
|
|
security: 'is_granted("ROLE_USER") and (object.getSender() == user or object.getRecipient() == user)' |
62
|
|
|
) |
63
|
|
|
], |
64
|
|
|
normalizationContext: ['groups' => ['message:read', 'user:read', 'message:recipient', 'message:sender']] |
65
|
|
|
)] |
66
|
|
|
#[ApiFilter( |
67
|
|
|
SearchFilter::class, |
68
|
|
|
properties: [ |
69
|
|
|
'sender' => 'exact', |
70
|
|
|
'recipient' => 'exact', |
71
|
|
|
'type' => 'exact', |
72
|
|
|
'object' => 'partial' |
73
|
|
|
] |
74
|
|
|
)] |
75
|
|
|
#[ApiFilter(BooleanFilter::class, properties: ['isDeletedSender', 'isDeletedRecipient', 'isOpened'])] |
76
|
|
|
class Message |
77
|
|
|
{ |
78
|
|
|
use TimestampableEntity; |
79
|
|
|
|
80
|
|
|
#[Groups(['message:read', 'message:update'])] |
81
|
|
|
#[ORM\Id, ORM\Column, ORM\GeneratedValue] |
82
|
|
|
private ?int $id = null; |
83
|
|
|
|
84
|
|
|
#[Groups(['message:read', 'message:insert'])] |
85
|
|
|
#[Assert\Length(max: 255)] |
86
|
|
|
#[ORM\Column(length: 255, nullable: false)] |
87
|
|
|
private ?string $object; |
88
|
|
|
|
89
|
|
|
#[Groups(['message:read', 'message:insert'])] |
90
|
|
|
#[ORM\Column(type:'text', nullable: true)] |
91
|
|
|
private ?string $message; |
92
|
|
|
|
93
|
|
|
#[Groups(['message:read'])] |
94
|
|
|
#[Assert\Length(max: 50)] |
95
|
|
|
#[ORM\Column(length: 50, nullable: false, options: ['default' => 'DEFAULT'])] |
96
|
|
|
private ?string $type = 'DEFAULT'; |
97
|
|
|
|
98
|
|
|
#[Groups(['message:read'])] |
99
|
|
|
#[ORM\ManyToOne(targetEntity: UserInterface::class, fetch: 'EAGER')] |
100
|
|
|
#[ORM\JoinColumn(name:'sender_id', referencedColumnName:'id', nullable:false)] |
101
|
|
|
private $sender; |
102
|
|
|
|
103
|
|
|
#[Groups(['message:read', 'message:insert'])] |
104
|
|
|
#[ORM\ManyToOne(targetEntity: UserInterface::class, fetch: 'EAGER')] |
105
|
|
|
#[ORM\JoinColumn(name:'recipient_id', referencedColumnName:'id', nullable:false)] |
106
|
|
|
private $recipient; |
107
|
|
|
|
108
|
|
|
#[Groups(['message:read', 'message:update'])] |
109
|
|
|
#[ORM\Column(nullable: false, options: ['default' => false])] |
110
|
|
|
private bool $isOpened = false; |
111
|
|
|
|
112
|
|
|
#[Groups(['message:read', 'message:update'])] |
113
|
|
|
#[ORM\Column(nullable: false, options: ['default' => false])] |
114
|
|
|
private bool $isDeletedSender = false; |
115
|
|
|
|
116
|
|
|
#[Groups(['message:read', 'message:update'])] |
117
|
|
|
#[ORM\Column(nullable: false, options: ['default' => false])] |
118
|
|
|
private bool $isDeletedRecipient = false; |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
public function __toString() |
122
|
|
|
{ |
123
|
|
|
return sprintf('Message [%s]', $this->id); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function setId(int $id): void |
127
|
|
|
{ |
128
|
|
|
$this->id = $id; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function getId(): ?int |
132
|
|
|
{ |
133
|
|
|
return $this->id; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function setObject(string $object): void |
137
|
|
|
{ |
138
|
|
|
$this->object = $object; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getObject(): ?string |
142
|
|
|
{ |
143
|
|
|
return $this->object; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function setType(string $type): void |
147
|
|
|
{ |
148
|
|
|
$this->type = $type; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getType(): ?string |
152
|
|
|
{ |
153
|
|
|
return $this->type; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setMessage(string $message): void |
157
|
|
|
{ |
158
|
|
|
$this->message = $message; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getMessage(): ?string |
162
|
|
|
{ |
163
|
|
|
return $this->message; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getSender() |
167
|
|
|
{ |
168
|
|
|
return $this->sender; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setSender($sender): void |
172
|
|
|
{ |
173
|
|
|
$this->sender = $sender; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getRecipient() |
177
|
|
|
{ |
178
|
|
|
return $this->recipient; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function setRecipient($recipient): void |
182
|
|
|
{ |
183
|
|
|
$this->recipient = $recipient; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setIsOpened(bool $isOpened): void |
187
|
|
|
{ |
188
|
|
|
$this->isOpened = $isOpened; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function getIsOpened(): bool |
192
|
|
|
{ |
193
|
|
|
return $this->isOpened; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function setIsDeletedSender(bool $isDeletedSender): void |
197
|
|
|
{ |
198
|
|
|
$this->isDeletedSender = $isDeletedSender; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function getIsDeletedSender(): bool |
202
|
|
|
{ |
203
|
|
|
return $this->isDeletedSender; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function setIsDeletedRecipient(bool $isDeletedRecipient): void |
207
|
|
|
{ |
208
|
|
|
$this->isDeletedRecipient = $isDeletedRecipient; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getIsDeletedRecipient(): bool |
212
|
|
|
{ |
213
|
|
|
return $this->isDeletedRecipient; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|