<?php
namespace App\Entity;
use App\Enum\TelegramCampaignParticipant\SendStatus;
use App\Repository\TelegramCampaignParticipantRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=TelegramCampaignParticipantRepository::class)
*/
class TelegramCampaignParticipant
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Candidate::class)
*/
private $candidate;
/**
* @ORM\ManyToOne(targetEntity=TelegramCampaign::class, inversedBy="participants")
*/
private $telegramCampaign;
/**
* @ORM\Column(type="enum", options={"values": "telegram_campaign_participant_enum"})
*/
private $sendStatus = SendStatus::SEND_STATUS_NOT_SENT;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $responseData;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $sentAt;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $errorText;
/**
* @ORM\Column(type="smallint", nullable=true)
*/
private $checkDeliveryTrysCount;
/**
* @ORM\OneToMany(targetEntity=TelegramCampaignDeliveryState::class, mappedBy="telegramCampaignParticipant")
*/
private $deliveryStates;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
*/
private $person;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->deliveryStates = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getCandidate(): ?Candidate
{
return $this->candidate;
}
public function setCandidate(?Candidate $candidate): self
{
$this->candidate = $candidate;
return $this;
}
public function getTelegramCampaign(): ?TelegramCampaign
{
return $this->telegramCampaign;
}
public function setTelegramCampaign(?TelegramCampaign $telegramCampaign): self
{
$this->telegramCampaign = $telegramCampaign;
return $this;
}
public function getSendStatus()
{
return $this->sendStatus;
}
public function isSendStatusCanceled(): bool
{
return $this->getSendStatus() === SendStatus::SEND_STATUS_CANCELED;
}
public function isSendStatusNotSent(): bool
{
return $this->getSendStatus() === SendStatus::SEND_STATUS_NOT_SENT;
}
public function isSendStatusDeliveryCheck(): bool
{
return $this->getSendStatus() === SendStatus::SEND_STATUS_DELIVERY_CHECK;
}
public function canRemoveFromCampaign(): bool
{
return in_array($this->getSendStatus(), SendStatus::getCampaignRemovableStatuses());
}
public function canCancelFromCampaign(): bool
{
return in_array($this->getSendStatus(), SendStatus::getCampaignCancelableStatuses());
}
public function getSendStatusText(): string
{
return SendStatus::getText($this->sendStatus);
}
public function setSendStatus($sendStatus): self
{
$this->sendStatus = $sendStatus;
if ($sendStatus === SendStatus::SEND_STATUS_SENT
|| $sendStatus === SendStatus::SEND_STATUS_DELIVERY_CHECK) {
$this->setSentAt(new \DateTime());
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getSendStatusCssClass($prefix = null): string
{
return SendStatus::getCssClass($this->sendStatus, $prefix);
}
public function getResponseData(): ?array
{
return json_decode($this->responseData ?? "{}", true);
}
public function setResponseData(?array $responseData): self
{
$this->responseData = json_encode($responseData,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
return $this;
}
public function appendResponseDataItem(array $responseDataItem): self
{
$allData = $this->getResponseData() ?? [];
$allData[] = $responseDataItem;
$this->setResponseData($allData);
return $this;
}
public function setResponseDataItem(array $responseDataItem, int $key): self
{
$allData = $this->getResponseData() ?? [];
$allData[$key] = $responseDataItem;
$this->setResponseData($allData);
return $this;
}
public function getSentAt(): ?\DateTimeInterface
{
return $this->sentAt;
}
public function setSentAt(?\DateTimeInterface $sentAt): self
{
$this->sentAt = $sentAt;
return $this;
}
public function getErrorText(): ?string
{
if ($this->getSendStatus() != SendStatus::SEND_STATUS_ERROR) {
return null;
}
return $this->errorText;
}
private function setErrorText(?string $errorText): self
{
$this->errorText = $errorText;
return $this;
}
public function appendErrorText(string $errorText): self
{
$currentErrorText = $this->getErrorText();
if ($currentErrorText) {
$newErrorText = rtrim($currentErrorText, ".") . ".\n" . $errorText;
} else {
$newErrorText = $errorText;
}
return $this->setErrorText($newErrorText);
}
public function getCheckDeliveryTrysCount(): ?int
{
return $this->checkDeliveryTrysCount;
}
public function setCheckDeliveryTrysCount(?int $checkDeliveryTrysCount): self
{
$this->checkDeliveryTrysCount = $checkDeliveryTrysCount;
return $this;
}
public function increaseCheckDeliveryTrysCount(): self
{
$this->checkDeliveryTrysCount = ($this->checkDeliveryTrysCount ?? 0) + 1;
return $this;
}
public function isMaxCheckDeliveryTrysCount(int $maxTrys = 3): bool
{
return $this->checkDeliveryTrysCount !== null && $this->checkDeliveryTrysCount >= $maxTrys;
}
/**
* @return Collection<int, TelegramCampaignDeliveryState>
*/
public function getTelegramCampaignDeliveryStates(): Collection
{
return $this->telegramCampaignDeliveryStates;
}
public function addTelegramCampaignDeliveryState(TelegramCampaignDeliveryState $telegramCampaignDeliveryState): self
{
if (!$this->telegramCampaignDeliveryStates->contains($telegramCampaignDeliveryState)) {
$this->telegramCampaignDeliveryStates[] = $telegramCampaignDeliveryState;
$telegramCampaignDeliveryState->setParticipant($this);
}
return $this;
}
public function removeTelegramCampaignDeliveryState(TelegramCampaignDeliveryState $telegramCampaignDeliveryState): self
{
if ($this->telegramCampaignDeliveryStates->removeElement($telegramCampaignDeliveryState)) {
// set the owning side to null (unless already changed)
if ($telegramCampaignDeliveryState->getParticipant() === $this) {
$telegramCampaignDeliveryState->setParticipant(null);
}
}
return $this;
}
/**
* @return Collection<int, TelegramCampaignDeliveryState>
*/
public function getDeliveryStates(): Collection
{
return $this->deliveryStates;
}
public function addDeliveryState(TelegramCampaignDeliveryState $deliveryState): self
{
if (!$this->deliveryStates->contains($deliveryState)) {
$this->deliveryStates[] = $deliveryState;
$deliveryState->setTelegramCampaignParticipant($this);
}
return $this;
}
public function removeDeliveryState(TelegramCampaignDeliveryState $deliveryState): self
{
if ($this->deliveryStates->removeElement($deliveryState)) {
// set the owning side to null (unless already changed)
if ($deliveryState->getTelegramCampaignParticipant() === $this) {
$deliveryState->setTelegramCampaignParticipant(null);
}
}
return $this;
}
public function hasDeliveryState(TelegramCampaignMessage $message): bool
{
foreach ($this->getDeliveryStates() as $deliveryState) {
if ($deliveryState->getMessage()->getId() === $message->getId()) {
return true;
}
}
return false;
}
public function getDeliveryState(TelegramCampaignMessage $message): ?TelegramCampaignDeliveryState
{
foreach ($this->getDeliveryStates() as $deliveryState) {
if ($deliveryState->getMessage()->getId() === $message->getId()) {
return $deliveryState;
}
}
return null;
}
public function getPerson(): ?Person
{
if ($this->getCandidate() !== null) {
return $this->getCandidate()->getPerson();
}
return $this->person;
}
public function setPerson(?Person $person): self
{
$this->person = $person;
return $this;
}
public function isSendStatusPartialSent(): bool
{
return $this->getSendStatus() === SendStatus::SEND_STATUS_PARTIAL_SENT;
}
public function isSendStatusSent(): bool
{
return $this->getSendStatus() === SendStatus::SEND_STATUS_SENT;
}
public function isSendStatusError(): bool
{
return $this->getSendStatus() === SendStatus::SEND_STATUS_ERROR;
}
}