<?php
namespace App\Entity;
use App\Entity\ConversationMessage\ConversationMessage;
use App\Entity\ExternalPartner\IntegratedExternalPartnerCustomer;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use InvalidArgumentException;
use Symfony\Component\Validator\Constraints as Assert;
use ValueError;
/**
* @ORM\Entity
*
* @ORM\Table(
* name="automated_conversation_messages_mailings",
* indexes={
*
* @ORM\Index(name="mailing_type_mailed_at_idx", columns={"mailing_type", "mailed_at"}),
* @ORM\Index(name="recurrent_jobs_id_mailing_type_mailed_at_idx", columns={"recurrent_jobs_id", "mailing_type", "mailed_at"})
* }
* )
*/
class AutomatedConversationMessagesMailing
{
public const MAILING_TYPE_JOBRADAR = 0;
// LEGACY
public const MAILING_TYPE_TANKUNDRAST = 1;
public const MAILING_TYPE_WISAG = 2;
public const MAILING_TYPE_WISAG_PREMIUM = 3;
public const MAILING_TYPE_RHENUS = 4;
public const MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR = 5;
public const MAILING_TYPE_LEAD_CONVERSION_RADAR = 6;
public const MAILING_TYPES = [
self::MAILING_TYPE_JOBRADAR,
self::MAILING_TYPE_TANKUNDRAST,
self::MAILING_TYPE_WISAG,
self::MAILING_TYPE_WISAG_PREMIUM,
self::MAILING_TYPE_RHENUS,
self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR,
self::MAILING_TYPE_LEAD_CONVERSION_RADAR
];
public const TRIGGERING_SIDE_UNDEFINED = 0;
public const TRIGGERING_SIDE_RECURRENT_JOB = 1;
public const TRIGGERING_SIDE_WANTED_JOB = 2;
/** @throws InvalidArgumentException */
public function __construct(
?RecurrentJob $recurrentJob,
?WantedJob $wantedJob,
int $mailingType,
DateTime $mailedAt,
?IntegratedExternalPartnerCustomer $integratedExternalPartnerCustomer = null
) {
if ($mailingType === self::MAILING_TYPE_JOBRADAR) {
throw new ValueError('Jobradar is now based on JobradarMatches, therefore new AutomatedConversationMessagesMailings of this type must no longer be created.');
}
if ($mailingType === self::MAILING_TYPE_LEAD_CONVERSION_RADAR
&& (is_null($wantedJob) || is_null($recurrentJob))
) {
throw new InvalidArgumentException('Mailing type lead conversion radar (6) must be called with both $wantedJob and $recurrentJob being not null.');
}
if (!is_null($wantedJob)
&& !is_null($recurrentJob)
&& $mailingType !== self::MAILING_TYPE_LEAD_CONVERSION_RADAR
) {
throw new InvalidArgumentException('Must be called with $wantedJob or $recurrentJob, but not both if mailing type is not lead conversion radar (6).');
}
if (!is_null($recurrentJob)
&& !($mailingType === self::MAILING_TYPE_TANKUNDRAST
|| $mailingType === self::MAILING_TYPE_WISAG
|| $mailingType === self::MAILING_TYPE_WISAG_PREMIUM
|| $mailingType === self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR
|| $mailingType === self::MAILING_TYPE_LEAD_CONVERSION_RADAR
)
) {
throw new InvalidArgumentException('A recurrent job can only be set if mailing type is jobradar (0) or tankundrast (1) or wisag (2) or wisag premium (3) oder premium radar (5) or lead conversion radar (6).');
}
if (!is_null($wantedJob)
&& $mailingType !== self::MAILING_TYPE_LEAD_CONVERSION_RADAR
) {
throw new InvalidArgumentException('A wanted job can only be set if mailing type is jobradar (0) or lead conversion radar (6).');
}
if (($mailingType === self::MAILING_TYPE_TANKUNDRAST
|| $mailingType === self::MAILING_TYPE_WISAG
|| $mailingType === self::MAILING_TYPE_WISAG_PREMIUM
|| $mailingType === self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR
)
&& is_null($recurrentJob)
) {
throw new InvalidArgumentException('For mailing types tankundrast, wisag, wisag_premium, integrated_external_partner_customer_premium_radar, the recurrentJob must not be null, but is null.');
}
if (!in_array($mailingType, self::MAILING_TYPES)) {
throw new InvalidArgumentException('Mailing type ' . $mailingType . ' is invalid.');
}
$this->recurrentJob = $recurrentJob;
$this->wantedJob = $wantedJob;
$this->mailingType = $mailingType;
$this->mailedAt = $mailedAt;
$this->conversationMessages = new ArrayCollection();
$this->recurrentJobs = new ArrayCollection();
$this->wantedJobs = new ArrayCollection();
$this->numberOfCreatedMessages = 0;
$this->integratedExternalPartnerCustomer = $integratedExternalPartnerCustomer;
}
/**
* @ORM\GeneratedValue(strategy="CUSTOM")
*
* @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
*
* @ORM\Column(name="id", type="guid")
*
* @ORM\Id
*/
private ?string $id = null;
public function getId(): ?string
{
return $this->id;
}
/**
* @var Collection|ConversationMessage[]
*
* @ORM\OneToMany(targetEntity="\App\Entity\ConversationMessage\ConversationMessage", mappedBy="automatedConversationMessagesMailing", cascade={"persist", "remove"}, fetch="EXTRA_LAZY")
*/
private Collection $conversationMessages;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\RecurrentJob", inversedBy="automatedConversationMessagesMailings", cascade={"persist"})
*
* @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private ?RecurrentJob $recurrentJob;
/** @throws Exception */
public function getRecurrentJob(): ?RecurrentJob
{
if (!(
$this->mailingType === self::MAILING_TYPE_JOBRADAR
|| $this->mailingType === self::MAILING_TYPE_TANKUNDRAST
|| $this->mailingType === self::MAILING_TYPE_WISAG
|| $this->mailingType === self::MAILING_TYPE_WISAG_PREMIUM
|| $this->mailingType === self::MAILING_TYPE_RHENUS
|| $this->mailingType === self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR
|| $this->mailingType === self::MAILING_TYPE_LEAD_CONVERSION_RADAR)) {
throw new Exception('Cannot retrieve recurrent job for this mailing type.');
}
return $this->recurrentJob;
}
/**
* @ORM\ManyToOne(targetEntity="App\Entity\WantedJob", inversedBy="automatedConversationMessagesMailings", cascade={"persist"})
*
* @ORM\JoinColumn(name="wanted_jobs_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private ?WantedJob $wantedJob;
/** @throws Exception */
public function getWantedJob(): ?WantedJob
{
return $this->wantedJob;
}
/**
* @ORM\ManyToOne(targetEntity="App\Entity\ExternalPartner\IntegratedExternalPartnerCustomer", cascade={"persist"})
*
* @ORM\JoinColumn(name="integrated_external_partner_customers_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private ?IntegratedExternalPartnerCustomer $integratedExternalPartnerCustomer;
/** @throws Exception */
public function getIntegratedExternalPartnerCustomer(): ?IntegratedExternalPartnerCustomer
{
return $this->integratedExternalPartnerCustomer;
}
/**
* @var Collection|RecurrentJob[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\RecurrentJob", cascade={"persist"})
*
* @ORM\JoinTable(
* name="recurrent_jobs_automated_conversation_messages_mailings",
* inverseJoinColumns={
*
* @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", onDelete="CASCADE")
* },
* joinColumns={
* @ORM\JoinColumn(name="automated_conversation_messages_mailings_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*
* @Assert\Type("\Doctrine\Common\Collections\Collection")
*/
private Collection $recurrentJobs;
/**
* @return Collection|RecurrentJob[]
*
* @throws Exception
*/
public function getRecurrentJobs(): Collection
{
if (!($this->mailingType === self::MAILING_TYPE_JOBRADAR)) {
throw new Exception('Can only retrieve recurrent jobs if mailing type is jobradar (0).');
}
return $this->recurrentJobs;
}
public function addRecurrentJob(RecurrentJob $recurrentJob): void
{
$this->recurrentJobs->add($recurrentJob);
}
/**
* @var Collection|WantedJob[]
*
* @ORM\ManyToMany(targetEntity="App\Entity\WantedJob", cascade={"persist"})
*
* @ORM\JoinTable(
* name="wanted_jobs_automated_conversation_messages_mailings",
* inverseJoinColumns={
*
* @ORM\JoinColumn(name="wanted_jobs_id", referencedColumnName="id", onDelete="CASCADE")
* },
* joinColumns={
* @ORM\JoinColumn(name="automated_conversation_messages_mailings_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*
* @Assert\Type("\Doctrine\Common\Collections\Collection")
*/
private Collection $wantedJobs;
/**
* @return Collection|WantedJob[]
*
* @throws Exception
*/
public function getWantedJobs(): Collection
{
return $this->wantedJobs;
}
public function addWantedJob(WantedJob $wantedJob): void
{
$this->wantedJobs->add($wantedJob);
}
/**
* @ORM\Column(name="mailing_type", type="smallint", nullable=false)
*/
private int $mailingType;
public function getMailingType(): int
{
return $this->mailingType;
}
public function getTriggeringSide(): int
{
if ($this->mailingType !== self::MAILING_TYPE_JOBRADAR) {
return self::TRIGGERING_SIDE_UNDEFINED;
}
if (!is_null($this->recurrentJob)) {
return self::TRIGGERING_SIDE_RECURRENT_JOB;
}
if (!is_null($this->wantedJob)) {
return self::TRIGGERING_SIDE_WANTED_JOB;
}
return self::TRIGGERING_SIDE_UNDEFINED;
}
/**
* @ORM\Column(name="mailed_at", type="datetime", nullable=false)
*/
private DateTime $mailedAt;
public function getMailedAt(): DateTime
{
return $this->mailedAt;
}
public function setMailedAt(DateTime $mailedAt): void
{
$this->mailedAt = $mailedAt;
}
/**
* @ORM\Column(name="number_of_created_messages", type="integer", nullable=false)
*/
private int $numberOfCreatedMessages;
public function getNumberOfCreatedMessages(): int
{
return $this->numberOfCreatedMessages;
}
public function setNumberOfCreatedMessages(int $numberOfCreatedMessages): void
{
$this->numberOfCreatedMessages = $numberOfCreatedMessages;
}
}