<?php
namespace App\Entity\Chat;
use App\Entity\Account\User;
use App\Entity\Channel\Channel;
use App\Repository\Chat\ConversationRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\InheritanceType;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameInterface;
use DrosalysWeb\ObjectExtensions\Blame\Model\BlameTrait;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampInterface;
use DrosalysWeb\ObjectExtensions\Timestamp\Model\TimestampTrait;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation as Serial;
#[ORM\Entity(repositoryClass: ConversationRepository::class)]
#[ORM\Table(name: 'chat_conversations')]
#[InheritanceType('JOINED')]
#[DiscriminatorColumn(name: 'dtype', type: 'string')]
#[DiscriminatorMap([
'conversation' => Conversation::class,
'service_conversation' => ServiceConversation::class,
'canal' => Canal::class,
'temporary_canal' => TemporaryCanal::class
])]
class Conversation implements TimestampInterface, BlameInterface
{
use TimestampTrait;
use BlameTrait;
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Serial\Groups(groups: ['Conversation', 'Identifier'])]
private ?int $id = null;
#[ORM\ManyToOne(targetEntity: Channel::class)]
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
private ?Channel $channel = null;
#[ORM\Column(type: 'integer')]
#[
Assert\NotBlank(message: 'entity.tchat.canal.type.NotBlank'),
Assert\Type(
type: "integer"
)
]
#[Serial\Groups(groups: ['Conversation', 'PostCanal', 'Identifier'])]
private ?int $type = null;
#[ORM\OneToMany(mappedBy: 'conversation', targetEntity: ConversationUser::class, cascade: ['persist', 'remove'], orphanRemoval: true)]
#[Serial\Groups(['PostCanal'])]
private ?Collection $conversationUsers;
#[ORM\OneToMany(mappedBy: 'conversation', targetEntity: Message::class, cascade: ['persist', 'remove'], fetch: 'EXTRA_LAZY', orphanRemoval: true)]
private ?Collection $messages;
public function __construct()
{
$this->conversationUsers = new ArrayCollection();
$this->messages = new ArrayCollection();
}
public function hasConversationUser(User $user): bool
{
$userAlreadyIn = $this->getConversationUsers()->filter(function(ConversationUser $conversationUser) use ($user) {
return $conversationUser->getUser()->getId() === $user->getId();
})->first();
return $userAlreadyIn !== false;
}
#[Serial\Groups(groups: ['Conversation'])]
public function getCountConversationUsers(): int
{
return $this->conversationUsers->count();
}
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection<int, ConversationUser>
*/
public function getConversationUsers(): Collection
{
return $this->conversationUsers;
}
public function getConversationUserIds(): array
{
return array_map(function(ConversationUser $conversationUser) {
return $conversationUser->getUser()->getId();
}, $this->conversationUsers->toArray());
}
public function addConversationUser(ConversationUser $conversationUser): self
{
if (!$this->conversationUsers->contains($conversationUser)) {
$this->conversationUsers[] = $conversationUser;
$conversationUser->setConversation($this);
}
return $this;
}
public function removeConversationUser(ConversationUser $conversationUser): self
{
if ($this->conversationUsers->removeElement($conversationUser)) {
// set the owning side to null (unless already changed)
if ($conversationUser->getConversation() === $this) {
$conversationUser->setConversation(null);
}
}
return $this;
}
public function getConversationUserByUser(User $user): ?ConversationUser
{
$conversationUsersToRemove = $this->conversationUsers->filter(function(ConversationUser $conversationUser) use ($user) {
return $conversationUser->getUser()->getId() === $user->getId();
})->first();
if ($conversationUsersToRemove) {
return $conversationUsersToRemove;
}
return null;
}
public function getMessages(): ?Collection
{
return $this->messages;
}
public function setMessages(?Collection $messages): Conversation
{
$this->messages = $messages;
return $this;
}
public function getChannel(): ?Channel
{
return $this->channel;
}
public function setChannel(?Channel $channel): Conversation
{
$this->channel = $channel;
return $this;
}
}