<?php
namespace App\Entity\Whatsapp;
use App\Enum\WhatsappMessageStatusEnum;
use App\Repository\Whatsapp\WhatsappMessageStatusRepository;
use DateTime;
use DateTimeImmutable;
use DateTimeInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="vulco_whatsapp_message_status")
* @ORM\Entity(repositoryClass=WhatsappMessageStatusRepository::class)
*
* @see https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#message-status-updates
* @see https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components#statuses-object
* @see https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes
*/
class WhatsappMessageStatus
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
protected int $id;
/**
* @ORM\ManyToOne(targetEntity="WhatsappMessage", inversedBy="statuses")
*/
private WhatsappMessage $whatsappMessage;
/**
* @ORM\Column(type="string", length=255, nullable=false, options={"default": "failed"})
*/
private string $status = WhatsappMessageStatusEnum::FAILED;
/**
* @ORM\Column(type="datetime")
*/
private DateTimeInterface $date;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $errorCode = null; // @see https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $errorMessage = null;
public function __construct(
string $status = WhatsappMessageStatusEnum::ACCEPTED,
DateTimeInterface $date = new DateTime())
{
$this->status = $status;
$this->date = $date;
}
public function getId(): int
{
return $this->id;
}
public function getWhatsappMessage(): WhatsappMessage
{
return $this->whatsappMessage;
}
public function setWhatsappMessage(WhatsappMessage $whatsappMessage): self
{
$this->whatsappMessage = $whatsappMessage;
return $this;
}
public function getStatus(): string
{
return $this->status;
}
public function getStatusAsTranslationKey(): string
{
if (array_key_exists($this->status, WhatsappMessageStatusEnum::getTranslations())) {
return WhatsappMessageStatusEnum::getTranslations()[$this->status];
}
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function getDate(): DateTimeInterface
{
return $this->date;
}
public function setDate(DateTimeInterface $date): self
{
$this->date = $date;
return $this;
}
public function getErrorCode(): ?string
{
return $this->errorCode;
}
public function setErrorCode(?string $errorCode): self
{
$this->errorCode = $errorCode;
return $this;
}
public function getErrorMessage(): ?string
{
return $this->errorMessage;
}
public function setErrorMessage(?string $errorMessage): self
{
$this->errorMessage = $errorMessage;
return $this;
}
public function isUnknown(): bool
{
if ($this->isAccepted()
|| $this->isFailed()
|| $this->isSent()
|| $this->isDelivered()
|| $this->isRead()) {
return false;
}
return true;
}
public function isAccepted(): bool
{
if (WhatsappMessageStatusEnum::ACCEPTED === $this->getStatus()) {
return true;
}
return false;
}
public function isFailed(): bool
{
if (WhatsappMessageStatusEnum::FAILED === $this->getStatus()) {
return true;
}
return false;
}
public function isSent(): bool
{
if (WhatsappMessageStatusEnum::SENT === $this->getStatus()) {
return true;
}
return false;
}
public function isDelivered(): bool
{
if (WhatsappMessageStatusEnum::DELIVERED === $this->getStatus()) {
return true;
}
return false;
}
public function isRead(): bool
{
if (WhatsappMessageStatusEnum::READ === $this->getStatus()) {
return true;
}
return false;
}
}