src/App/Entity/AutomatedConversationMessagesMailing.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\ConversationMessage\ConversationMessage;
  4. use App\Entity\ExternalPartner\IntegratedExternalPartnerCustomer;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Exception;
  10. use InvalidArgumentException;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use ValueError;
  13. /**
  14. * @ORM\Entity
  15. *
  16. * @ORM\Table(
  17. * name="automated_conversation_messages_mailings",
  18. * indexes={
  19. *
  20. * @ORM\Index(name="mailing_type_mailed_at_idx", columns={"mailing_type", "mailed_at"}),
  21. * @ORM\Index(name="recurrent_jobs_id_mailing_type_mailed_at_idx", columns={"recurrent_jobs_id", "mailing_type", "mailed_at"})
  22. * }
  23. * )
  24. */
  25. class AutomatedConversationMessagesMailing
  26. {
  27. public const MAILING_TYPE_JOBRADAR = 0;
  28. // LEGACY
  29. public const MAILING_TYPE_TANKUNDRAST = 1;
  30. public const MAILING_TYPE_WISAG = 2;
  31. public const MAILING_TYPE_WISAG_PREMIUM = 3;
  32. public const MAILING_TYPE_RHENUS = 4;
  33. public const MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR = 5;
  34. public const MAILING_TYPE_LEAD_CONVERSION_RADAR = 6;
  35. public const MAILING_TYPES = [
  36. self::MAILING_TYPE_JOBRADAR,
  37. self::MAILING_TYPE_TANKUNDRAST,
  38. self::MAILING_TYPE_WISAG,
  39. self::MAILING_TYPE_WISAG_PREMIUM,
  40. self::MAILING_TYPE_RHENUS,
  41. self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR,
  42. self::MAILING_TYPE_LEAD_CONVERSION_RADAR
  43. ];
  44. public const TRIGGERING_SIDE_UNDEFINED = 0;
  45. public const TRIGGERING_SIDE_RECURRENT_JOB = 1;
  46. public const TRIGGERING_SIDE_WANTED_JOB = 2;
  47. /** @throws InvalidArgumentException */
  48. public function __construct(
  49. ?RecurrentJob $recurrentJob,
  50. ?WantedJob $wantedJob,
  51. int $mailingType,
  52. DateTime $mailedAt,
  53. ?IntegratedExternalPartnerCustomer $integratedExternalPartnerCustomer = null
  54. ) {
  55. if ($mailingType === self::MAILING_TYPE_JOBRADAR) {
  56. throw new ValueError('Jobradar is now based on JobradarMatches, therefore new AutomatedConversationMessagesMailings of this type must no longer be created.');
  57. }
  58. if ($mailingType === self::MAILING_TYPE_LEAD_CONVERSION_RADAR
  59. && (is_null($wantedJob) || is_null($recurrentJob))
  60. ) {
  61. throw new InvalidArgumentException('Mailing type lead conversion radar (6) must be called with both $wantedJob and $recurrentJob being not null.');
  62. }
  63. if (!is_null($wantedJob)
  64. && !is_null($recurrentJob)
  65. && $mailingType !== self::MAILING_TYPE_LEAD_CONVERSION_RADAR
  66. ) {
  67. throw new InvalidArgumentException('Must be called with $wantedJob or $recurrentJob, but not both if mailing type is not lead conversion radar (6).');
  68. }
  69. if (!is_null($recurrentJob)
  70. && !($mailingType === self::MAILING_TYPE_TANKUNDRAST
  71. || $mailingType === self::MAILING_TYPE_WISAG
  72. || $mailingType === self::MAILING_TYPE_WISAG_PREMIUM
  73. || $mailingType === self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR
  74. || $mailingType === self::MAILING_TYPE_LEAD_CONVERSION_RADAR
  75. )
  76. ) {
  77. 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).');
  78. }
  79. if (!is_null($wantedJob)
  80. && $mailingType !== self::MAILING_TYPE_LEAD_CONVERSION_RADAR
  81. ) {
  82. throw new InvalidArgumentException('A wanted job can only be set if mailing type is jobradar (0) or lead conversion radar (6).');
  83. }
  84. if (($mailingType === self::MAILING_TYPE_TANKUNDRAST
  85. || $mailingType === self::MAILING_TYPE_WISAG
  86. || $mailingType === self::MAILING_TYPE_WISAG_PREMIUM
  87. || $mailingType === self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR
  88. )
  89. && is_null($recurrentJob)
  90. ) {
  91. 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.');
  92. }
  93. if (!in_array($mailingType, self::MAILING_TYPES)) {
  94. throw new InvalidArgumentException('Mailing type ' . $mailingType . ' is invalid.');
  95. }
  96. $this->recurrentJob = $recurrentJob;
  97. $this->wantedJob = $wantedJob;
  98. $this->mailingType = $mailingType;
  99. $this->mailedAt = $mailedAt;
  100. $this->conversationMessages = new ArrayCollection();
  101. $this->recurrentJobs = new ArrayCollection();
  102. $this->wantedJobs = new ArrayCollection();
  103. $this->numberOfCreatedMessages = 0;
  104. $this->integratedExternalPartnerCustomer = $integratedExternalPartnerCustomer;
  105. }
  106. /**
  107. * @ORM\GeneratedValue(strategy="CUSTOM")
  108. *
  109. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  110. *
  111. * @ORM\Column(name="id", type="guid")
  112. *
  113. * @ORM\Id
  114. */
  115. private ?string $id = null;
  116. public function getId(): ?string
  117. {
  118. return $this->id;
  119. }
  120. /**
  121. * @var Collection|ConversationMessage[]
  122. *
  123. * @ORM\OneToMany(targetEntity="\App\Entity\ConversationMessage\ConversationMessage", mappedBy="automatedConversationMessagesMailing", cascade={"persist", "remove"}, fetch="EXTRA_LAZY")
  124. */
  125. private Collection $conversationMessages;
  126. /**
  127. * @ORM\ManyToOne(targetEntity="App\Entity\RecurrentJob", inversedBy="automatedConversationMessagesMailings", cascade={"persist"})
  128. *
  129. * @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  130. */
  131. private ?RecurrentJob $recurrentJob;
  132. /** @throws Exception */
  133. public function getRecurrentJob(): ?RecurrentJob
  134. {
  135. if (!(
  136. $this->mailingType === self::MAILING_TYPE_JOBRADAR
  137. || $this->mailingType === self::MAILING_TYPE_TANKUNDRAST
  138. || $this->mailingType === self::MAILING_TYPE_WISAG
  139. || $this->mailingType === self::MAILING_TYPE_WISAG_PREMIUM
  140. || $this->mailingType === self::MAILING_TYPE_RHENUS
  141. || $this->mailingType === self::MAILING_TYPE_INTEGRATED_EXTERNAL_PARTNER_CUSTOMER_PREMIUM_RADAR
  142. || $this->mailingType === self::MAILING_TYPE_LEAD_CONVERSION_RADAR)) {
  143. throw new Exception('Cannot retrieve recurrent job for this mailing type.');
  144. }
  145. return $this->recurrentJob;
  146. }
  147. /**
  148. * @ORM\ManyToOne(targetEntity="App\Entity\WantedJob", inversedBy="automatedConversationMessagesMailings", cascade={"persist"})
  149. *
  150. * @ORM\JoinColumn(name="wanted_jobs_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  151. */
  152. private ?WantedJob $wantedJob;
  153. /** @throws Exception */
  154. public function getWantedJob(): ?WantedJob
  155. {
  156. return $this->wantedJob;
  157. }
  158. /**
  159. * @ORM\ManyToOne(targetEntity="App\Entity\ExternalPartner\IntegratedExternalPartnerCustomer", cascade={"persist"})
  160. *
  161. * @ORM\JoinColumn(name="integrated_external_partner_customers_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  162. */
  163. private ?IntegratedExternalPartnerCustomer $integratedExternalPartnerCustomer;
  164. /** @throws Exception */
  165. public function getIntegratedExternalPartnerCustomer(): ?IntegratedExternalPartnerCustomer
  166. {
  167. return $this->integratedExternalPartnerCustomer;
  168. }
  169. /**
  170. * @var Collection|RecurrentJob[]
  171. *
  172. * @ORM\ManyToMany(targetEntity="App\Entity\RecurrentJob", cascade={"persist"})
  173. *
  174. * @ORM\JoinTable(
  175. * name="recurrent_jobs_automated_conversation_messages_mailings",
  176. * inverseJoinColumns={
  177. *
  178. * @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", onDelete="CASCADE")
  179. * },
  180. * joinColumns={
  181. * @ORM\JoinColumn(name="automated_conversation_messages_mailings_id", referencedColumnName="id", onDelete="CASCADE")
  182. * }
  183. * )
  184. *
  185. * @Assert\Type("\Doctrine\Common\Collections\Collection")
  186. */
  187. private Collection $recurrentJobs;
  188. /**
  189. * @return Collection|RecurrentJob[]
  190. *
  191. * @throws Exception
  192. */
  193. public function getRecurrentJobs(): Collection
  194. {
  195. if (!($this->mailingType === self::MAILING_TYPE_JOBRADAR)) {
  196. throw new Exception('Can only retrieve recurrent jobs if mailing type is jobradar (0).');
  197. }
  198. return $this->recurrentJobs;
  199. }
  200. public function addRecurrentJob(RecurrentJob $recurrentJob): void
  201. {
  202. $this->recurrentJobs->add($recurrentJob);
  203. }
  204. /**
  205. * @var Collection|WantedJob[]
  206. *
  207. * @ORM\ManyToMany(targetEntity="App\Entity\WantedJob", cascade={"persist"})
  208. *
  209. * @ORM\JoinTable(
  210. * name="wanted_jobs_automated_conversation_messages_mailings",
  211. * inverseJoinColumns={
  212. *
  213. * @ORM\JoinColumn(name="wanted_jobs_id", referencedColumnName="id", onDelete="CASCADE")
  214. * },
  215. * joinColumns={
  216. * @ORM\JoinColumn(name="automated_conversation_messages_mailings_id", referencedColumnName="id", onDelete="CASCADE")
  217. * }
  218. * )
  219. *
  220. * @Assert\Type("\Doctrine\Common\Collections\Collection")
  221. */
  222. private Collection $wantedJobs;
  223. /**
  224. * @return Collection|WantedJob[]
  225. *
  226. * @throws Exception
  227. */
  228. public function getWantedJobs(): Collection
  229. {
  230. return $this->wantedJobs;
  231. }
  232. public function addWantedJob(WantedJob $wantedJob): void
  233. {
  234. $this->wantedJobs->add($wantedJob);
  235. }
  236. /**
  237. * @ORM\Column(name="mailing_type", type="smallint", nullable=false)
  238. */
  239. private int $mailingType;
  240. public function getMailingType(): int
  241. {
  242. return $this->mailingType;
  243. }
  244. public function getTriggeringSide(): int
  245. {
  246. if ($this->mailingType !== self::MAILING_TYPE_JOBRADAR) {
  247. return self::TRIGGERING_SIDE_UNDEFINED;
  248. }
  249. if (!is_null($this->recurrentJob)) {
  250. return self::TRIGGERING_SIDE_RECURRENT_JOB;
  251. }
  252. if (!is_null($this->wantedJob)) {
  253. return self::TRIGGERING_SIDE_WANTED_JOB;
  254. }
  255. return self::TRIGGERING_SIDE_UNDEFINED;
  256. }
  257. /**
  258. * @ORM\Column(name="mailed_at", type="datetime", nullable=false)
  259. */
  260. private DateTime $mailedAt;
  261. public function getMailedAt(): DateTime
  262. {
  263. return $this->mailedAt;
  264. }
  265. public function setMailedAt(DateTime $mailedAt): void
  266. {
  267. $this->mailedAt = $mailedAt;
  268. }
  269. /**
  270. * @ORM\Column(name="number_of_created_messages", type="integer", nullable=false)
  271. */
  272. private int $numberOfCreatedMessages;
  273. public function getNumberOfCreatedMessages(): int
  274. {
  275. return $this->numberOfCreatedMessages;
  276. }
  277. public function setNumberOfCreatedMessages(int $numberOfCreatedMessages): void
  278. {
  279. $this->numberOfCreatedMessages = $numberOfCreatedMessages;
  280. }
  281. }