<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* ApiToken
*
* @ORM\Table(name="api_token")
* @ORM\Entity(repositoryClass="App\Repository\ApiTokenRepository")
*/
class ApiToken
{
/**
* @var int
*
* @ORM\Id
* @ORM\Column(name="id", type="integer", options={"unsigned":true})
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="token", type="string", length=255)
*/
private $token;
/**
* @var \DateTime
*
* @ORM\Column(name="expires_at", type="datetime")
*/
private $expiresAt;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Users", inversedBy="apiTokens")
* @ORM\JoinColumn(nullable=false)
* @ORM\JoinColumn(onDelete="CASCADE")
*/
private $user;
public function __construct(Users $user, \DateTime $expiresAt)
{
$this->token = bin2hex(random_bytes(60));
$this->user = $user;
$this->expiresAt = $expiresAt;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Get token
*
* @return string
*/
public function getToken()
{
return $this->token;
}
/**
* Get expiresAt
*
* @return \DateTime
*/
public function getExpiresAt()
{
return $this->expiresAt;
}
public function getUser(): ?Users
{
return $this->user;
}
public function isExpired(): bool
{
return $this->getExpiresAt() <= new \DateTime();
}
public function setUser(?Users $user): void {
$this->user = $user;
}
}