src/UserBundle/Entity/ApiToken.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <[email protected]>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\UserBundle\Entity;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\DBAL\Types\Types;
  15. use Doctrine\ORM\Mapping as ORM;
  16. use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
  17. use Ramsey\Uuid\Doctrine\UuidOrderedTimeGenerator;
  18. use Ramsey\Uuid\UuidInterface;
  19. use SolidInvoice\CoreBundle\Traits\Entity\CompanyAware;
  20. use SolidInvoice\CoreBundle\Traits\Entity\TimeStampable;
  21. use SolidInvoice\UserBundle\Repository\ApiTokenRepository;
  22. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  23. use Symfony\Component\Security\Core\User\UserInterface;
  24. use Symfony\Component\Validator\Constraints as Assert;
  25. #[ORM\Table(ApiToken::TABLE_NAME)]
  26. #[ORM\Entity(repositoryClassApiTokenRepository::class)]
  27. #[UniqueEntity(['name''user'])]
  28. class ApiToken
  29. {
  30.     final public const TABLE_NAME 'api_tokens';
  31.     use TimeStampable;
  32.     use CompanyAware;
  33.     #[ORM\Column(typeUuidBinaryOrderedTimeType::NAME)]
  34.     #[ORM\Id]
  35.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  36.     #[ORM\CustomIdGenerator(class: UuidOrderedTimeGenerator::class)]
  37.     private ?UuidInterface $id null;
  38.     #[ORM\Column(typeTypes::STRINGlength125)]
  39.     #[Assert\NotBlank]
  40.     private ?string $name null;
  41.     #[ORM\Column(typeTypes::STRINGlength125)]
  42.     private ?string $token null;
  43.     /**
  44.      * @var Collection<int, ApiTokenHistory>
  45.      */
  46.     #[ORM\OneToMany(mappedBy'token'targetEntityApiTokenHistory::class, cascade: ['persist''remove'], fetch'EXTRA_LAZY'orphanRemovaltrue)]
  47.     #[ORM\OrderBy(['created' => 'DESC'])]
  48.     private Collection $history;
  49.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'apiTokens')]
  50.     #[ORM\JoinColumn(name'user_id')]
  51.     private ?UserInterface $user null;
  52.     public function __construct()
  53.     {
  54.         $this->history = new ArrayCollection();
  55.     }
  56.     public function getId(): UuidInterface
  57.     {
  58.         return $this->id;
  59.     }
  60.     public function getName(): ?string
  61.     {
  62.         return $this->name;
  63.     }
  64.     public function setName(string $name): self
  65.     {
  66.         $this->name $name;
  67.         return $this;
  68.     }
  69.     public function getToken(): ?string
  70.     {
  71.         return $this->token;
  72.     }
  73.     public function setToken(string $token): self
  74.     {
  75.         $this->token $token;
  76.         return $this;
  77.     }
  78.     /**
  79.      * @return Collection<int, ApiTokenHistory>
  80.      */
  81.     public function getHistory(): Collection
  82.     {
  83.         return $this->history;
  84.     }
  85.     public function addHistory(ApiTokenHistory $history): self
  86.     {
  87.         $this->history[] = $history;
  88.         $history->setToken($this)
  89.             ->setCompany($this->getCompany());
  90.         return $this;
  91.     }
  92.     public function removeHistory(ApiTokenHistory $history): self
  93.     {
  94.         $this->history->removeElement($history);
  95.         return $this;
  96.     }
  97.     public function getUser(): ?UserInterface
  98.     {
  99.         return $this->user;
  100.     }
  101.     public function setUser(UserInterface $user): self
  102.     {
  103.         $this->user $user;
  104.         return $this;
  105.     }
  106. }