<?php
namespace App\Entity\Messaging;
use App\Entity\AdminUser;
use App\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\App\Messaging\ConversationRepository")
*/
class Conversation
{
public const STATUS_WAIT_CUSTOMER = 'WAIT_CUSTOMER';
public const STATUS_WAIT_SCP = 'WAIT_SCP';
public const STATUS_TERMINATE = 'TERMINATE';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
protected $status = self::STATUS_WAIT_SCP;
/**
* @ORM\Column(type="datetime")
* @Assert\NotBlank
*/
protected $updatedAt;
/**
* @ORM\Column(type="datetime")
* @Assert\NotBlank
*/
protected $viewAt;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank
*/
protected $object;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Messaging\Subject", inversedBy="conversations")
*/
protected $subject;
/**
* @ORM\Column(type="json", nullable=true)
*
* @var array
*/
protected $contractNumbers = [];
/**
* @ORM\Column(type="boolean", nullable=true)
*/
protected $allContracts;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Messaging\Team")
*/
protected $team;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @ORM\JoinColumn(nullable=false, name="customer_user")
* @Assert\NotBlank
*/
protected $user;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Messaging\Message", mappedBy="conversation", orphanRemoval=true, cascade={"persist"})
* @ORM\OrderBy({"date" = "ASC"})
*/
protected $messages;
/**
* @ORM\Column(type="boolean")
* @Assert\Type(type="boolean")
*/
protected $claim = false;
/**
* @ORM\Column(type="boolean")
* @Assert\Type(type="boolean")
*/
protected $hasAttachments = false;
public function __construct(
User|AdminUser|null $user = null,
?Subject $subject = null
) {
if (null !== $subject) {
$this->setSubject($subject);
}
if (null !== $user) {
$this->setUser($user);
}
$this->messages = new ArrayCollection();
$now = new \DateTimeImmutable();
$this->viewAt = $now;
$this->updatedAt = $now;
}
public function getId(): ?int
{
return $this->id;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getViewAt(): ?\DateTimeInterface
{
return $this->viewAt;
}
public function setViewAt(\DateTimeInterface $viewAt): self
{
$this->viewAt = $viewAt;
return $this;
}
public function getObject(): ?string
{
$theme = $this->subject?->getTheme()?->getLabel();
if (!is_null($theme)) {
return str_replace(sprintf('{%s}', Theme::THEME_STAMP), $theme, $this->object);
}
return $this->object;
}
public function setObject(string $object): self
{
$this->object = $object;
return $this;
}
public function getSubject(): ?Subject
{
return $this->subject;
}
public function setSubject(?Subject $subject): self
{
$this->subject = $subject;
return $this;
}
/**
* The contractId property was removed, but the getter could still be needed
* for compatibility (eg. serialization)
*/
public function getContractId(): ?string
{
return null;
}
public function getContractNumbers(): ?array
{
return $this->contractNumbers;
}
public function setContractNumbers(array $contractNumbers): self
{
$this->contractNumbers = $contractNumbers;
return $this;
}
public function isAllContracts()
{
return $this->allContracts;
}
public function setAllContracts(?bool $allContracts): self
{
$this->allContracts = $allContracts;
return $this;
}
public function getTeam(): ?Team
{
return $this->team;
}
public function setTeam(?Team $team): self
{
$this->team = $team;
return $this;
}
public function getUser(): User|AdminUser|null
{
return $this->user;
}
public function setUser(User|AdminUser|null $user): self
{
$this->user = $user;
return $this;
}
public function setClaim(bool $claim): self
{
$this->claim = $claim;
return $this;
}
public function isClaim(): bool
{
return $this->claim;
}
/**
* @return Collection|Message[]
*/
public function getMessages(): Collection
{
return $this->messages;
}
public function addMessage(Message $message): self
{
if (!$this->messages->contains($message)) {
$this->messages[] = $message;
$this->setUpdatedAt(new \DateTimeImmutable());
$message->setConversation($this);
}
return $this;
}
public function removeMessage(Message $message): self
{
if ($this->messages->contains($message)) {
$this->messages->removeElement($message);
// set the owning side to null (unless already changed)
if ($message->getConversation() === $this) {
$message->setConversation(null);
}
}
return $this;
}
public function hasAttachments(): bool
{
return $this->hasAttachments;
}
public function setHasAttachments(bool $hasAttachments): self
{
$this->hasAttachments = $hasAttachments;
return $this;
}
public function isRead(): bool
{
return $this->getViewAt() > $this->getUpdatedAt();
}
public function getEntityName(): string
{
return 'Conversation';
}
}