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, options={"default": "707660405753664"})
  21.      */
  22.     private string $phoneNumberId// the WHATSAPP_PHONE_NUMBER_ID that sends the message
  23.     /**
  24.      * @ORM\Column(type="string", length=255, nullable=false)
  25.      */
  26.     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.
  27.     /**
  28.      * @ORM\Column(type="string", length=255, nullable=false)
  29.      */
  30.     private string $phone// Recipient phone number. Is returned by the API as "input" in the Response object after sending a message to the customer.
  31.     /**
  32.      * @ORM\Column(type="string", length=255, nullable=false)
  33.      */
  34.     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.
  35.     /**
  36.      * @ORM\OneToMany(targetEntity="WhatsappMessageStatus", mappedBy="whatsappMessage", cascade={"persist", "remove"}, orphanRemoval=true)
  37.      * @ORM\OrderBy({"date": "ASC"})
  38.      */
  39.     private ?Collection $statuses null;
  40.     public function __construct()
  41.     {
  42.         $this->statuses = new ArrayCollection();
  43.     }
  44.     public function getPhoneNumberId(): string
  45.     {
  46.         return $this->phoneNumberId;
  47.     }
  48.     public function setPhoneNumberId(string $phoneNumberId): self
  49.     {
  50.         $this->phoneNumberId $phoneNumberId;
  51.         return $this;
  52.     }
  53.     public function getWamId(): string
  54.     {
  55.         return $this->wamId;
  56.     }
  57.     public function setWamId(string $wamId): self
  58.     {
  59.         $this->wamId $wamId;
  60.         return $this;
  61.     }
  62.     public function getPhone(): string
  63.     {
  64.         return $this->phone;
  65.     }
  66.     public function getPhoneWithoutPlusSign(): string
  67.     {
  68.         if (str_starts_with($this->phone'+')) {
  69.             return ltrim($this->phone'+');
  70.         }
  71.         return $this->phone;
  72.     }
  73.     /**
  74.      * Set the recipient phone number. Is returned by the API as "input" in the Response object after sending a message to the customer.
  75.      *
  76.      * @param string $phone
  77.      * @return $this
  78.      */
  79.     public function setPhone(string $phone): self
  80.     {
  81.         $this->phone $phone;
  82.         return $this;
  83.     }
  84.     public function getWaId(): string
  85.     {
  86.         return $this->waId;
  87.     }
  88.     public function setWaId(string $waId): self
  89.     {
  90.         $this->waId $waId;
  91.         return $this;
  92.     }
  93.     public function getStatuses(): Collection
  94.     {
  95.         return $this->statuses;
  96.     }
  97.     public function addStatus(WhatsappMessageStatus $status): self
  98.     {
  99.         if (!$this->getStatuses()->contains($status)) {
  100.             $status->setWhatsappMessage($this);
  101.             $this->statuses->add($status);
  102.         }
  103.         return $this;
  104.     }
  105.     public function removeStatus(WhatsappMessageStatus $status): self
  106.     {
  107.         if ($this->getStatuses()->contains($status)) {
  108.             $this->statuses->removeElement($status);
  109.         }
  110.         return $this;
  111.     }
  112.     public function isAccepted(): bool
  113.     {
  114.         /** @var WhatsappMessageStatus $status */
  115.         foreach ($this->getStatuses() as $status) {
  116.             if ($status->isAccepted()) {
  117.                 return true;
  118.             }
  119.         }
  120.         return false;
  121.     }
  122.     public function isAcceptedString(): string
  123.     {
  124.         if ($this->isAccepted()) {
  125.             return WhatsappMessageStatusEnum::ACCEPTED;
  126.         }
  127.         return '';
  128.     }
  129.     public function isFailed(): bool
  130.     {
  131.         /** @var WhatsappMessageStatus $status */
  132.         foreach ($this->getStatuses() as $status) {
  133.             if ($status->isFailed()) {
  134.                 return true;
  135.             }
  136.         }
  137.         return false;
  138.     }
  139.     public function isFailedString(): string
  140.     {
  141.         if ($this->isFailed()) {
  142.             return WhatsappMessageStatusEnum::FAILED;
  143.         }
  144.         return '';
  145.     }
  146.     public function isSent(): bool
  147.     {
  148.         /** @var WhatsappMessageStatus $status */
  149.         foreach ($this->getStatuses() as $status) {
  150.             if ($status->isSent()) {
  151.                 return true;
  152.             }
  153.         }
  154.         return false;
  155.     }
  156.     public function isSentString(): string
  157.     {
  158.         if ($this->isSent()) {
  159.             return WhatsappMessageStatusEnum::SENT;
  160.         }
  161.         return '';
  162.     }
  163.     public function isDelivered(): bool
  164.     {
  165.         /** @var WhatsappMessageStatus $status */
  166.         foreach ($this->getStatuses() as $status) {
  167.             if ($status->isDelivered()) {
  168.                 return true;
  169.             }
  170.         }
  171.         return false;
  172.     }
  173.     public function isDeliveredString(): string
  174.     {
  175.         if ($this->isDelivered()) {
  176.             return WhatsappMessageStatusEnum::DELIVERED;
  177.         }
  178.         return '';
  179.     }
  180.     public function isRead(): bool
  181.     {
  182.         /** @var WhatsappMessageStatus $status */
  183.         foreach ($this->getStatuses() as $status) {
  184.             if ($status->isRead()) {
  185.                 return true;
  186.             }
  187.         }
  188.         return false;
  189.     }
  190.     public function isReadString(): string
  191.     {
  192.         if ($this->isRead()) {
  193.             return WhatsappMessageStatusEnum::READ;
  194.         }
  195.         return '';
  196.     }
  197.     /**
  198.      * Object $status collection could not be ordered by the logical sequence of whatsapp message status (see Cloud API documentation)
  199.      * @param Collection|null $statusCollection
  200.      * @param int $index
  201.      * @return WhatsappMessageStatus
  202.      */
  203.     public function getMostRelevantStatus(?Collection $statuses nullint $index 0): ?WhatsappMessageStatus
  204.     {
  205.         if (is_null($statuses)) {
  206.             $statuses $this->getStatuses();
  207.         }
  208.         /** @var WhatsappMessageStatus $status */
  209.         foreach ($statuses as $status) {
  210.             if ($status->getStatus() === WhatsappMessageStatusEnum::getArrayOrderedByStatusReverseSequence()[$index]) {
  211.                 return $status;
  212.             }
  213.         }
  214.         if ($index++ < count(WhatsappMessageStatusEnum::getArrayOrderedByStatusReverseSequence())) {
  215.             return $this->getMostRelevantStatus($statuses$index);
  216.         } else {
  217.             return null;
  218.         }
  219.     }
  220. }