<?phpnamespace App\Entity;use App\Repository\ClientTokenRepository;use Doctrine\ORM\Mapping as ORM;use JMS\Serializer\Annotation as Serializer;/** * @ORM\Entity(repositoryClass=ClientTokenRepository::class) */class ClientToken{ //public const VALIDITY = '+24 hour'; public const VALIDITY = '+365 days'; /** * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ private $id; /** * @Serializer\Groups({"api"}) * @Serializer\SerializedName("access_token") * @ORM\Column(type="string", length=255) */ private $token; /** * @Serializer\Groups({"api"}) * @Serializer\SerializedName("expires_at") * @ORM\Column(type="datetime") */ private $expiresAt; /** * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="clientTokens") * @ORM\JoinColumn(nullable=false) */ private $client; /** * @Serializer\Groups({"api"}) * @ORM\Column(type="string", length=255) */ private $refreshToken; /** * ClientToken constructor. * * @param Client $client */ public function __construct(Client $client) { $this->update(); $this->client = $client; } public function update( ) { try { $this->token = bin2hex( random_bytes( 60 ) ); $this->refreshToken = bin2hex( random_bytes( 60 ) ); } catch ( \Exception $e ) { } $this->expiresAt = new \DateTime(self::VALIDITY); return $this; } public function isExpired(): bool { return $this->getExpiresAt() <= new \DateTime(); } public function getId(): ?int { return $this->id; } public function getToken(): ?string { return $this->token; } public function setToken(string $token): self { $this->token = $token; return $this; } public function getExpiresAt(): ?\DateTimeInterface { return $this->expiresAt; } public function setExpiresAt(\DateTimeInterface $expiresAt): self { $this->expiresAt = $expiresAt; return $this; } public function getClient(): ?Client { return $this->client; } public function setClient(?Client $client): self { $this->client = $client; return $this; } public function getRefreshToken(): ?string { return $this->refreshToken; } public function setRefreshToken(string $refreshToken): self { $this->refreshToken = $refreshToken; return $this; }}