src/Entity/RefreshToken.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\RefreshTokenRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. /**
  6.  * @ORM\Entity(repositoryClass=RefreshTokenRepository::class)
  7.  */
  8. class RefreshToken
  9. {
  10.     const TOKEN_EXPIRATION_MINUTES 60 24 30// 30 days
  11.     /**
  12.      * @ORM\Id
  13.      * @ORM\GeneratedValue
  14.      * @ORM\Column(type="integer")
  15.      */
  16.     private $id;
  17.     /**
  18.      * @ORM\Column(type="datetime")
  19.      */
  20.     private $createdAt;
  21.     /**
  22.      * @ORM\Column(type="boolean", nullable=true)
  23.      */
  24.     private $revoked;
  25.     /**
  26.      * @ORM\Column(type="string", length=255, unique=true)
  27.      */
  28.     private $token;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity=User::class)
  31.      * @ORM\JoinColumn(nullable=false)
  32.      */
  33.     private $user;
  34.     public function __construct()
  35.     {
  36.         $this->createdAt = new \DateTime();
  37.     }
  38.     public function getId(): ?int
  39.     {
  40.         return $this->id;
  41.     }
  42.     public function getCreatedAt(): ?\DateTimeInterface
  43.     {
  44.         return $this->createdAt;
  45.     }
  46.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  47.     {
  48.         $this->createdAt $createdAt;
  49.         return $this;
  50.     }
  51.     public function isRevoked(): ?bool
  52.     {
  53.         return $this->revoked;
  54.     }
  55.     public function setRevoked(?bool $revoked): self
  56.     {
  57.         $this->revoked $revoked;
  58.         return $this;
  59.     }
  60.     public function getExpiresAt(): ?\DateTimeInterface
  61.     {
  62.         if ($this->createdAt) {
  63.             $expiresAt = clone $this->createdAt;
  64.             $expiresAt->modify('+' self::TOKEN_EXPIRATION_MINUTES ' minutes');
  65.             return $expiresAt;
  66.         }
  67.         return null;
  68.     }
  69.     public function isExpired(): bool
  70.     {
  71.         $expiresAt $this->getExpiresAt();
  72.         if ($expiresAt) {
  73.             return new \DateTime() > $expiresAt;
  74.         }
  75.         return false;
  76.     }
  77.     public function isAlive(): bool
  78.     {
  79.         return !$this->isExpired() && !$this->isRevoked();
  80.     }
  81.     public function getToken(): ?string
  82.     {
  83.         return $this->token;
  84.     }
  85.     public function setToken(string $token): self
  86.     {
  87.         $this->token $token;
  88.         return $this;
  89.     }
  90.     public function getUser(): ?User
  91.     {
  92.         return $this->user;
  93.     }
  94.     public function setUser(?User $user): self
  95.     {
  96.         $this->user $user;
  97.         return $this;
  98.     }
  99. }