<?php
namespace App\Entity\Email;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\MiniAbstractBase;
use App\Entity\Traits\SiteTrait;
use App\Entity\Email\EmailMessageStatus;
use App\Enum\EmailMessageStatusEnum;
use App\Repository\Email\EmailMessageRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @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"})})
* @ORM\Entity(repositoryClass=EmailMessageRepository::class)
*
* @see https://developers.brevo.com/docs/getting-started
* @see https://developers.brevo.com/docs/transactional-webhooks
* @see https://developers.brevo.com/docs/send-a-transactional-email
* @see https://developers.brevo.com/docs/send-a-transactional-email#tracking-your-transactional-activity-through-webhooks
* @see https://developers.brevo.com/reference/sendtransacemail
*/
class EmailMessage extends MiniAbstractBase implements SiteInterface
{
use SiteTrait;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $messageId = null; // message-id at the third party mail provider (Brevo e.g.)
/**
* @ORM\Column(type="string", length=511, nullable=false)
*/
private string $subject;
/**
* @ORM\Column(type="string", length=320, nullable=false)
*/
private string $messageTo; // TO email
/**
* @ORM\Column(type="string", length=320, nullable=false)
*/
private string $messageFrom; // FROM email
/**
* @ORM\Column(type="array")
*/
private array $tags = [];
/**
* @ORM\OneToMany(targetEntity="EmailMessageStatus", mappedBy="emailMessage", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"date": "ASC"})
*/
private ?Collection $statuses = null;
public function __construct()
{
$this->statuses = new ArrayCollection();
}
public function getMessageId(): ?string
{
return $this->messageId;
}
public function setMessageId(?string $messageId): self
{
$this->messageId = $messageId;
return $this;
}
public function getSubject(): string
{
return $this->subject;
}
public function setSubject(string $subject): self
{
$this->subject = $subject;
return $this;
}
public function getMessageTo(): string
{
return $this->messageTo;
}
public function setMessageTo(string $messageTo): self
{
$this->messageTo = $messageTo;
return $this;
}
public function getMessageFrom(): string
{
return $this->messageFrom;
}
public function setMessageFrom(string $messageFrom): self
{
$this->messageFrom = $messageFrom;
return $this;
}
public function getTags(): array
{
return $this->tags;
}
public function setTags(array $tags): self
{
$this->tags = $tags;
return $this;
}
public function hasTag(string $tag): bool
{
return in_array(strtoupper($tag), $this->tags, true);
}
public function addTag(string $tag): self
{
if (!$this->hasTag($tag)) {
$this->tags[] = strtoupper($tag);
}
return $this;
}
public function removeTag(string $tag): self
{
if (false !== $key = array_search(strtoupper($tag), $this->tags, true)) {
unset($this->tags[$key]);
$this->tags = array_values($this->tags);
}
return $this;
}
public function getTagsAsString(): string
{
if (!empty($this->tags)) {
return implode(', ', $this->tags);
}
return '';
}
public function getStatuses(): Collection
{
return $this->statuses;
}
public function addStatus(EmailMessageStatus $status): self
{
if (!$this->getStatuses()->contains($status)) {
$status->setEmailMessage($this);
$this->statuses->add($status);
}
return $this;
}
public function removeStatus(EmailMessageStatus $status): self
{
if ($this->getStatuses()->contains($status)) {
$this->statuses->removeElement($status);
}
return $this;
}
/**
* Object $status collection could not be ordered by the logical sequence of email message status
*
* @param Collection|null $statuses
* @param int $index
* @return EmailMessageStatus|null
*/
public function getMostRelevantStatus(?Collection $statuses = null, int $index = 0): ?EmailMessageStatus
{
if (is_null($statuses)) {
$statuses = $this->getStatuses();
}
/** @var EmailMessageStatus $status */
foreach ($statuses as $status) {
if ($status->getStatus() === EmailMessageStatusEnum::getArrayOrderedByStatusReverseSequence()[$index]) {
return $status;
}
}
if ($index++ < count(EmailMessageStatusEnum::getArrayOrderedByStatusReverseSequence())) {
return $this->getMostRelevantStatus($statuses, $index);
} else {
return null;
}
}
}