src/Entity/Purchase.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PurchaseRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. use Symfony\Component\Uid\Uuid;
  10. /**
  11.  * @ORM\Entity(repositoryClass=PurchaseRepository::class)
  12.  * @ORM\Table(indexes={
  13.  *     @ORM\Index(name="code_idx", columns={"code"}),
  14.  *     @ORM\Index(name="login_code_idx", columns={"login_code"}),
  15.  *     @ORM\Index(name="email_idx", columns={"email"}),
  16.  * }))
  17.  */
  18. class Purchase implements LoginCodeInterface
  19. {
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\Column(type="uuid", unique=true)
  23.      */
  24.     private string $id;
  25.     /**
  26.      * @ORM\Column(type="string", length=16, unique=true)
  27.      */
  28.     private $number;
  29.     /**
  30.      * @ORM\Column(type="string", length=16)
  31.      */
  32.     private $code;
  33.     /**
  34.      * @ORM\Column(type="string", length=255)
  35.      */
  36.     private $email;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $firstName;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $lastName;
  45.     /**
  46.      * @ORM\Column(type="date_immutable")
  47.      */
  48.     private $date;
  49.     /**
  50.      * @ORM\ManyToOne(targetEntity=Event::class)
  51.      * @ORM\JoinColumn(nullable=false)
  52.      */
  53.     private $event;
  54.     /**
  55.      * @ORM\Column(type="string", length=128, nullable=true)
  56.      */
  57.     private $token;
  58.     /**
  59.      * @ORM\Column(type="string", length=32, nullable=true)
  60.      */
  61.     private $loginCode;
  62.     /**
  63.      * @ORM\Column(type="datetime_immutable", nullable=true)
  64.      */
  65.     private $loginCodeExpireAt;
  66.     /**
  67.      * @ORM\OneToMany(targetEntity=Ticket::class, mappedBy="purchase", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
  68.      */
  69.     private $tickets;
  70.     /**
  71.      * @ORM\Column(type="datetime_immutable", nullable=true)
  72.      */
  73.     private $campaignOpenedAt;
  74.     /**
  75.      * @ORM\Column(type="datetime_immutable", nullable=true)
  76.      */
  77.     private $campaignClickedAt;
  78.     /**
  79.      * @ORM\Column(type="datetime_immutable", nullable=true)
  80.      */
  81.     private $barcodeOpenedAt;
  82.     /**
  83.      * @ORM\Column(type="datetime_immutable", nullable=true)
  84.      */
  85.     private $ticketsSentAt;
  86.     /**
  87.      * @ORM\OneToMany(targetEntity=Invitee::class, mappedBy="purchase", cascade={"persist", "remove"}, orphanRemoval=true)
  88.      */
  89.     private $invitees;
  90.     public function __construct()
  91.     {
  92.         $this->id Uuid::v6();
  93.         $this->tickets = new ArrayCollection();
  94.         $this->invitees = new ArrayCollection();
  95.     }
  96.     public function getId(): string
  97.     {
  98.         return $this->id;
  99.     }
  100.     public function getNumber(): ?string
  101.     {
  102.         return $this->number;
  103.     }
  104.     public function setNumber(string $number): self
  105.     {
  106.         $this->number $number;
  107.         return $this;
  108.     }
  109.     public function getCode(): ?string
  110.     {
  111.         return $this->code;
  112.     }
  113.     public function setCode(string $code): self
  114.     {
  115.         $this->code $code;
  116.         return $this;
  117.     }
  118.     public function getEmail(): ?string
  119.     {
  120.         return $this->email;
  121.     }
  122.     public function setEmail(string $email): self
  123.     {
  124.         $this->email $email;
  125.         return $this;
  126.     }
  127.     public function getFirstName(): ?string
  128.     {
  129.         return $this->firstName;
  130.     }
  131.     public function setFirstName(?string $firstName): self
  132.     {
  133.         $this->firstName $firstName;
  134.         return $this;
  135.     }
  136.     public function getLastName(): ?string
  137.     {
  138.         return $this->lastName;
  139.     }
  140.     public function getFullName(): ?string
  141.     {
  142.         return trim($this->firstName.' '.$this->lastName);
  143.     }
  144.     public function setLastName(?string $lastName): self
  145.     {
  146.         $this->lastName $lastName;
  147.         return $this;
  148.     }
  149.     public function getDate(): ?\DateTimeImmutable
  150.     {
  151.         return $this->date;
  152.     }
  153.     public function setDate(\DateTimeImmutable $date): self
  154.     {
  155.         $this->date $date;
  156.         return $this;
  157.     }
  158.     public function getEvent(): ?Event
  159.     {
  160.         return $this->event;
  161.     }
  162.     public function setEvent(?Event $event): self
  163.     {
  164.         $this->event $event;
  165.         return $this;
  166.     }
  167.     public function getToken(): ?string
  168.     {
  169.         return $this->token;
  170.     }
  171.     public function setToken(?string $token): self
  172.     {
  173.         $this->token $token;
  174.         return $this;
  175.     }
  176.     public function getLoginCode(): ?string
  177.     {
  178.         return $this->loginCode;
  179.     }
  180.     public function setLoginCode(?string $loginCode): self
  181.     {
  182.         $this->loginCode $loginCode;
  183.         return $this;
  184.     }
  185.     public function getLoginCodeExpireAt(): ?\DateTimeImmutable
  186.     {
  187.         return $this->loginCodeExpireAt;
  188.     }
  189.     public function setLoginCodeExpireAt(?\DateTimeImmutable $codeExpireAt): self
  190.     {
  191.         $this->loginCodeExpireAt $codeExpireAt;
  192.         return $this;
  193.     }
  194.     /**
  195.      * @return Collection|Ticket[]
  196.      */
  197.     public function getTicketById(string $id): Ticket
  198.     {
  199.         foreach ($this->tickets as $ticket) {
  200.             if ($ticket->getId()==$id) {
  201.                 return $ticket;
  202.             }
  203.         }
  204.         throw new BadRequestHttpException();
  205.     }
  206.     /**
  207.      * @return Collection|Ticket[]
  208.      */
  209.     public function getTickets(): Collection
  210.     {
  211.         return $this->tickets;
  212.     }
  213.     public function addTicket(Ticket $ticket): self
  214.     {
  215.         if (!$this->tickets->contains($ticket)) {
  216.             $this->tickets[] = $ticket;
  217.             $ticket->setPurchase($this);
  218.         }
  219.         return $this;
  220.     }
  221.     public function removeTicket(Ticket $ticket): self
  222.     {
  223.         if ($this->tickets->removeElement($ticket)) {
  224.             // set the owning side to null (unless already changed)
  225.             if ($ticket->getPurchase() === $this) {
  226.                 $ticket->setPurchase(null);
  227.             }
  228.         }
  229.         return $this;
  230.     }
  231.     public function getCampaignOpenedAt(): ?\DateTimeImmutable
  232.     {
  233.         return $this->campaignOpenedAt;
  234.     }
  235.     public function setCampaignOpenedAt(?\DateTimeImmutable $campaignOpenedAt): self
  236.     {
  237.         $this->campaignOpenedAt $campaignOpenedAt;
  238.         return $this;
  239.     }
  240.     public function getCampaignClickedAt(): ?\DateTimeImmutable
  241.     {
  242.         return $this->campaignClickedAt;
  243.     }
  244.     public function setCampaignClickedAt(?\DateTimeImmutable $campaignClickedAt): self
  245.     {
  246.         $this->campaignClickedAt $campaignClickedAt;
  247.         return $this;
  248.     }
  249.     public function getBarcodeOpenedAt(): ?\DateTimeImmutable
  250.     {
  251.         return $this->barcodeOpenedAt;
  252.     }
  253.     public function setBarcodeOpenedAt(?\DateTimeImmutable $barcodeOpenedAt): self
  254.     {
  255.         $this->barcodeOpenedAt $barcodeOpenedAt;
  256.         return $this;
  257.     }
  258.     public function getTicketsSentAt(): ?\DateTimeImmutable
  259.     {
  260.         return $this->ticketsSentAt;
  261.     }
  262.     public function setTicketsSentAt(?\DateTimeImmutable $ticketsSentAt): self
  263.     {
  264.         $this->ticketsSentAt $ticketsSentAt;
  265.         return $this;
  266.     }
  267.     /**
  268.      * @return Collection<int, Invitee>
  269.      */
  270.     public function getInvitees(): Collection
  271.     {
  272.         return $this->invitees;
  273.     }
  274.     public function addInvitee(Invitee $invitee): self
  275.     {
  276.         if (!$this->invitees->contains($invitee)) {
  277.             $this->invitees[] = $invitee;
  278.             $invitee->setPurchase($this);
  279.         }
  280.         return $this;
  281.     }
  282.     public function removeInvitee(Invitee $invitee): self
  283.     {
  284.         if ($this->invitees->removeElement($invitee)) {
  285.             // set the owning side to null (unless already changed)
  286.             if ($invitee->getPurchase() === $this) {
  287.                 $invitee->setPurchase(null);
  288.             }
  289.         }
  290.         return $this;
  291.     }
  292.     public function isValidInviteeLoginCode(): bool
  293.     {
  294.         if (!empty($this->getLoginCode()) && $this->getLoginCodeExpireAt()>=(new \DateTime()))
  295.             return true;
  296.         foreach ($this->getInvitees() as $invitee) {
  297.             if (!empty($invitee->getLoginCode()) && ($invitee->getLoginCodeExpireAt()>=(new \DateTime()))) {
  298.                 return true;
  299.             }
  300.         }
  301.         return false;
  302.     }
  303.     public function checkLoginCode(string $code): ?LoginCodeInterface
  304.     {
  305.         if ($this->getLoginCode()==$code && $this->getLoginCodeExpireAt()>=(new \DateTime())) {
  306.             return $this;
  307.         }
  308.         foreach ($this->getInvitees() as $invitee) {
  309.             if ($invitee->getLoginCode()==$code && $invitee->getLoginCodeExpireAt()>=(new \DateTime())) {
  310.                 return $invitee;
  311.             }
  312.         }
  313.         return null;
  314.     }
  315. }