src/Entity/Card.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CardRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints\Range;
  9. #[ORM\Entity(repositoryClassCardRepository::class)]
  10. class Card
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id null;
  16.     #[ORM\OneToOne(inversedBy'card'cascade: ['persist''remove'])]
  17.     #[ORM\JoinColumn(nullablefalse)]
  18.     private ?User $user null;
  19.     #[ORM\Column(length255)]
  20.     #[Assert\File(mimeTypes: ["image/png""image/jpeg"])]
  21.     private ?string $qrCodeImage null;
  22.     #[ORM\Column(nullablefalse)]
  23.     #[Range(
  24.         min0,
  25.         max100,
  26.         notInRangeMessage"Points must be between {{ min }} and {{ max }}."
  27.     )]
  28.     private int $points 0;
  29.     #[ORM\OneToMany(mappedBy'card'targetEntityCardTransaction::class)]
  30.     private Collection $cardTransactions;
  31.     #[ORM\Column(length255nullabletrue)]
  32.     private ?string $appleWalletToken null;
  33.     #[ORM\OneToMany(mappedBy'card'targetEntityCardTransaction::class)]
  34.     private $transactions;
  35.     #[ORM\Column(length255nullabletrue)]
  36.     private ?string $appleWalletPushToken null;
  37.     #[ORM\Column(length255nullabletrue)]
  38.     private ?string $deviceLibraryIdentifier null;
  39.     #[ORM\Column(type'datetime')]
  40.     private ?\DateTimeInterface $updatedAt null;
  41.     #[ORM\Column(type'string'length255nullabletrue)]
  42.     private ?string $lastNotification null;
  43.     public function __construct()
  44.     {
  45.         $this->cardTransactions = new ArrayCollection();
  46.     }
  47.     public function getId(): ?int
  48.     {
  49.         return $this->id;
  50.     }
  51.     public function getUser(): ?User
  52.     {
  53.         return $this->user;
  54.     }
  55.     public function setUser(User $user): static
  56.     {
  57.         $this->user $user;
  58.         return $this;
  59.     }
  60.     public function getQrCodeImage(): ?string
  61.     {
  62.         return $this->qrCodeImage;
  63.     }
  64.     public function setQrCodeImage(string $qrCodeImage): static
  65.     {
  66.         $this->qrCodeImage $qrCodeImage;
  67.         return $this;
  68.     }
  69.     public function getPoints(): int
  70.     {
  71.         return $this->points;
  72.     }
  73.     public function setPoints(int $points): static
  74.     {
  75.         $this->points $points;
  76.         return $this;
  77.     }
  78.     /**
  79.      * @return Collection<int, CardTransaction>
  80.      */
  81.     public function getCardTransactions(): Collection
  82.     {
  83.         return $this->cardTransactions;
  84.     }
  85.     public function addCardTransaction(CardTransaction $cardTransaction): static
  86.     {
  87.         if (!$this->cardTransactions->contains($cardTransaction)) {
  88.             $this->cardTransactions->add($cardTransaction);
  89.             $cardTransaction->setCardId($this);
  90.         }
  91.         return $this;
  92.     }
  93.     public function removeCardTransaction(CardTransaction $cardTransaction): static
  94.     {
  95.         if ($this->cardTransactions->removeElement($cardTransaction)) {
  96.             // set the owning side to null (unless already changed)
  97.             if ($cardTransaction->getCardId() === $this) {
  98.                 $cardTransaction->setCardId(null);
  99.             }
  100.         }
  101.         return $this;
  102.     }
  103.     public function getAppleWalletToken(): ?string
  104.     {
  105.         return $this->appleWalletToken;
  106.     }
  107.     public function setAppleWalletToken(?string $appleWalletToken): static
  108.     {
  109.         $this->appleWalletToken $appleWalletToken;
  110.         return $this;
  111.     }
  112.     public function generateAppleWalletToken(): static
  113.     {
  114.         $this->appleWalletToken bin2hex(random_bytes(32));
  115.         return $this;
  116.     }
  117.     public function getLastTransactionDate(): ?\DateTimeInterface
  118.     {
  119.         $lastTransaction $this->transactions->last();
  120.         return $lastTransaction $lastTransaction->getTransactionDate() : null;
  121.     }
  122.     public function getAppleWalletPushToken(): ?string
  123.     {
  124.         return $this->appleWalletPushToken;
  125.     }
  126.     public function setAppleWalletPushToken(?string $appleWalletPushToken): self
  127.     {
  128.         $this->appleWalletPushToken $appleWalletPushToken;
  129.         return $this;
  130.     }
  131.     public function getDeviceLibraryIdentifier(): ?string
  132.     {
  133.         return $this->deviceLibraryIdentifier;
  134.     }
  135.     public function setDeviceLibraryIdentifier(?string $deviceLibraryIdentifier): void
  136.     {
  137.         $this->deviceLibraryIdentifier $deviceLibraryIdentifier;
  138.     }
  139.     public function getUpdatedAt(): ?\DateTimeInterface
  140.     {
  141.         return $this->updatedAt;
  142.     }
  143.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): void
  144.     {
  145.         $this->updatedAt $updatedAt;
  146.     }
  147.     public function getLastNotification(): ?string
  148.     {
  149.         return $this->lastNotification;
  150.     }
  151.     public function setLastNotification(?string $lastNotification): void
  152.     {
  153.         $this->lastNotification $lastNotification;
  154.     }
  155.     // Dans Card.php
  156.     public function getLastTransaction(): ?CardTransaction
  157.     {
  158.         if ($this->cardTransactions->isEmpty()) {
  159.             return null;
  160.         }
  161.         $transactions $this->cardTransactions->toArray();
  162.         usort($transactions, function (CardTransaction $aCardTransaction $b) {
  163.             return $b->getTransactionDate() <=> $a->getTransactionDate();
  164.         });
  165.         return $transactions[0];
  166.     }
  167. }