src/App/Entity/Membership/RecurrentJobBooking.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Membership;
  3. use App\Entity\RecurrentJob;
  4. use App\Entity\User;
  5. use App\Utility\DateTimeUtility;
  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. /**
  12. * @ORM\Entity
  13. *
  14. * @ORM\Table(
  15. * name="recurrent_job_bookings"
  16. * )
  17. */
  18. class RecurrentJobBooking
  19. {
  20. public function __construct(User $user)
  21. {
  22. $this->user = $user;
  23. $this->user->addRecurrentJobBooking($this);
  24. $this->billwerkComponentSubscriptions = new ArrayCollection();
  25. }
  26. /**
  27. * @ORM\GeneratedValue(strategy="CUSTOM")
  28. *
  29. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  30. *
  31. * @ORM\Column(name="id", type="guid")
  32. *
  33. * @ORM\Id
  34. */
  35. private string $id;
  36. public function getId(): string
  37. {
  38. return $this->id;
  39. }
  40. /**
  41. * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="recurrentJobBookings", cascade={"persist"})
  42. *
  43. * @ORM\JoinColumn(name="users_id", onDelete="CASCADE", nullable=false)
  44. */
  45. private User $user;
  46. public function getUser(): User
  47. {
  48. return $this->user;
  49. }
  50. public function setUser(User $user): void
  51. {
  52. $this->user = $user;
  53. }
  54. /**
  55. * @ORM\OneToOne(targetEntity="App\Entity\RecurrentJob", inversedBy="booking", cascade={"persist"})
  56. *
  57. * @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
  58. */
  59. private ?RecurrentJob $recurrentJob;
  60. public function getRecurrentJob(): ?RecurrentJob
  61. {
  62. return $this->recurrentJob;
  63. }
  64. public function setRecurrentJob(?RecurrentJob $recurrentJob): void
  65. {
  66. if (!is_null($recurrentJob)) {
  67. if ($recurrentJob->getJoboffererProfile()->getUser()->getId() !== $this->user->getId()) {
  68. throw new InvalidArgumentException("Was asked to set recurrent job '{$recurrentJob->getId()}', but it belongs to user '{$recurrentJob->getJoboffererProfile()->getUser()->getId()}' and not to user '{$this->user->getId()}'.");
  69. }
  70. }
  71. $this->recurrentJob = $recurrentJob;
  72. }
  73. /**
  74. * @ORM\Column(name="renew_automatically", type="boolean", nullable=false)
  75. */
  76. private bool $renewAutomatically;
  77. public function setRenewAutomatically(bool $renewAutomatically): void
  78. {
  79. $this->renewAutomatically = $renewAutomatically;
  80. }
  81. public function isRenewAutomatically(): bool
  82. {
  83. return $this->renewAutomatically;
  84. }
  85. /**
  86. * @var BillwerkComponentSubscription[]|Collection
  87. *
  88. * @ORM\OneToMany(targetEntity="App\Entity\Membership\BillwerkComponentSubscription", mappedBy="recurrentJobBooking", cascade={"persist", "remove"})
  89. */
  90. private Collection $billwerkComponentSubscriptions;
  91. public function getBillwerkComponentSubscriptions(): Collection
  92. {
  93. return $this->billwerkComponentSubscriptions;
  94. }
  95. public function addBillwerkComponentSubscription(BillwerkComponentSubscription $billwerkComponentSubscription): void
  96. {
  97. $this->getBillwerkComponentSubscriptions()->add($billwerkComponentSubscription);
  98. }
  99. /** @throws Exception */
  100. public function hasActiveComponentSubscription(): bool
  101. {
  102. return $this->getActiveComponentSubscription() !== null;
  103. }
  104. public function getActiveComponentSubscription(): ?BillwerkComponentSubscription
  105. {
  106. $subscriptions = $this->billwerkComponentSubscriptions;
  107. foreach ($subscriptions as $subscription) {
  108. if (DateTimeUtility::createDateTimeUtc() >= $subscription->getStartDate()
  109. && DateTimeUtility::createDateTimeUtc() < $subscription->getEndDate()
  110. ) {
  111. return $subscription;
  112. }
  113. }
  114. return null;
  115. }
  116. public function getMostRecentlyStartingComponentSubscription(): ?BillwerkComponentSubscription
  117. {
  118. $subscriptions = $this->billwerkComponentSubscriptions;
  119. $mostRecentlyStartingComponentSubscription = null;
  120. foreach ($subscriptions as $subscription) {
  121. if (is_null($mostRecentlyStartingComponentSubscription)) {
  122. $mostRecentlyStartingComponentSubscription = $subscription;
  123. continue;
  124. }
  125. if ($subscription->getStartDate() > $mostRecentlyStartingComponentSubscription->getStartDate()) {
  126. $mostRecentlyStartingComponentSubscription = $subscription;
  127. }
  128. }
  129. return $mostRecentlyStartingComponentSubscription;
  130. }
  131. public function getEarliestStartingComponentSubscription(): ?BillwerkComponentSubscription
  132. {
  133. $subscriptions = $this->billwerkComponentSubscriptions;
  134. $earliestStartingComponentSubscription = null;
  135. foreach ($subscriptions as $subscription) {
  136. if (is_null($earliestStartingComponentSubscription)) {
  137. $earliestStartingComponentSubscription = $subscription;
  138. continue;
  139. }
  140. if ($subscription->getStartDate() < $earliestStartingComponentSubscription->getStartDate()) {
  141. $earliestStartingComponentSubscription = $subscription;
  142. }
  143. }
  144. return $earliestStartingComponentSubscription;
  145. }
  146. }