<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\ChatMessageRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=ChatMessageRepository::class)
* @ApiResource(
* mercure={
* "private"=true,
* "normalization_context"={
* "groups"={
* "chat_message:read:id",
* "chat_message:read:content",
* "chat_message:read:received",
* "chat_message:read:read",
* "chat_message:read:createdAt",
* "chat_message:read:user",
* "user:read:id",
* "user:read:firstName",
* "user:read:lastName",
* "user:read:client",
* "client:read:id",
* "client:read:firstName",
* "client:read:lastName",
* "client:read:avatar",
* "chat_message:read:attachments",
* "media_object:read:contentUrl"
* }
* }
* },
* order={"createdAt": "DESC"},
* normalizationContext={
* "groups"={"chat_message:read"}
* },
* denormalizationContext={
* "groups"={
* "chat_message:read",
* "chat_message:read:attachments",
* "media_object:read:contentUrl"
* }
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_USER')"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_USER')",
* "path"="/chat_messages",
* "method"="POST",
* "controller"="App\Controller\Api\ChatMessageController::post"
* },
* "get_by_chat"={
* "description"="Get ChatMessages by Chat.",
* "path"="/chats/{id}/chat_messages",
* "method"="GET",
* "controller"="App\Controller\Api\ChatMessageController::getChatMessagesByChat",
* "security"="is_granted('ROLE_USER')"
* }
* }
* )
* @ApiFilter(SearchFilter::class, properties={
* "chat.id", "user.id", "user.active"
* })
* @ApiFilter(OrderFilter::class)
* @ApiFilter(GroupFilter::class,
* arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* }
* )
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class ChatMessage
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @ApiProperty(identifier=true)
* @Groups({"chat_message:read", "chat_message:read:id", "chat:read"})
*/
private ?int $id = null;
/**
* @ORM\ManyToOne(targetEntity=Chat::class, inversedBy="chatMessages", cascade={"persist", "refresh"})
* @Groups({"chat_message:read", "chat_message:write", "chat_message:read:chat"})
*/
private ?Chat $chat = null;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @Assert\NotNull
* @Groups({"chat_message:read", "chat_message:write", "chat_message:read:user", "chat:read"})
*/
private ?User $user;
/**
* @ORM\Column(name="`content`", type="text", nullable=true)
* @Groups({"chat_message:read", "chat_message:write", "chat_message:read:content", "chat:read"})
*/
private ?string $content = "";
/**
* @ORM\ManyToMany(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
* @Groups({"chat_message:read", "chat_message:read:attachments", "chat:read"})
*/
private $attachments;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="create")
* @Groups({"chat_message:read", "chat_message:read:createdAt", "chat:read"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"chat_message:read", "chat_message:read:updatedAt", "chat:read"})
*/
private $updatedAt;
/**
* Post Custom Field
* Permet d'envoyer un message à un autre utilisateur.
* @Groups({"chat_message:read", "chat_message:write"})
*/
private $recipient;
/**
* Post Custom Field
* Permet d'envoyer un message à une Team.
* @Groups({"chat_message:read", "chat_message:write"})
*/
private $team;
/**
* Post Custom Field
* @Groups({"chat_message:read""chat_message:read:received", "chat:read"})
*/
private $received = false;
/**
* Post Custom Field
* @Groups({"chat_message:read""chat_message:read:read", "chat:read"})
*/
private $read = false;
public function __construct()
{
$this->attachments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getChat(): ?Chat
{
return $this->chat;
}
public function setChat(?Chat $chat): self
{
$this->chat = $chat;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
/**
* @return Collection<int, MediaObject>
*/
public function getAttachments(): Collection
{
return $this->attachments;
}
/**
* @Groups({"chat_message:write"})
*/
public function setAttachmentFile($file): self
{
$mediaObject = new MediaObject();
$mediaObject->setFile($file);
$this->addAttachment($mediaObject);
return $this;
}
/**
* @Groups({"chat_message:write"})
*/
public function setAttachmentFiles(?array $files): self
{
// Supprime tous les MediaObject
if (!$this->attachments->isEmpty()) {
foreach ($this->attachments->toArray() as $attachment) {
$this->removeAttachment($attachment);
}
}
foreach ($files as $file) {
$this->setAttachmentFile($file);
}
return $this;
}
public function addAttachment(MediaObject $attachment): self
{
if (!$this->attachments->contains($attachment)) {
$this->attachments[] = $attachment;
}
return $this;
}
public function removeAttachment(MediaObject $attachment): self
{
$this->attachments->removeElement($attachment);
return $this;
}
public function getReceived(): ?bool
{
return $this->received;
}
/**
* @Groups({"chat_message:read", "chat_message:read:isReceived"})
*/
public function isReceived(): ?bool
{
return $this->received;
}
public function setReceived(?bool $received): self
{
$this->received = $received;
return $this;
}
public function getRead(): ?bool
{
return $this->read;
}
/**
* @Groups({"chat_message:read", "chat_message:read:isRead"})
*/
public function isRead(): ?bool
{
return $this->read;
}
public function setRead(?bool $read): self
{
$this->read = $read;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getTeam(): ?Team
{
return $this->team;
}
public function setTeam(?Team $team): self
{
$this->team = $team;
return $this;
}
public function getRecipient(): ?User
{
return $this->recipient;
}
public function setRecipient(?User $recipient): self
{
$this->recipient = $recipient;
return $this;
}
}