src/Entity/Whatsapp/WhatsappMessage.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Whatsapp;
  3. use App\Entity\AbstractBase;
  4. use App\Enum\WhatsappMessageStatusEnum;
  5. use App\Repository\Whatsapp\WhatsappMessageRepository;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. /**
  10.  * @ORM\Table(name="vulco_whatsapp_message", indexes={@ORM\Index(name="whatsapp_message_wamid_idx", columns={"wam_id"})})
  11.  * @ORM\Entity(repositoryClass=WhatsappMessageRepository::class)
  12.  *
  13.  * @see https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages#successful-response
  14.  * @see https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#message-status-updates
  15.  * @see https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/components#statuses-object
  16.  */
  17. class WhatsappMessage extends AbstractBase
  18. {
  19.     /**
  20.      * @ORM\Column(type="string", length=255, nullable=false)
  21.      */
  22.     private string $wamId// wam_id or wamid (WhatsApp message ID). Messages are identified by a unique ID (WAMID). You can track message in the Webhooks through its WAMID. Is returned by the API as "id" in the Response object after sending a message to the customer.
  23.     /**
  24.      * @ORM\Column(type="string", length=255, nullable=false)
  25.      */
  26.     private string $phone// Recipient phone number. Is returned by the API as "input" in the Response object after sending a message to the customer.
  27.     /**
  28.      * @ORM\Column(type="string", length=255, nullable=false)
  29.      */
  30.     private string $waId// wa_id: The customer's WhatsApp ID. A business can respond to a customer using this ID. This ID may not match the customer's phone number, which is returned by the API as "input" in the Response object after sending a message to the customer.
  31.     /**
  32.      * @ORM\OneToMany(targetEntity="WhatsappMessageStatus", mappedBy="whatsappMessage", cascade={"persist", "remove"}, orphanRemoval=true)
  33.      * @ORM\OrderBy({"date": "ASC"})
  34.      */
  35.     private ?Collection $statuses null;
  36.     public function __construct()
  37.     {
  38.         $this->statuses = new ArrayCollection();
  39.     }
  40.     public function getWamId(): string
  41.     {
  42.         return $this->wamId;
  43.     }
  44.     public function setWamId(string $wamId): self
  45.     {
  46.         $this->wamId $wamId;
  47.         return $this;
  48.     }
  49.     public function getPhone(): string
  50.     {
  51.         return $this->phone;
  52.     }
  53.     public function getPhoneWithoutPlusSign(): string
  54.     {
  55.         if (str_starts_with($this->phone'+')) {
  56.             return ltrim($this->phone'+');
  57.         }
  58.         return $this->phone;
  59.     }
  60.     /**
  61.      * Set the recipient phone number. Is returned by the API as "input" in the Response object after sending a message to the customer.
  62.      *
  63.      * @param string $phone
  64.      * @return $this
  65.      */
  66.     public function setPhone(string $phone): self
  67.     {
  68.         $this->phone $phone;
  69.         return $this;
  70.     }
  71.     public function getWaId(): string
  72.     {
  73.         return $this->waId;
  74.     }
  75.     public function setWaId(string $waId): self
  76.     {
  77.         $this->waId $waId;
  78.         return $this;
  79.     }
  80.     public function getStatuses(): Collection
  81.     {
  82.         return $this->statuses;
  83.     }
  84.     public function addStatus(WhatsappMessageStatus $status): self
  85.     {
  86.         if (!$this->getStatuses()->contains($status)) {
  87.             $status->setWhatsappMessage($this);
  88.             $this->statuses->add($status);
  89.         }
  90.         return $this;
  91.     }
  92.     public function removeStatus(WhatsappMessageStatus $status): self
  93.     {
  94.         if ($this->getStatuses()->contains($status)) {
  95.             $this->statuses->removeElement($status);
  96.         }
  97.         return $this;
  98.     }
  99.     public function isAccepted(): bool
  100.     {
  101.         /** @var WhatsappMessageStatus $status */
  102.         foreach ($this->getStatuses() as $status) {
  103.             if ($status->isAccepted()) {
  104.                 return true;
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.     public function isAcceptedString(): string
  110.     {
  111.         if ($this->isAccepted()) {
  112.             return WhatsappMessageStatusEnum::ACCEPTED;
  113.         }
  114.         return '';
  115.     }
  116.     public function isFailed(): bool
  117.     {
  118.         /** @var WhatsappMessageStatus $status */
  119.         foreach ($this->getStatuses() as $status) {
  120.             if ($status->isFailed()) {
  121.                 return true;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     public function isFailedString(): string
  127.     {
  128.         if ($this->isFailed()) {
  129.             return WhatsappMessageStatusEnum::FAILED;
  130.         }
  131.         return '';
  132.     }
  133.     public function isSent(): bool
  134.     {
  135.         /** @var WhatsappMessageStatus $status */
  136.         foreach ($this->getStatuses() as $status) {
  137.             if ($status->isSent()) {
  138.                 return true;
  139.             }
  140.         }
  141.         return false;
  142.     }
  143.     public function isSentString(): string
  144.     {
  145.         if ($this->isSent()) {
  146.             return WhatsappMessageStatusEnum::SENT;
  147.         }
  148.         return '';
  149.     }
  150.     public function isDelivered(): bool
  151.     {
  152.         /** @var WhatsappMessageStatus $status */
  153.         foreach ($this->getStatuses() as $status) {
  154.             if ($status->isDelivered()) {
  155.                 return true;
  156.             }
  157.         }
  158.         return false;
  159.     }
  160.     public function isDeliveredString(): string
  161.     {
  162.         if ($this->isDelivered()) {
  163.             return WhatsappMessageStatusEnum::DELIVERED;
  164.         }
  165.         return '';
  166.     }
  167.     public function isRead(): bool
  168.     {
  169.         /** @var WhatsappMessageStatus $status */
  170.         foreach ($this->getStatuses() as $status) {
  171.             if ($status->isRead()) {
  172.                 return true;
  173.             }
  174.         }
  175.         return false;
  176.     }
  177.     public function isReadString(): string
  178.     {
  179.         if ($this->isRead()) {
  180.             return WhatsappMessageStatusEnum::READ;
  181.         }
  182.         return '';
  183.     }
  184.     /**
  185.      * Object $status collection could not be ordered by the logical sequence of whatsapp message status (see Cloud API documentation)
  186.      * @param Collection|null $statusCollection
  187.      * @param int $index
  188.      * @return WhatsappMessageStatus
  189.      */
  190.     public function getMostRelevantStatus(?Collection $statuses nullint $index 0): ?WhatsappMessageStatus
  191.     {
  192.         if (is_null($statuses)) {
  193.             $statuses $this->getStatuses();
  194.         }
  195.         /** @var WhatsappMessageStatus $status */
  196.         foreach ($statuses as $status) {
  197.             if ($status->getStatus() === WhatsappMessageStatusEnum::getArrayOrderedByStatusReverseSequence()[$index]) {
  198.                 return $status;
  199.             }
  200.         }
  201.         if ($index++ < count(WhatsappMessageStatusEnum::getArrayOrderedByStatusReverseSequence())) {
  202.             return $this->getMostRelevantStatus($statuses$index);
  203.         } else {
  204.             return null;
  205.         }
  206.     }
  207. }