src/App/Entity/Membership/BillwerkComponentSubscription.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Membership;
  3. use App\Enum\Billwerk\ComponentContext;
  4. use App\Utility\ReflectionHelper;
  5. use DateTime;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use InvalidArgumentException;
  8. use JanusHercules\SelfServiceTrafficCampaign\Domain\Entity\SelfServiceTrafficCampaignBooking;
  9. use LogicException;
  10. /**
  11. * @ORM\Entity()
  12. *
  13. * @ORM\Table(
  14. * name="billwerk_component_subscriptions"
  15. * )
  16. *
  17. * Note that this entity is actually used to represent two different Billwerk entities: component subscriptions and usages,
  18. * which are very similar in nature but not the same things. Component subscriptions are used to back the recurrent job bookings
  19. * while the user's contract is in trial, and upon renewal, usages are used to back the recurrent job bookings.
  20. */
  21. class BillwerkComponentSubscription
  22. {
  23. public const COMPONENT_SUBSCRIPTION_TYPE_COMPONENT_SUBSCRIPTION = 0;
  24. public const COMPONENT_SUBSCRIPTION_TYPE_USAGE_COMPONENT = 1;
  25. public function __construct(
  26. int $type,
  27. string $id,
  28. BillwerkContract $billwerkContract,
  29. string $componentId,
  30. DateTime $startDate,
  31. DateTime $endDate,
  32. ?RecurrentJobBooking $recurrentJobBooking = null,
  33. ?SelfServiceTrafficCampaignBooking $selfServiceTrafficCampaignBooking = null,
  34. bool $includedInInterimBilling = false
  35. ) {
  36. if (!ReflectionHelper::hasConstWithValue(self::class, 'COMPONENT_SUBSCRIPTION_TYPE_', $type)) {
  37. throw new InvalidArgumentException("No component subscription type for value '$type'.");
  38. }
  39. if (is_null($recurrentJobBooking) && is_null($selfServiceTrafficCampaignBooking)) {
  40. throw new InvalidArgumentException('Either recurrentJobBooking or selfServiceTrafficCampaignBooking must be provided.');
  41. }
  42. if (!is_null($recurrentJobBooking) && !is_null($selfServiceTrafficCampaignBooking)) {
  43. throw new InvalidArgumentException('Cannot provide both recurrentJobBooking and selfServiceTrafficCampaignBooking.');
  44. }
  45. $this->type = $type;
  46. $this->id = $id;
  47. $this->billwerkContract = $billwerkContract;
  48. $this->componentId = $componentId;
  49. $this->startDate = $startDate;
  50. $this->endDate = $endDate;
  51. $this->recurrentJobBooking = $recurrentJobBooking;
  52. $this->selfServiceTrafficCampaignBooking = $selfServiceTrafficCampaignBooking;
  53. $this->includedInInterimBilling = $includedInInterimBilling;
  54. }
  55. /**
  56. * @ORM\Column(name="id", type="string")
  57. *
  58. * @ORM\Id
  59. */
  60. private string $id;
  61. public function getId(): string
  62. {
  63. return $this->id;
  64. }
  65. /**
  66. * @ORM\ManyToOne(targetEntity="App\Entity\Membership\BillwerkContract")
  67. *
  68. * @ORM\JoinColumn(name="billwerk_contracts_id", onDelete="CASCADE", nullable=false)
  69. */
  70. private BillwerkContract $billwerkContract;
  71. /**
  72. * @ORM\ManyToOne(targetEntity="App\Entity\Membership\RecurrentJobBooking", inversedBy="billwerkComponentSubscriptions", cascade={"persist"})
  73. *
  74. * @ORM\JoinColumn(name="recurrent_job_bookings_id", onDelete="CASCADE", nullable=true)
  75. */
  76. private ?RecurrentJobBooking $recurrentJobBooking;
  77. public function getRecurrentJobBooking(): ?RecurrentJobBooking
  78. {
  79. return $this->recurrentJobBooking;
  80. }
  81. /**
  82. * @ORM\OneToOne(targetEntity="JanusHercules\SelfServiceTrafficCampaign\Domain\Entity\SelfServiceTrafficCampaignBooking", inversedBy="billwerkComponentSubscription")
  83. *
  84. * @ORM\JoinColumn(name="self_service_traffic_campaign_bookings_id", onDelete="CASCADE", nullable=true)
  85. */
  86. private ?SelfServiceTrafficCampaignBooking $selfServiceTrafficCampaignBooking;
  87. public function getSelfServiceTrafficCampaignBooking(): ?SelfServiceTrafficCampaignBooking
  88. {
  89. return $this->selfServiceTrafficCampaignBooking;
  90. }
  91. /**
  92. * @ORM\Column(name="component_id", type="string", length=255, nullable=false)
  93. */
  94. private string $componentId;
  95. public function getComponentId(): string
  96. {
  97. return $this->componentId;
  98. }
  99. /**
  100. * @ORM\Column(name="start_date", type="datetime")
  101. */
  102. private DateTime $startDate;
  103. public function getStartDate(): DateTime
  104. {
  105. return $this->startDate;
  106. }
  107. /**
  108. * @ORM\Column(name="end_date", type="datetime")
  109. */
  110. private DateTime $endDate;
  111. public function getEndDate(): DateTime
  112. {
  113. return $this->endDate;
  114. }
  115. public function setEndDate(DateTime $endDate): void
  116. {
  117. $this->endDate = $endDate;
  118. }
  119. /**
  120. * @ORM\Column(name="type", type="integer", nullable=false)
  121. */
  122. private int $type;
  123. public function getType(): int
  124. {
  125. return $this->type;
  126. }
  127. /**
  128. * @ORM\Column(name="included_in_interim_billing", type="boolean", nullable=false)
  129. */
  130. private bool $includedInInterimBilling;
  131. public function isIncludedInInterimBilling(): bool
  132. {
  133. return $this->includedInInterimBilling;
  134. }
  135. public function setIsIncludedInInterimBilling(bool $includedInInterimBilling): void
  136. {
  137. $this->includedInInterimBilling = $includedInInterimBilling;
  138. }
  139. public function getComponentContext(): ComponentContext
  140. {
  141. if ($this->recurrentJobBooking !== null) {
  142. return ComponentContext::PLUS_RECURRENT_JOB;
  143. }
  144. if ($this->selfServiceTrafficCampaignBooking !== null) {
  145. return ComponentContext::SELF_SERVICE_TRAFFIC_CAMPAIGN;
  146. }
  147. throw new LogicException('Could not determine component context for BillwerkComponentSubscription: Neither a RecurrentJobBooking nor a SelfServiceTrafficCampaignBooking is set.');
  148. }
  149. }