src/Entity/Email/EmailMessage.php line 27

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