<?php
namespace App\Entity;
use App\Repository\PurchaseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Uid\Uuid;
/**
* @ORM\Entity(repositoryClass=PurchaseRepository::class)
* @ORM\Table(indexes={
* @ORM\Index(name="code_idx", columns={"code"}),
* @ORM\Index(name="login_code_idx", columns={"login_code"}),
* @ORM\Index(name="email_idx", columns={"email"}),
* }))
*/
class Purchase implements LoginCodeInterface
{
/**
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
*/
private string $id;
/**
* @ORM\Column(type="string", length=16, unique=true)
*/
private $number;
/**
* @ORM\Column(type="string", length=16)
*/
private $code;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName;
/**
* @ORM\Column(type="date_immutable")
*/
private $date;
/**
* @ORM\ManyToOne(targetEntity=Event::class)
* @ORM\JoinColumn(nullable=false)
*/
private $event;
/**
* @ORM\Column(type="string", length=128, nullable=true)
*/
private $token;
/**
* @ORM\Column(type="string", length=32, nullable=true)
*/
private $loginCode;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $loginCodeExpireAt;
/**
* @ORM\OneToMany(targetEntity=Ticket::class, mappedBy="purchase", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
private $tickets;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $campaignOpenedAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $campaignClickedAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $barcodeOpenedAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $ticketsSentAt;
/**
* @ORM\OneToMany(targetEntity=Invitee::class, mappedBy="purchase", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $invitees;
public function __construct()
{
$this->id = Uuid::v6();
$this->tickets = new ArrayCollection();
$this->invitees = new ArrayCollection();
}
public function getId(): string
{
return $this->id;
}
public function getNumber(): ?string
{
return $this->number;
}
public function setNumber(string $number): self
{
$this->number = $number;
return $this;
}
public function getCode(): ?string
{
return $this->code;
}
public function setCode(string $code): self
{
$this->code = $code;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getFirstName(): ?string
{
return $this->firstName;
}
public function setFirstName(?string $firstName): self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName(): ?string
{
return $this->lastName;
}
public function getFullName(): ?string
{
return trim($this->firstName.' '.$this->lastName);
}
public function setLastName(?string $lastName): self
{
$this->lastName = $lastName;
return $this;
}
public function getDate(): ?\DateTimeImmutable
{
return $this->date;
}
public function setDate(\DateTimeImmutable $date): self
{
$this->date = $date;
return $this;
}
public function getEvent(): ?Event
{
return $this->event;
}
public function setEvent(?Event $event): self
{
$this->event = $event;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(?string $token): self
{
$this->token = $token;
return $this;
}
public function getLoginCode(): ?string
{
return $this->loginCode;
}
public function setLoginCode(?string $loginCode): self
{
$this->loginCode = $loginCode;
return $this;
}
public function getLoginCodeExpireAt(): ?\DateTimeImmutable
{
return $this->loginCodeExpireAt;
}
public function setLoginCodeExpireAt(?\DateTimeImmutable $codeExpireAt): self
{
$this->loginCodeExpireAt = $codeExpireAt;
return $this;
}
/**
* @return Collection|Ticket[]
*/
public function getTicketById(string $id): Ticket
{
foreach ($this->tickets as $ticket) {
if ($ticket->getId()==$id) {
return $ticket;
}
}
throw new BadRequestHttpException();
}
/**
* @return Collection|Ticket[]
*/
public function getTickets(): Collection
{
return $this->tickets;
}
public function addTicket(Ticket $ticket): self
{
if (!$this->tickets->contains($ticket)) {
$this->tickets[] = $ticket;
$ticket->setPurchase($this);
}
return $this;
}
public function removeTicket(Ticket $ticket): self
{
if ($this->tickets->removeElement($ticket)) {
// set the owning side to null (unless already changed)
if ($ticket->getPurchase() === $this) {
$ticket->setPurchase(null);
}
}
return $this;
}
public function getCampaignOpenedAt(): ?\DateTimeImmutable
{
return $this->campaignOpenedAt;
}
public function setCampaignOpenedAt(?\DateTimeImmutable $campaignOpenedAt): self
{
$this->campaignOpenedAt = $campaignOpenedAt;
return $this;
}
public function getCampaignClickedAt(): ?\DateTimeImmutable
{
return $this->campaignClickedAt;
}
public function setCampaignClickedAt(?\DateTimeImmutable $campaignClickedAt): self
{
$this->campaignClickedAt = $campaignClickedAt;
return $this;
}
public function getBarcodeOpenedAt(): ?\DateTimeImmutable
{
return $this->barcodeOpenedAt;
}
public function setBarcodeOpenedAt(?\DateTimeImmutable $barcodeOpenedAt): self
{
$this->barcodeOpenedAt = $barcodeOpenedAt;
return $this;
}
public function getTicketsSentAt(): ?\DateTimeImmutable
{
return $this->ticketsSentAt;
}
public function setTicketsSentAt(?\DateTimeImmutable $ticketsSentAt): self
{
$this->ticketsSentAt = $ticketsSentAt;
return $this;
}
/**
* @return Collection<int, Invitee>
*/
public function getInvitees(): Collection
{
return $this->invitees;
}
public function addInvitee(Invitee $invitee): self
{
if (!$this->invitees->contains($invitee)) {
$this->invitees[] = $invitee;
$invitee->setPurchase($this);
}
return $this;
}
public function removeInvitee(Invitee $invitee): self
{
if ($this->invitees->removeElement($invitee)) {
// set the owning side to null (unless already changed)
if ($invitee->getPurchase() === $this) {
$invitee->setPurchase(null);
}
}
return $this;
}
public function isValidInviteeLoginCode(): bool
{
if (!empty($this->getLoginCode()) && $this->getLoginCodeExpireAt()>=(new \DateTime()))
return true;
foreach ($this->getInvitees() as $invitee) {
if (!empty($invitee->getLoginCode()) && ($invitee->getLoginCodeExpireAt()>=(new \DateTime()))) {
return true;
}
}
return false;
}
public function checkLoginCode(string $code): ?LoginCodeInterface
{
if ($this->getLoginCode()==$code && $this->getLoginCodeExpireAt()>=(new \DateTime())) {
return $this;
}
foreach ($this->getInvitees() as $invitee) {
if ($invitee->getLoginCode()==$code && $invitee->getLoginCodeExpireAt()>=(new \DateTime())) {
return $invitee;
}
}
return null;
}
}