src/Entity/ClientToken.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ClientTokenRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use JMS\Serializer\Annotation as Serializer;
  6. /**
  7. * @ORM\Entity(repositoryClass=ClientTokenRepository::class)
  8. */
  9. class ClientToken
  10. {
  11. //public const VALIDITY = '+24 hour';
  12. public const VALIDITY = '+365 days';
  13. /**
  14. * @ORM\Id
  15. * @ORM\GeneratedValue
  16. * @ORM\Column(type="integer")
  17. */
  18. private $id;
  19. /**
  20. * @Serializer\Groups({"api"})
  21. * @Serializer\SerializedName("access_token")
  22. * @ORM\Column(type="string", length=255)
  23. */
  24. private $token;
  25. /**
  26. * @Serializer\Groups({"api"})
  27. * @Serializer\SerializedName("expires_at")
  28. * @ORM\Column(type="datetime")
  29. */
  30. private $expiresAt;
  31. /**
  32. * @ORM\ManyToOne(targetEntity=Client::class, inversedBy="clientTokens")
  33. * @ORM\JoinColumn(nullable=false)
  34. */
  35. private $client;
  36. /**
  37. * @Serializer\Groups({"api"})
  38. * @ORM\Column(type="string", length=255)
  39. */
  40. private $refreshToken;
  41. /**
  42. * ClientToken constructor.
  43. *
  44. * @param Client $client
  45. */
  46. public function __construct(Client $client)
  47. {
  48. $this->update();
  49. $this->client = $client;
  50. }
  51. public function update( )
  52. {
  53. try {
  54. $this->token = bin2hex( random_bytes( 60 ) );
  55. $this->refreshToken = bin2hex( random_bytes( 60 ) );
  56. } catch ( \Exception $e ) {
  57. }
  58. $this->expiresAt = new \DateTime(self::VALIDITY);
  59. return $this;
  60. }
  61. public function isExpired(): bool
  62. {
  63. return $this->getExpiresAt() <= new \DateTime();
  64. }
  65. public function getId(): ?int
  66. {
  67. return $this->id;
  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. public function getExpiresAt(): ?\DateTimeInterface
  79. {
  80. return $this->expiresAt;
  81. }
  82. public function setExpiresAt(\DateTimeInterface $expiresAt): self
  83. {
  84. $this->expiresAt = $expiresAt;
  85. return $this;
  86. }
  87. public function getClient(): ?Client
  88. {
  89. return $this->client;
  90. }
  91. public function setClient(?Client $client): self
  92. {
  93. $this->client = $client;
  94. return $this;
  95. }
  96. public function getRefreshToken(): ?string
  97. {
  98. return $this->refreshToken;
  99. }
  100. public function setRefreshToken(string $refreshToken): self
  101. {
  102. $this->refreshToken = $refreshToken;
  103. return $this;
  104. }
  105. }