src/App/Entity/FirebaseToken.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Utility\DateTimeUtility;
  4. use App\Utility\GuidUtility;
  5. use DateTime;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Exception;
  8. /**
  9. * @ORM\Entity()
  10. *
  11. * @ORM\Table(
  12. * name="firebase_tokens"
  13. * )
  14. */
  15. class FirebaseToken
  16. {
  17. public const ORIGIN_ID_MOBILE_APP_MAIN = 0;
  18. /**
  19. * @throws Exception
  20. */
  21. public function __construct(string $currentToken, int $originId, ?User $user = null, ?string $apiKeyId = null)
  22. {
  23. $this->currentToken = $currentToken;
  24. $this->originId = $originId;
  25. $this->user = $user;
  26. $this->apiKeyId = $apiKeyId;
  27. $this->previousTokens = [];
  28. $this->createdAt = DateTimeUtility::createDateTimeUtc();
  29. $this->updatedAt = null;
  30. $this->refreshedAt = null;
  31. }
  32. public function refreshToken(string $newCurrentToken, ?string $apiKeyId = null): void
  33. {
  34. $this->previousTokens[] = $this->currentToken;
  35. $this->currentToken = $newCurrentToken;
  36. $this->apiKeyId = $apiKeyId;
  37. $this->refreshedAt = DateTimeUtility::createDateTimeUtc();
  38. }
  39. /**
  40. * @ORM\GeneratedValue(strategy="CUSTOM")
  41. *
  42. * @ORM\CustomIdGenerator(class="App\Utility\DatabaseIdGenerator")
  43. *
  44. * @ORM\Column(name="id", type="guid")
  45. *
  46. * @ORM\Id
  47. */
  48. private string $id;
  49. public function setId(string $id)
  50. {
  51. GuidUtility::validOrThrow($id);
  52. $this->id = $id;
  53. }
  54. public function getId(): string
  55. {
  56. return $this->id;
  57. }
  58. /**
  59. * @ORM\Column(name="current_token", type="text", unique=true)
  60. */
  61. private string $currentToken;
  62. public function getCurrentToken(): string
  63. {
  64. return $this->currentToken;
  65. }
  66. /**
  67. * @ORM\Column(name="previous_tokens", type="array", nullable=false)
  68. */
  69. private array $previousTokens;
  70. /**
  71. * @return array|int[]
  72. */
  73. public function getPreviousTokens(): array
  74. {
  75. if (!is_array($this->previousTokens)) {
  76. return [];
  77. }
  78. return array_values($this->previousTokens);
  79. }
  80. /**
  81. * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="firebaseTokens", cascade={"persist"})
  82. *
  83. * @ORM\JoinColumn(name="users_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
  84. */
  85. private User $user;
  86. public function setUser(?User $user): void
  87. {
  88. $this->user = $user;
  89. }
  90. public function getUser(): ?User
  91. {
  92. return $this->user;
  93. }
  94. /**
  95. * @ORM\Column(name="api_key_id", type="text", nullable=true)
  96. */
  97. private string $apiKeyId;
  98. public function setApiKeyId(?string $apiKeyId): void
  99. {
  100. $this->apiKeyId = $apiKeyId;
  101. }
  102. /**
  103. * @ORM\Column(name="origin_id", type="smallint", nullable=false)
  104. */
  105. private int $originId;
  106. /**
  107. * @ORM\Column(name="created_at", type="datetime", nullable=false)
  108. */
  109. private DateTime $createdAt;
  110. public function setCreatedAt(DateTime $createdAt): void
  111. {
  112. $this->createdAt = $createdAt;
  113. }
  114. public function getCreatedAt(): DateTime
  115. {
  116. return $this->createdAt;
  117. }
  118. /**
  119. * @ORM\Column(name="updated_at", type="datetime", nullable=true)
  120. */
  121. private ?DateTime $updatedAt;
  122. public function setUpdatedAt(DateTime $updatedAt): void
  123. {
  124. $this->updatedAt = $updatedAt;
  125. }
  126. public function getUpdatedAt(): ?DateTime
  127. {
  128. return $this->updatedAt;
  129. }
  130. /**
  131. * @ORM\Column(name="refreshed_at", type="datetime", nullable=true)
  132. */
  133. private ?DateTime $refreshedAt;
  134. }