src/Entity/TelegramCampaignParticipant.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\TelegramCampaignParticipant\SendStatus;
  4. use App\Repository\TelegramCampaignParticipantRepository;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * @ORM\Entity(repositoryClass=TelegramCampaignParticipantRepository::class)
  10.  */
  11. class TelegramCampaignParticipant
  12. {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\ManyToOne(targetEntity=Candidate::class)
  21.      */
  22.     private $candidate;
  23.     /**
  24.      * @ORM\ManyToOne(targetEntity=TelegramCampaign::class, inversedBy="participants")
  25.      */
  26.     private $telegramCampaign;
  27.     /**
  28.      * @ORM\Column(type="enum", options={"values": "telegram_campaign_participant_enum"})
  29.      */
  30.     private $sendStatus SendStatus::SEND_STATUS_NOT_SENT;
  31.     /**
  32.      * @ORM\Column(type="datetime")
  33.      */
  34.     private $createdAt;
  35.     /**
  36.      * @ORM\Column(type="text", nullable=true)
  37.      */
  38.     private $responseData;
  39.     /**
  40.      * @ORM\Column(type="datetime", nullable=true)
  41.      */
  42.     private $sentAt;
  43.     /**
  44.      * @ORM\Column(type="text", nullable=true)
  45.      */
  46.     private $errorText;
  47.     /**
  48.      * @ORM\Column(type="smallint", nullable=true)
  49.      */
  50.     private $checkDeliveryTrysCount;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity=TelegramCampaignDeliveryState::class, mappedBy="telegramCampaignParticipant")
  53.      */
  54.     private $deliveryStates;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity=Person::class)
  57.      */
  58.     private $person;
  59.     public function __construct()
  60.     {
  61.         $this->createdAt = new \DateTime();
  62.         $this->deliveryStates = new ArrayCollection();
  63.     }
  64.     public function getId(): ?int
  65.     {
  66.         return $this->id;
  67.     }
  68.     public function getCandidate(): ?Candidate
  69.     {
  70.         return $this->candidate;
  71.     }
  72.     public function setCandidate(?Candidate $candidate): self
  73.     {
  74.         $this->candidate $candidate;
  75.         return $this;
  76.     }
  77.     public function getTelegramCampaign(): ?TelegramCampaign
  78.     {
  79.         return $this->telegramCampaign;
  80.     }
  81.     public function setTelegramCampaign(?TelegramCampaign $telegramCampaign): self
  82.     {
  83.         $this->telegramCampaign $telegramCampaign;
  84.         return $this;
  85.     }
  86.     public function getSendStatus()
  87.     {
  88.         return $this->sendStatus;
  89.     }
  90.     public function isSendStatusCanceled(): bool
  91.     {
  92.         return $this->getSendStatus() === SendStatus::SEND_STATUS_CANCELED;
  93.     }
  94.     public function isSendStatusNotSent(): bool
  95.     {
  96.         return $this->getSendStatus() === SendStatus::SEND_STATUS_NOT_SENT;
  97.     }
  98.     public function isSendStatusDeliveryCheck(): bool
  99.     {
  100.         return $this->getSendStatus() === SendStatus::SEND_STATUS_DELIVERY_CHECK;
  101.     }
  102.     public function canRemoveFromCampaign(): bool
  103.     {
  104.         return in_array($this->getSendStatus(), SendStatus::getCampaignRemovableStatuses());
  105.     }
  106.     public function canCancelFromCampaign(): bool
  107.     {
  108.         return in_array($this->getSendStatus(), SendStatus::getCampaignCancelableStatuses());
  109.     }
  110.     public function getSendStatusText(): string
  111.     {
  112.         return SendStatus::getText($this->sendStatus);
  113.     }
  114.     public function setSendStatus($sendStatus): self
  115.     {
  116.         $this->sendStatus $sendStatus;
  117.         if ($sendStatus === SendStatus::SEND_STATUS_SENT
  118.             || $sendStatus === SendStatus::SEND_STATUS_DELIVERY_CHECK) {
  119.             $this->setSentAt(new \DateTime());
  120.         }
  121.         return $this;
  122.     }
  123.     public function getCreatedAt(): ?\DateTimeInterface
  124.     {
  125.         return $this->createdAt;
  126.     }
  127.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  128.     {
  129.         $this->createdAt $createdAt;
  130.         return $this;
  131.     }
  132.     public function getSendStatusCssClass($prefix null): string
  133.     {
  134.         return SendStatus::getCssClass($this->sendStatus$prefix);
  135.     }
  136.     public function getResponseData(): ?array
  137.     {
  138.         return json_decode($this->responseData ?? "{}"true);
  139.     }
  140.     public function setResponseData(?array $responseData): self
  141.     {
  142.         $this->responseData json_encode($responseData,
  143.             JSON_UNESCAPED_SLASHES JSON_UNESCAPED_UNICODE);
  144.         return $this;
  145.     }
  146.     public function appendResponseDataItem(array $responseDataItem): self
  147.     {
  148.         $allData $this->getResponseData() ?? [];
  149.         $allData[] = $responseDataItem;
  150.         $this->setResponseData($allData);
  151.         return $this;
  152.     }
  153.     public function setResponseDataItem(array $responseDataItemint $key): self
  154.     {
  155.         $allData $this->getResponseData() ?? [];
  156.         $allData[$key] = $responseDataItem;
  157.         $this->setResponseData($allData);
  158.         return $this;
  159.     }
  160.     public function getSentAt(): ?\DateTimeInterface
  161.     {
  162.         return $this->sentAt;
  163.     }
  164.     public function setSentAt(?\DateTimeInterface $sentAt): self
  165.     {
  166.         $this->sentAt $sentAt;
  167.         return $this;
  168.     }
  169.     public function getErrorText(): ?string
  170.     {
  171.         if ($this->getSendStatus() != SendStatus::SEND_STATUS_ERROR) {
  172.             return null;
  173.         }
  174.         return $this->errorText;
  175.     }
  176.     private function setErrorText(?string $errorText): self
  177.     {
  178.         $this->errorText $errorText;
  179.         return $this;
  180.     }
  181.     public function appendErrorText(string $errorText): self
  182.     {
  183.         $currentErrorText $this->getErrorText();
  184.         if ($currentErrorText) {
  185.             $newErrorText rtrim($currentErrorText".") . ".\n" $errorText;
  186.         } else {
  187.             $newErrorText $errorText;
  188.         }
  189.         return $this->setErrorText($newErrorText);
  190.     }
  191.     public function getCheckDeliveryTrysCount(): ?int
  192.     {
  193.         return $this->checkDeliveryTrysCount;
  194.     }
  195.     public function setCheckDeliveryTrysCount(?int $checkDeliveryTrysCount): self
  196.     {
  197.         $this->checkDeliveryTrysCount $checkDeliveryTrysCount;
  198.         return $this;
  199.     }
  200.     public function increaseCheckDeliveryTrysCount(): self
  201.     {
  202.         $this->checkDeliveryTrysCount = ($this->checkDeliveryTrysCount ?? 0) + 1;
  203.         return $this;
  204.     }
  205.     public function isMaxCheckDeliveryTrysCount(int $maxTrys 3): bool
  206.     {
  207.         return $this->checkDeliveryTrysCount !== null && $this->checkDeliveryTrysCount >= $maxTrys;
  208.     }
  209.     /**
  210.      * @return Collection<int, TelegramCampaignDeliveryState>
  211.      */
  212.     public function getTelegramCampaignDeliveryStates(): Collection
  213.     {
  214.         return $this->telegramCampaignDeliveryStates;
  215.     }
  216.     public function addTelegramCampaignDeliveryState(TelegramCampaignDeliveryState $telegramCampaignDeliveryState): self
  217.     {
  218.         if (!$this->telegramCampaignDeliveryStates->contains($telegramCampaignDeliveryState)) {
  219.             $this->telegramCampaignDeliveryStates[] = $telegramCampaignDeliveryState;
  220.             $telegramCampaignDeliveryState->setParticipant($this);
  221.         }
  222.         return $this;
  223.     }
  224.     public function removeTelegramCampaignDeliveryState(TelegramCampaignDeliveryState $telegramCampaignDeliveryState): self
  225.     {
  226.         if ($this->telegramCampaignDeliveryStates->removeElement($telegramCampaignDeliveryState)) {
  227.             // set the owning side to null (unless already changed)
  228.             if ($telegramCampaignDeliveryState->getParticipant() === $this) {
  229.                 $telegramCampaignDeliveryState->setParticipant(null);
  230.             }
  231.         }
  232.         return $this;
  233.     }
  234.     /**
  235.      * @return Collection<int, TelegramCampaignDeliveryState>
  236.      */
  237.     public function getDeliveryStates(): Collection
  238.     {
  239.         return $this->deliveryStates;
  240.     }
  241.     public function addDeliveryState(TelegramCampaignDeliveryState $deliveryState): self
  242.     {
  243.         if (!$this->deliveryStates->contains($deliveryState)) {
  244.             $this->deliveryStates[] = $deliveryState;
  245.             $deliveryState->setTelegramCampaignParticipant($this);
  246.         }
  247.         return $this;
  248.     }
  249.     public function removeDeliveryState(TelegramCampaignDeliveryState $deliveryState): self
  250.     {
  251.         if ($this->deliveryStates->removeElement($deliveryState)) {
  252.             // set the owning side to null (unless already changed)
  253.             if ($deliveryState->getTelegramCampaignParticipant() === $this) {
  254.                 $deliveryState->setTelegramCampaignParticipant(null);
  255.             }
  256.         }
  257.         return $this;
  258.     }
  259.     public function hasDeliveryState(TelegramCampaignMessage $message): bool
  260.     {
  261.         foreach ($this->getDeliveryStates() as $deliveryState) {
  262.             if ($deliveryState->getMessage()->getId() === $message->getId()) {
  263.                 return true;
  264.             }
  265.         }
  266.         return false;
  267.     }
  268.     public function getDeliveryState(TelegramCampaignMessage $message): ?TelegramCampaignDeliveryState
  269.     {
  270.         foreach ($this->getDeliveryStates() as $deliveryState) {
  271.             if ($deliveryState->getMessage()->getId() === $message->getId()) {
  272.                 return $deliveryState;
  273.             }
  274.         }
  275.         return null;
  276.     }
  277.     public function getPerson(): ?Person
  278.     {
  279.         if ($this->getCandidate() !== null) {
  280.             return $this->getCandidate()->getPerson();
  281.         }
  282.         return $this->person;
  283.     }
  284.     public function setPerson(?Person $person): self
  285.     {
  286.         $this->person $person;
  287.         return $this;
  288.     }
  289.     public function isSendStatusPartialSent(): bool
  290.     {
  291.         return $this->getSendStatus() === SendStatus::SEND_STATUS_PARTIAL_SENT;
  292.     }
  293.     public function isSendStatusSent(): bool
  294.     {
  295.         return $this->getSendStatus() === SendStatus::SEND_STATUS_SENT;
  296.     }
  297.     public function isSendStatusError(): bool
  298.     {
  299.         return $this->getSendStatus() === SendStatus::SEND_STATUS_ERROR;
  300.     }
  301. }