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", orphanRemoval=true)
  53.      */
  54.     private $deliveryStates;
  55.     /**
  56.      * @ORM\ManyToOne(targetEntity=Person::class)
  57.      */
  58.     private $person;
  59.     /**
  60.      * @ORM\Column(type="text", nullable=true)
  61.      */
  62.     private $historyData;
  63.     public function __construct()
  64.     {
  65.         $this->createdAt = new \DateTime();
  66.         $this->deliveryStates = new ArrayCollection();
  67.     }
  68.     public function getId(): ?int
  69.     {
  70.         return $this->id;
  71.     }
  72.     public function getCandidate(): ?Candidate
  73.     {
  74.         return $this->candidate;
  75.     }
  76.     public function setCandidate(?Candidate $candidate): self
  77.     {
  78.         $this->candidate $candidate;
  79.         return $this;
  80.     }
  81.     public function getTelegramCampaign(): ?TelegramCampaign
  82.     {
  83.         return $this->telegramCampaign;
  84.     }
  85.     public function setTelegramCampaign(?TelegramCampaign $telegramCampaign): self
  86.     {
  87.         $this->telegramCampaign $telegramCampaign;
  88.         return $this;
  89.     }
  90.     public function getSendStatus()
  91.     {
  92.         return $this->sendStatus;
  93.     }
  94.     public function isSendStatusCanceled(): bool
  95.     {
  96.         return $this->getSendStatus() === SendStatus::SEND_STATUS_CANCELED;
  97.     }
  98.     public function isSendStatusNotSent(): bool
  99.     {
  100.         return $this->getSendStatus() === SendStatus::SEND_STATUS_NOT_SENT;
  101.     }
  102.     public function isSendStatusDeliveryCheck(): bool
  103.     {
  104.         return $this->getSendStatus() === SendStatus::SEND_STATUS_DELIVERY_CHECK;
  105.     }
  106.     public function canRemoveFromCampaign(): bool
  107.     {
  108.         return in_array($this->getSendStatus(), SendStatus::getCampaignRemovableStatuses());
  109.     }
  110.     public function canCancelFromCampaign(): bool
  111.     {
  112.         return in_array($this->getSendStatus(), SendStatus::getCampaignCancelableStatuses());
  113.     }
  114.     public function getSendStatusText(): string
  115.     {
  116.         return SendStatus::getText($this->sendStatus);
  117.     }
  118.     public function setSendStatus($sendStatus): self
  119.     {
  120.         $this->sendStatus $sendStatus;
  121.         if ($sendStatus === SendStatus::SEND_STATUS_SENT
  122.             || $sendStatus === SendStatus::SEND_STATUS_DELIVERY_CHECK) {
  123.             $this->setSentAt(new \DateTime());
  124.         }
  125.         return $this;
  126.     }
  127.     public function getCreatedAt(): ?\DateTimeInterface
  128.     {
  129.         return $this->createdAt;
  130.     }
  131.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  132.     {
  133.         $this->createdAt $createdAt;
  134.         return $this;
  135.     }
  136.     public function getSendStatusCssClass($prefix null): string
  137.     {
  138.         return SendStatus::getCssClass($this->sendStatus$prefix);
  139.     }
  140.     public function getResponseData(): ?array
  141.     {
  142.         return json_decode($this->responseData ?? "{}"true);
  143.     }
  144.     public function setResponseData(?array $responseData): self
  145.     {
  146.         $this->responseData json_encode($responseData,
  147.             JSON_UNESCAPED_SLASHES JSON_UNESCAPED_UNICODE);
  148.         return $this;
  149.     }
  150.     public function appendResponseDataItem(array $responseDataItem): self
  151.     {
  152.         $allData $this->getResponseData() ?? [];
  153.         $allData[] = $responseDataItem;
  154.         $this->setResponseData($allData);
  155.         return $this;
  156.     }
  157.     public function setResponseDataItem(array $responseDataItemint $key): self
  158.     {
  159.         $allData $this->getResponseData() ?? [];
  160.         $allData[$key] = $responseDataItem;
  161.         $this->setResponseData($allData);
  162.         return $this;
  163.     }
  164.     public function getSentAt(): ?\DateTimeInterface
  165.     {
  166.         return $this->sentAt;
  167.     }
  168.     public function setSentAt(?\DateTimeInterface $sentAt): self
  169.     {
  170.         $this->sentAt $sentAt;
  171.         return $this;
  172.     }
  173.     public function getErrorText(): ?string
  174.     {
  175.         if ($this->getSendStatus() != SendStatus::SEND_STATUS_ERROR) {
  176.             return null;
  177.         }
  178.         return $this->errorText;
  179.     }
  180.     private function setErrorText(?string $errorText): self
  181.     {
  182.         $this->errorText $errorText;
  183.         return $this;
  184.     }
  185.     public function appendErrorText(string $errorText): self
  186.     {
  187.         $currentErrorText $this->getErrorText();
  188.         if ($currentErrorText) {
  189.             $newErrorText rtrim($currentErrorText".") . ".\n" $errorText;
  190.         } else {
  191.             $newErrorText $errorText;
  192.         }
  193.         return $this->setErrorText($newErrorText);
  194.     }
  195.     public function getCheckDeliveryTrysCount(): ?int
  196.     {
  197.         return $this->checkDeliveryTrysCount;
  198.     }
  199.     public function setCheckDeliveryTrysCount(?int $checkDeliveryTrysCount): self
  200.     {
  201.         $this->checkDeliveryTrysCount $checkDeliveryTrysCount;
  202.         return $this;
  203.     }
  204.     public function increaseCheckDeliveryTrysCount(): self
  205.     {
  206.         $this->checkDeliveryTrysCount = ($this->checkDeliveryTrysCount ?? 0) + 1;
  207.         return $this;
  208.     }
  209.     public function isMaxCheckDeliveryTrysCount(int $maxTrys 3): bool
  210.     {
  211.         return $this->checkDeliveryTrysCount !== null && $this->checkDeliveryTrysCount >= $maxTrys;
  212.     }
  213.     /**
  214.      * @return Collection<int, TelegramCampaignDeliveryState>
  215.      */
  216.     public function getTelegramCampaignDeliveryStates(): Collection
  217.     {
  218.         return $this->telegramCampaignDeliveryStates;
  219.     }
  220.     public function addTelegramCampaignDeliveryState(TelegramCampaignDeliveryState $telegramCampaignDeliveryState): self
  221.     {
  222.         if (!$this->telegramCampaignDeliveryStates->contains($telegramCampaignDeliveryState)) {
  223.             $this->telegramCampaignDeliveryStates[] = $telegramCampaignDeliveryState;
  224.             $telegramCampaignDeliveryState->setParticipant($this);
  225.         }
  226.         return $this;
  227.     }
  228.     public function removeTelegramCampaignDeliveryState(TelegramCampaignDeliveryState $telegramCampaignDeliveryState): self
  229.     {
  230.         if ($this->telegramCampaignDeliveryStates->removeElement($telegramCampaignDeliveryState)) {
  231.             // set the owning side to null (unless already changed)
  232.             if ($telegramCampaignDeliveryState->getParticipant() === $this) {
  233.                 $telegramCampaignDeliveryState->setParticipant(null);
  234.             }
  235.         }
  236.         return $this;
  237.     }
  238.     /**
  239.      * @return Collection<int, TelegramCampaignDeliveryState>
  240.      */
  241.     public function getDeliveryStates(): Collection
  242.     {
  243.         return $this->deliveryStates;
  244.     }
  245.     public function addDeliveryState(TelegramCampaignDeliveryState $deliveryState): self
  246.     {
  247.         if (!$this->deliveryStates->contains($deliveryState)) {
  248.             $this->deliveryStates[] = $deliveryState;
  249.             $deliveryState->setTelegramCampaignParticipant($this);
  250.         }
  251.         return $this;
  252.     }
  253.     public function removeDeliveryState(TelegramCampaignDeliveryState $deliveryState): self
  254.     {
  255.         if ($this->deliveryStates->removeElement($deliveryState)) {
  256.             // set the owning side to null (unless already changed)
  257.             if ($deliveryState->getTelegramCampaignParticipant() === $this) {
  258.                 $deliveryState->setTelegramCampaignParticipant(null);
  259.             }
  260.         }
  261.         return $this;
  262.     }
  263.     public function hasDeliveryState(TelegramCampaignMessage $message): bool
  264.     {
  265.         foreach ($this->getDeliveryStates() as $deliveryState) {
  266.             if ($deliveryState->getMessage()->getId() === $message->getId()) {
  267.                 return true;
  268.             }
  269.         }
  270.         return false;
  271.     }
  272.     public function getDeliveryState(TelegramCampaignMessage $message): ?TelegramCampaignDeliveryState
  273.     {
  274.         foreach ($this->getDeliveryStates() as $deliveryState) {
  275.             if ($deliveryState->getMessage()->getId() === $message->getId()) {
  276.                 return $deliveryState;
  277.             }
  278.         }
  279.         return null;
  280.     }
  281.     public function getPerson(): ?Person
  282.     {
  283.         if ($this->getCandidate() !== null) {
  284.             return $this->getCandidate()->getPerson();
  285.         }
  286.         return $this->person;
  287.     }
  288.     public function setPerson(?Person $person): self
  289.     {
  290.         $this->person $person;
  291.         return $this;
  292.     }
  293.     public function isSendStatusPartialSent(): bool
  294.     {
  295.         return $this->getSendStatus() === SendStatus::SEND_STATUS_PARTIAL_SENT;
  296.     }
  297.     public function isSendStatusSent(): bool
  298.     {
  299.         return $this->getSendStatus() === SendStatus::SEND_STATUS_SENT;
  300.     }
  301.     public function isSendStatusError(): bool
  302.     {
  303.         return $this->getSendStatus() === SendStatus::SEND_STATUS_ERROR;
  304.     }
  305.     public function getHistoryData(): ?array
  306.     {
  307.         return json_decode($this->historyData ?? "{}"true);
  308.     }
  309.     public function setHistoryData(?array $historyData): self
  310.     {
  311.         $this->historyData json_encode($historyData,
  312.             JSON_UNESCAPED_SLASHES JSON_UNESCAPED_UNICODE);
  313.         return $this;
  314.     }
  315.     public function appendHistoryDataItem(array $historyDataItem): self
  316.     {
  317.         $allData $this->getHistoryData() ?? [];
  318.         $allData[] = $historyDataItem;
  319.         $this->setHistoryData($allData);
  320.         return $this;
  321.     }
  322.     public function setHistoryDataItem(array $historyDataItemint $key): self
  323.     {
  324.         $allData $this->getHistoryData() ?? [];
  325.         $allData[$key] = $historyDataItem;
  326.         $this->setHistoryData($allData);
  327.         return $this;
  328.     }
  329. }