src/Entity/Chat.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\CommonTrait;
  4. use App\Repository\ChatRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. /**
  10.  * @ORM\Entity(repositoryClass=ChatRepository::class)
  11.  * @ORM\Table(name="app_chat")
  12.  * @UniqueEntity({"uuid"})
  13.  * @ORM\HasLifecycleCallbacks()
  14.  */
  15. class Chat
  16. {
  17.     use CommonTrait;
  18.     /**
  19.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="chats")
  20.      * @ORM\JoinColumn(nullable=false)
  21.      */
  22.     private $personOne;
  23.     /**
  24.      * @ORM\ManyToOne(targetEntity=User::class)
  25.      * @ORM\JoinColumn(nullable=false)
  26.      */
  27.     private $personTwo;
  28.     /**
  29.      * @ORM\OneToMany(targetEntity=ChatMessage::class, mappedBy="chat", orphanRemoval=true)
  30.      *
  31.      */
  32.     private $chatMessages;
  33.     /**
  34.      * @ORM\ManyToOne(targetEntity=Item::class)
  35.      * @ORM\JoinColumn(nullable=false)
  36.      */
  37.     private $item;
  38.     public function __construct()
  39.     {
  40.         $this->chatMessages = new ArrayCollection();
  41.     }
  42.     public function getPersonOne(): ?User
  43.     {
  44.         return $this->personOne;
  45.     }
  46.     public function setPersonOne(?User $personOne): self
  47.     {
  48.         $this->personOne $personOne;
  49.         return $this;
  50.     }
  51.     public function getPersonTwo(): ?User
  52.     {
  53.         return $this->personTwo;
  54.     }
  55.     public function setPersonTwo(?User $personTwo): self
  56.     {
  57.         $this->personTwo $personTwo;
  58.         return $this;
  59.     }
  60.     /**
  61.      * @return Collection|ChatMessage[]
  62.      */
  63.     public function getChatMessages(): Collection
  64.     {
  65.         return $this->chatMessages;
  66.     }
  67.     public function addChatMessage(ChatMessage $chatMessage): self
  68.     {
  69.         if (!$this->chatMessages->contains($chatMessage)) {
  70.             $this->chatMessages[] = $chatMessage;
  71.             $chatMessage->setChat($this);
  72.         }
  73.         return $this;
  74.     }
  75.     public function removeChatMessage(ChatMessage $chatMessage): self
  76.     {
  77.         if ($this->chatMessages->removeElement($chatMessage)) {
  78.             // set the owning side to null (unless already changed)
  79.             if ($chatMessage->getChat() === $this) {
  80.                 $chatMessage->setChat(null);
  81.             }
  82.         }
  83.         return $this;
  84.     }
  85.     public function getItem(): ?Item
  86.     {
  87.         return $this->item;
  88.     }
  89.     public function setItem(?Item $item): self
  90.     {
  91.         $this->item $item;
  92.         return $this;
  93.     }
  94.     public function getLastMessage()
  95.     {
  96.         $message $this->getChatMessages()[0];
  97.         foreach ($this->getChatMessages() as $chatMessage) {
  98.             if($chatMessage->getCreatedAt() > $message->getCreatedAt() || !$message) {
  99.                 $message $chatMessage;
  100.             }
  101.         }
  102.         return $message;
  103.     }
  104. }