Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * @ORM\Entity(repositoryClass="App\Repository\ApiTokenRepository")
  9.  */
  10. class ApiToken
  11. {
  12.     /**
  13.      * @ORM\Id()
  14.      * @ORM\GeneratedValue()
  15.      * @ORM\Column(type="integer")
  16.      */
  17.     private $id;
  18.  
  19.     /**
  20.      * @ORM\Column(type="string", length=255)
  21.      */
  22.     private $token;
  23.  
  24.     /**
  25.      * @ORM\Column(type="datetime")
  26.      */
  27.     private $expiresAt;
  28.  
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="apiTokens")
  31.      * @ORM\JoinColumn(nullable=false)
  32.      */
  33.     private $user;
  34.  
  35.     /**
  36.      * ApiToken constructor.
  37.      * @param User $user
  38.      * @throws \Exception
  39.      */
  40.     public function __construct(User $user)
  41.     {
  42.         $this->token = bin2hex(random_bytes(60));
  43.         $this->user = $user;
  44.         $this->expiresAt = new \DateTime('+ 1 hour');
  45.     }
  46.  
  47.     public function getId(): ?int
  48.     {
  49.         return $this->id;
  50.     }
  51.  
  52.     public function getToken(): ?string
  53.     {
  54.         return $this->token;
  55.     }
  56.  
  57.     public function getExpiresAt(): ?\DateTimeInterface
  58.     {
  59.         return $this->expiresAt;
  60.     }
  61.  
  62.  
  63.     public function getUser(): ?User
  64.     {
  65.         return $this->user;
  66.     }
  67.  
  68.     public function isExpired(): bool
  69.     {
  70.         return $this->getExpiresAt() <= new \DateTime();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement