src/Entity/Whatsapp/WhatsappMessageStatus.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Whatsapp;
  3. use App\Enum\WhatsappMessageStatusEnum;
  4. use App\Repository\Whatsapp\WhatsappMessageStatusRepository;
  5. use DateTime;
  6. use DateTimeImmutable;
  7. use DateTimeInterface;
  8. use Doctrine\ORM\Mapping as ORM;
  9. /**
  10.  * @ORM\Table(name="vulco_whatsapp_message_status")
  11.  * @ORM\Entity(repositoryClass=WhatsappMessageStatusRepository::class)
  12.  *
  13.  * @see https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#message-status-updates
  14.  * @see https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components#statuses-object
  15.  * @see https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes
  16.  */
  17. class WhatsappMessageStatus
  18. {
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue
  22.      * @ORM\Column(type="integer")
  23.      */
  24.     protected int $id;
  25.     /**
  26.      * @ORM\ManyToOne(targetEntity="WhatsappMessage", inversedBy="statuses")
  27.      */
  28.     private WhatsappMessage $whatsappMessage;
  29.     /**
  30.      * @ORM\Column(type="string", length=255, nullable=false, options={"default": "failed"})
  31.      */
  32.     private string $status WhatsappMessageStatusEnum::FAILED;
  33.     /**
  34.      * @ORM\Column(type="datetime")
  35.      */
  36.     private DateTimeInterface $date;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private ?string $errorCode null// @see https://developers.facebook.com/docs/whatsapp/cloud-api/support/error-codes
  41.     /**
  42.      * @ORM\Column(type="text", length=10000, nullable=true)
  43.      */
  44.     private ?string $errorMessage null;
  45.     public function __construct(
  46.         string $status WhatsappMessageStatusEnum::ACCEPTED,
  47.         DateTimeInterface $date = new DateTime())
  48.     {
  49.         $this->status $status;
  50.         $this->date $date;
  51.     }
  52.     public function getId(): int
  53.     {
  54.         return $this->id;
  55.     }
  56.     public function getWhatsappMessage(): WhatsappMessage
  57.     {
  58.         return $this->whatsappMessage;
  59.     }
  60.     public function setWhatsappMessage(WhatsappMessage $whatsappMessage): self
  61.     {
  62.         $this->whatsappMessage $whatsappMessage;
  63.         return $this;
  64.     }
  65.     public function getStatus(): string
  66.     {
  67.         return $this->status;
  68.     }
  69.     public function getStatusAsTranslationKey(): string
  70.     {
  71.         if (array_key_exists($this->statusWhatsappMessageStatusEnum::getTranslations())) {
  72.             return WhatsappMessageStatusEnum::getTranslations()[$this->status];
  73.         }
  74.         return $this->status;
  75.     }
  76.     public function setStatus(string $status): self
  77.     {
  78.         $this->status $status;
  79.         return $this;
  80.     }
  81.     public function getDate(): DateTimeInterface
  82.     {
  83.         return $this->date;
  84.     }
  85.     public function setDate(DateTimeInterface $date): self
  86.     {
  87.         $this->date $date;
  88.         return $this;
  89.     }
  90.     public function getErrorCode(): ?string
  91.     {
  92.         return $this->errorCode;
  93.     }
  94.     public function setErrorCode(?string $errorCode): self
  95.     {
  96.         $this->errorCode $errorCode;
  97.         return $this;
  98.     }
  99.     public function getErrorMessage(): ?string
  100.     {
  101.         return $this->errorMessage;
  102.     }
  103.     public function setErrorMessage(?string $errorMessage): self
  104.     {
  105.         $this->errorMessage $errorMessage;
  106.         return $this;
  107.     }
  108.     public function isUnknown(): bool
  109.     {
  110.         if ($this->isAccepted()
  111.             || $this->isFailed()
  112.             || $this->isSent()
  113.             || $this->isDelivered()
  114.             || $this->isRead()) {
  115.             return false;
  116.         }
  117.         return true;
  118.     }
  119.     public function isAccepted(): bool
  120.     {
  121.         if (WhatsappMessageStatusEnum::ACCEPTED === $this->getStatus()) {
  122.             return true;
  123.         }
  124.         return false;
  125.     }
  126.     public function isFailed(): bool
  127.     {
  128.         if (WhatsappMessageStatusEnum::FAILED === $this->getStatus()) {
  129.             return true;
  130.         }
  131.         return false;
  132.     }
  133.     public function isSent(): bool
  134.     {
  135.         if (WhatsappMessageStatusEnum::SENT === $this->getStatus()) {
  136.             return true;
  137.         }
  138.         return false;
  139.     }
  140.     public function isDelivered(): bool
  141.     {
  142.         if (WhatsappMessageStatusEnum::DELIVERED === $this->getStatus()) {
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.     public function isRead(): bool
  148.     {
  149.         if (WhatsappMessageStatusEnum::READ === $this->getStatus()) {
  150.             return true;
  151.         }
  152.         return false;
  153.     }
  154. }