src/Entity/Email/EmailMessage.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Email;
  3. use App\Entity\Interfaces\SiteInterface;
  4. use App\Entity\MiniAbstractBase;
  5. use App\Entity\Traits\SiteTrait;
  6. use App\Entity\Email\EmailMessageStatus;
  7. use App\Enum\EmailMessageStatusEnum;
  8. use App\Repository\Email\EmailMessageRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. /**
  13.  * @ORM\Table(name="vulco_email_message", indexes={@ORM\Index(name="email_message_site_idx", columns={"site"}), @ORM\Index(name="email_message_message_id_idx", columns={"message_id"})})
  14.  * @ORM\Entity(repositoryClass=EmailMessageRepository::class)
  15.  *
  16.  * @see https://developers.brevo.com/docs/getting-started
  17.  * @see https://developers.brevo.com/docs/transactional-webhooks
  18.  * @see https://developers.brevo.com/docs/send-a-transactional-email
  19.  * @see https://developers.brevo.com/docs/send-a-transactional-email#tracking-your-transactional-activity-through-webhooks
  20.  * @see https://developers.brevo.com/reference/sendtransacemail
  21.  */
  22. class EmailMessage extends MiniAbstractBase implements SiteInterface
  23. {
  24.     use SiteTrait;
  25.     /**
  26.      * @ORM\Column(type="string", length=255, nullable=true)
  27.      */
  28.     private ?string $messageId null// message-id at the third party mail provider (Brevo e.g.)
  29.     /**
  30.      * @ORM\Column(type="string", length=511, nullable=false)
  31.      */
  32.     private string $subject;
  33.     /**
  34.      * @ORM\Column(type="string", length=320, nullable=false)
  35.      */
  36.     private string $messageTo// TO email
  37.     /**
  38.      * @ORM\Column(type="string", length=320, nullable=false)
  39.      */
  40.     private string $messageFrom// FROM email
  41.     /**
  42.      * @ORM\Column(type="array")
  43.      */
  44.     private array $tags = [];
  45.     /**
  46.      * @ORM\OneToMany(targetEntity="EmailMessageStatus", mappedBy="emailMessage", cascade={"persist", "remove"}, orphanRemoval=true)
  47.      * @ORM\OrderBy({"date": "ASC"})
  48.      */
  49.     private ?Collection $statuses null;
  50.     public function __construct()
  51.     {
  52.         $this->statuses = new ArrayCollection();
  53.     }
  54.     public function getMessageId(): ?string
  55.     {
  56.         return $this->messageId;
  57.     }
  58.     public function setMessageId(?string $messageId): self
  59.     {
  60.         $this->messageId $messageId;
  61.         return $this;
  62.     }
  63.     public function getSubject(): string
  64.     {
  65.         return $this->subject;
  66.     }
  67.     public function setSubject(string $subject): self
  68.     {
  69.         $this->subject $subject;
  70.         return $this;
  71.     }
  72.     public function getMessageTo(): string
  73.     {
  74.         return $this->messageTo;
  75.     }
  76.     public function setMessageTo(string $messageTo): self
  77.     {
  78.         $this->messageTo $messageTo;
  79.         return $this;
  80.     }
  81.     public function getMessageFrom(): string
  82.     {
  83.         return $this->messageFrom;
  84.     }
  85.     public function setMessageFrom(string $messageFrom): self
  86.     {
  87.         $this->messageFrom $messageFrom;
  88.         return $this;
  89.     }
  90.     public function getTags(): array
  91.     {
  92.         return $this->tags;
  93.     }
  94.     public function setTags(array $tags): self
  95.     {
  96.         $this->tags $tags;
  97.         return $this;
  98.     }
  99.     public function hasTag(string $tag): bool
  100.     {
  101.         return in_array(strtoupper($tag), $this->tagstrue);
  102.     }
  103.     public function addTag(string $tag): self
  104.     {
  105.         if (!$this->hasTag($tag)) {
  106.             $this->tags[] = strtoupper($tag);
  107.         }
  108.         return $this;
  109.     }
  110.     public function removeTag(string $tag): self
  111.     {
  112.         if (false !== $key array_search(strtoupper($tag), $this->tagstrue)) {
  113.             unset($this->tags[$key]);
  114.             $this->tags array_values($this->tags);
  115.         }
  116.         return $this;
  117.     }
  118.     public function getTagsAsString(): string
  119.     {
  120.         if (!empty($this->tags)) {
  121.             return implode(', '$this->tags);
  122.         }
  123.         return '';
  124.     }
  125.     public function getStatuses(): Collection
  126.     {
  127.         return $this->statuses;
  128.     }
  129.     public function addStatus(EmailMessageStatus $status): self
  130.     {
  131.         if (!$this->getStatuses()->contains($status)) {
  132.             $status->setEmailMessage($this);
  133.             $this->statuses->add($status);
  134.         }
  135.         return $this;
  136.     }
  137.     public function removeStatus(EmailMessageStatus $status): self
  138.     {
  139.         if ($this->getStatuses()->contains($status)) {
  140.             $this->statuses->removeElement($status);
  141.         }
  142.         return $this;
  143.     }
  144.     /**
  145.      * Object $status collection could not be ordered by the logical sequence of email message status
  146.      *
  147.      * @param Collection|null $statuses
  148.      * @param int $index
  149.      * @return EmailMessageStatus|null
  150.      */
  151.     public function getMostRelevantStatus(?Collection $statuses nullint $index 0): ?EmailMessageStatus
  152.     {
  153.         if (is_null($statuses)) {
  154.             $statuses $this->getStatuses();
  155.         }
  156.         /** @var EmailMessageStatus $status */
  157.         foreach ($statuses as $status) {
  158.             if ($status->getStatus() === EmailMessageStatusEnum::getArrayOrderedByStatusReverseSequence()[$index]) {
  159.                 return $status;
  160.             }
  161.         }
  162.         if ($index++ < count(EmailMessageStatusEnum::getArrayOrderedByStatusReverseSequence())) {
  163.             return $this->getMostRelevantStatus($statuses$index);
  164.         } else {
  165.             return null;
  166.         }
  167.     }
  168. }