<?php
namespace JanusHercules\Membership\Domain\Entity;
use App\Entity\RecurrentJob;
use App\Entity\User;
use App\Utility\DatabaseIdGenerator;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use InvalidArgumentException;
#[ORM\Entity]
#[ORM\Table(name: 'flex_membership_recurrent_job_slots')]
class FlexMembershipRecurrentJobSlot
{
public function __construct(
User $user,
?RecurrentJob $recurrentJob = null
) {
$this->user = $user;
if (!is_null($recurrentJob) && $recurrentJob->isPlus()) {
throw new InvalidArgumentException("Cannot set recurrent job '{$recurrentJob->getId()}' for flex membership slot because it is plus!");
}
$this->recurrentJob = $recurrentJob;
}
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator(class: DatabaseIdGenerator::class)]
#[ORM\Column(
type: Types::GUID,
unique: true
)]
private ?string $id = null;
public function getId(): ?string
{
return $this->id;
}
#[ORM\ManyToOne(
targetEntity: User::class,
cascade: ['persist'],
inversedBy: 'recurrentJobSlots'
)]
#[ORM\JoinColumn(
name: 'users_id',
referencedColumnName: 'id',
nullable: false,
onDelete: 'CASCADE'
)]
private User $user;
public function getUser(): User
{
return $this->user;
}
public function setUser(User $user): void
{
$this->user = $user;
}
#[ORM\OneToOne(
inversedBy : 'slot',
targetEntity: RecurrentJob::class,
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) && !is_null($joboffererProfile = $recurrentJob->getJoboffererProfile())) {
if ($joboffererProfile->getUser()->getId() !== $this->user->getId()) {
throw new InvalidArgumentException("Was asked to set recurrent job '{$recurrentJob->getId()}', but it belongs to user '{$joboffererProfile->getUser()->getId()}' and not to user '{$this->user->getId()}'.");
}
} elseif (!is_null($recurrentJob) && $recurrentJob->isPlus()) {
throw new InvalidArgumentException("Was asked to set recurrent job '{$recurrentJob->getId()}', but recurrent job is already plus.");
}
$this->recurrentJob = $recurrentJob;
}
}