src/Entity/Account/SupportMessage.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Account;
  3. use App\Entity\User;
  4. use App\Repository\SupportMessagesRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Doctrine\ORM\PersistentCollection;
  9. #[ORM\Table(name'support_messages')]
  10. #[ORM\Entity(repositoryClassSupportMessagesRepository::class)]
  11. class SupportMessage
  12. {
  13.     #[ORM\Id]
  14.     #[ORM\Column(name'id'type'integer')]
  15.     #[ORM\GeneratedValue(strategy'AUTO')]
  16.     protected int $id;
  17.     #[ORM\JoinColumn(name'parent_id'referencedColumnName'id'nullabletrue)]
  18.     #[ORM\ManyToOne(targetEntitySupportMessage::class, inversedBy'messages'cascade: ['all'])]
  19.     protected ?SupportMessage $parent;
  20.     #[ORM\OneToMany(targetEntitySupportMessage::class, mappedBy'parent'cascade: ['all'], orphanRemovaltrue)]
  21.     #[ORM\OrderBy(['createdAt' => 'ASC'])]
  22.     protected Collection $messages;
  23.     #[ORM\JoinColumn(name'user_id'referencedColumnName'id'nullabletrue)]
  24.     #[ORM\ManyToOne(targetEntityUser::class)]
  25.     protected ?User $owner null;
  26.     /**
  27.      * Defines whether a message is created by a user OR support manager.
  28.      */
  29.     #[ORM\Column(type'boolean')]
  30.     protected bool $wroteByUser;
  31.     #[ORM\Column(type'string'nullabletrue)]
  32.     protected ?string $email null;
  33.     #[ORM\Column(type'string'nullabletrue)]
  34.     protected ?string $topic null;
  35.     #[ORM\Column(type'text')]
  36.     protected ?string $message null;
  37.     #[ORM\Column(name'created_at'type'datetimetz_immutable'options: ['default' => 'CURRENT_TIMESTAMP'])]
  38.     protected \DateTimeImmutable $createdAt;
  39.     #[ORM\Column(name'last_message_at'type'datetimetz_immutable'options: ['default' => 'CURRENT_TIMESTAMP'])]
  40.     protected \DateTimeImmutable $lastMessageAt;
  41.     #[ORM\Column(type'boolean')]
  42.     protected bool $isRead false;
  43.     function __construct()
  44.     {
  45.         $this->messages = new ArrayCollection();
  46.         $this->createdAt = new \DateTimeImmutable('now');
  47.         $this->lastMessageAt $this->createdAt;
  48.     }
  49.     public function getLastMessageAt(): \DateTimeImmutable
  50.     {
  51.         return $this->lastMessageAt;
  52.     }
  53.     public function setLastMessageAt(\DateTimeImmutable $lastMessageAt): void
  54.     {
  55.         $this->lastMessageAt $lastMessageAt;
  56.     }
  57.     public function isRead(): bool
  58.     {
  59.         return $this->isRead;
  60.     }
  61.     public function setIsRead(bool $isRead): void
  62.     {
  63.         $this->isRead $isRead;
  64.     }
  65.     public function getOwner(): ?User
  66.     {
  67.         return $this->owner;
  68.     }
  69.     public function setOwner(?User $owner): void
  70.     {
  71.         $this->owner $owner;
  72.     }
  73.     public function getCreatedAt(): \DateTimeImmutable
  74.     {
  75.         return $this->createdAt;
  76.     }
  77.     public function setCreatedAt(\DateTimeImmutable $createdAt): void
  78.     {
  79.         $this->createdAt $createdAt;
  80.     }
  81.     public function getId(): int
  82.     {
  83.         return $this->id;
  84.     }
  85.     public function isOwnedBy(User $account): bool
  86.     {
  87.         return $account->getId() === $this->owner->getId();
  88.     }
  89.     public function getParent(): ?SupportMessage
  90.     {
  91.         return $this->parent;
  92.     }
  93.     public function setParent(?SupportMessage $parent): void
  94.     {
  95.         $this->parent $parent;
  96.     }
  97.     public function getEmail(): ?string
  98.     {
  99.         return $this->email;
  100.     }
  101.     public function setEmail(?string $email): void
  102.     {
  103.         $this->email $email;
  104.     }
  105.     public function getTopic(): ?string
  106.     {
  107.         return $this->topic;
  108.     }
  109.     public function setTopic(?string $topic): void
  110.     {
  111.         $this->topic $topic;
  112.     }
  113.     public function getMessage(): ?string
  114.     {
  115.         return $this->message;
  116.     }
  117.     public function setMessage(string $message): void
  118.     {
  119.         $this->message $message;
  120.     }
  121.     public function setTopicIsRead(bool $byUser): void
  122.     {
  123.         if(false == $byUser && true == $this->wroteByUser) {
  124.             $this->setIsRead(true);
  125.         } else if(true == $byUser && false == $this->wroteByUser) {
  126.             $this->setIsRead(true);
  127.         }
  128.         $this->getMessages()->map(function (SupportMessage $message) use ($byUser): SupportMessage {
  129.             if ($byUser != $message->isWroteByUser())
  130.                 $message->setIsRead(true);
  131.             return $message;
  132.         });
  133.     }
  134.     /**
  135.      * @return SupportMessage[]
  136.      */
  137.     public function getMessages(): Collection
  138.     {
  139.         return $this->messages;
  140.     }
  141.     public function setMessages(array $messages): void
  142.     {
  143.         $this->messages = new ArrayCollection($messages);
  144.     }
  145.     public function isWroteByUser(): bool
  146.     {
  147.         return $this->wroteByUser;
  148.     }
  149.     public function setWroteByUser(bool $wroteByUser): void
  150.     {
  151.         $this->wroteByUser $wroteByUser;
  152.     }
  153. }