<?php
namespace App\Entity;
use App\Repository\CardRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints\Range;
#[ORM\Entity(repositoryClass: CardRepository::class)]
class Card
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\OneToOne(inversedBy: 'card', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\Column(length: 255)]
#[Assert\File(mimeTypes: ["image/png", "image/jpeg"])]
private ?string $qrCodeImage = null;
#[ORM\Column(nullable: false)]
#[Range(
min: 0,
max: 100,
notInRangeMessage: "Points must be between {{ min }} and {{ max }}."
)]
private int $points = 0;
#[ORM\OneToMany(mappedBy: 'card', targetEntity: CardTransaction::class)]
private Collection $cardTransactions;
#[ORM\Column(length: 255, nullable: true)]
private ?string $appleWalletToken = null;
#[ORM\OneToMany(mappedBy: 'card', targetEntity: CardTransaction::class)]
private $transactions;
#[ORM\Column(length: 255, nullable: true)]
private ?string $appleWalletPushToken = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $deviceLibraryIdentifier = null;
#[ORM\Column(type: 'datetime')]
private ?\DateTimeInterface $updatedAt = null;
#[ORM\Column(type: 'string', length: 255, nullable: true)]
private ?string $lastNotification = null;
public function __construct()
{
$this->cardTransactions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(User $user): static
{
$this->user = $user;
return $this;
}
public function getQrCodeImage(): ?string
{
return $this->qrCodeImage;
}
public function setQrCodeImage(string $qrCodeImage): static
{
$this->qrCodeImage = $qrCodeImage;
return $this;
}
public function getPoints(): int
{
return $this->points;
}
public function setPoints(int $points): static
{
$this->points = $points;
return $this;
}
/**
* @return Collection<int, CardTransaction>
*/
public function getCardTransactions(): Collection
{
return $this->cardTransactions;
}
public function addCardTransaction(CardTransaction $cardTransaction): static
{
if (!$this->cardTransactions->contains($cardTransaction)) {
$this->cardTransactions->add($cardTransaction);
$cardTransaction->setCardId($this);
}
return $this;
}
public function removeCardTransaction(CardTransaction $cardTransaction): static
{
if ($this->cardTransactions->removeElement($cardTransaction)) {
// set the owning side to null (unless already changed)
if ($cardTransaction->getCardId() === $this) {
$cardTransaction->setCardId(null);
}
}
return $this;
}
public function getAppleWalletToken(): ?string
{
return $this->appleWalletToken;
}
public function setAppleWalletToken(?string $appleWalletToken): static
{
$this->appleWalletToken = $appleWalletToken;
return $this;
}
public function generateAppleWalletToken(): static
{
$this->appleWalletToken = bin2hex(random_bytes(32));
return $this;
}
public function getLastTransactionDate(): ?\DateTimeInterface
{
$lastTransaction = $this->transactions->last();
return $lastTransaction ? $lastTransaction->getTransactionDate() : null;
}
public function getAppleWalletPushToken(): ?string
{
return $this->appleWalletPushToken;
}
public function setAppleWalletPushToken(?string $appleWalletPushToken): self
{
$this->appleWalletPushToken = $appleWalletPushToken;
return $this;
}
public function getDeviceLibraryIdentifier(): ?string
{
return $this->deviceLibraryIdentifier;
}
public function setDeviceLibraryIdentifier(?string $deviceLibraryIdentifier): void
{
$this->deviceLibraryIdentifier = $deviceLibraryIdentifier;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): void
{
$this->updatedAt = $updatedAt;
}
public function getLastNotification(): ?string
{
return $this->lastNotification;
}
public function setLastNotification(?string $lastNotification): void
{
$this->lastNotification = $lastNotification;
}
// Dans Card.php
public function getLastTransaction(): ?CardTransaction
{
if ($this->cardTransactions->isEmpty()) {
return null;
}
$transactions = $this->cardTransactions->toArray();
usort($transactions, function (CardTransaction $a, CardTransaction $b) {
return $b->getTransactionDate() <=> $a->getTransactionDate();
});
return $transactions[0];
}
}