<?php
namespace App\Entity\Membership;
use App\Entity\RecurrentJob;
use App\Entity\User;
use App\Utility\DateTimeUtility;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use InvalidArgumentException;
/**
* @ORM\Entity
*
* @ORM\Table(
* name="recurrent_job_bookings"
* )
*/
class RecurrentJobBooking
{
public function __construct(User $user)
{
$this->user = $user;
$this->user->addRecurrentJobBooking($this);
$this->billwerkComponentSubscriptions = new ArrayCollection();
}
/**
* @ORM\GeneratedValue(strategy="CUSTOM")
*
* @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
*
* @ORM\Column(name="id", type="guid")
*
* @ORM\Id
*/
private string $id;
public function getId(): string
{
return $this->id;
}
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="recurrentJobBookings", cascade={"persist"})
*
* @ORM\JoinColumn(name="users_id", onDelete="CASCADE", nullable=false)
*/
private User $user;
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): void
{
$this->user = $user;
}
/**
* @ORM\OneToOne(targetEntity="App\Entity\RecurrentJob", inversedBy="booking", cascade={"persist"})
*
* @ORM\JoinColumn(name="recurrent_jobs_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private ?RecurrentJob $recurrentJob;
public function getRecurrentJob(): ?RecurrentJob
{
return $this->recurrentJob;
}
public function setRecurrentJob(?RecurrentJob $recurrentJob): void
{
if (!is_null($recurrentJob)) {
if ($recurrentJob->getJoboffererProfile()->getUser()->getId() !== $this->user->getId()) {
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()}'.");
}
}
$this->recurrentJob = $recurrentJob;
}
/**
* @ORM\Column(name="renew_automatically", type="boolean", nullable=false)
*/
private bool $renewAutomatically;
public function setRenewAutomatically(bool $renewAutomatically): void
{
$this->renewAutomatically = $renewAutomatically;
}
public function isRenewAutomatically(): bool
{
return $this->renewAutomatically;
}
/**
* @var BillwerkComponentSubscription[]|Collection
*
* @ORM\OneToMany(targetEntity="App\Entity\Membership\BillwerkComponentSubscription", mappedBy="recurrentJobBooking", cascade={"persist", "remove"})
*/
private Collection $billwerkComponentSubscriptions;
public function getBillwerkComponentSubscriptions(): Collection
{
return $this->billwerkComponentSubscriptions;
}
public function addBillwerkComponentSubscription(BillwerkComponentSubscription $billwerkComponentSubscription): void
{
$this->getBillwerkComponentSubscriptions()->add($billwerkComponentSubscription);
}
/** @throws Exception */
public function hasActiveComponentSubscription(): bool
{
return $this->getActiveComponentSubscription() !== null;
}
public function getActiveComponentSubscription(): ?BillwerkComponentSubscription
{
$subscriptions = $this->billwerkComponentSubscriptions;
foreach ($subscriptions as $subscription) {
if (DateTimeUtility::createDateTimeUtc() >= $subscription->getStartDate()
&& DateTimeUtility::createDateTimeUtc() < $subscription->getEndDate()
) {
return $subscription;
}
}
return null;
}
public function getMostRecentlyStartingComponentSubscription(): ?BillwerkComponentSubscription
{
$subscriptions = $this->billwerkComponentSubscriptions;
$mostRecentlyStartingComponentSubscription = null;
foreach ($subscriptions as $subscription) {
if (is_null($mostRecentlyStartingComponentSubscription)) {
$mostRecentlyStartingComponentSubscription = $subscription;
continue;
}
if ($subscription->getStartDate() > $mostRecentlyStartingComponentSubscription->getStartDate()) {
$mostRecentlyStartingComponentSubscription = $subscription;
}
}
return $mostRecentlyStartingComponentSubscription;
}
public function getEarliestStartingComponentSubscription(): ?BillwerkComponentSubscription
{
$subscriptions = $this->billwerkComponentSubscriptions;
$earliestStartingComponentSubscription = null;
foreach ($subscriptions as $subscription) {
if (is_null($earliestStartingComponentSubscription)) {
$earliestStartingComponentSubscription = $subscription;
continue;
}
if ($subscription->getStartDate() < $earliestStartingComponentSubscription->getStartDate()) {
$earliestStartingComponentSubscription = $subscription;
}
}
return $earliestStartingComponentSubscription;
}
}