src/UserBundle/Entity/Invitation.php line 17

Open in your IDE?
  1. <?php
  2. namespace UserBundle\Entity;
  3. use CoreBundle\Entity\Institution;
  4. use CoreBundle\Entity\Publication;
  5. use CoreBundle\Entity\Publisher;
  6. use DateTime;
  7. use DateTimeZone;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Exception;
  10. /**
  11. * @ORM\Table("invitation")
  12. * @ORM\Entity
  13. */
  14. class Invitation
  15. {
  16. const EXPIRES_AFTER = '24 hours';
  17. /**
  18. * @ORM\Column(name="id", type="integer")
  19. * @ORM\Id
  20. * @ORM\GeneratedValue(strategy="AUTO")
  21. */
  22. private int $id;
  23. /**
  24. * @ORM\Column(name="token", type="string", length=64)
  25. */
  26. private string $token;
  27. /**
  28. * @ORM\Column(name="email", type="string", length=255)
  29. */
  30. private string $email;
  31. /**
  32. * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
  33. * @ORM\JoinColumn(name="issued_by", referencedColumnName="id", onDelete="SET NULL", nullable=true)
  34. */
  35. private ?User $issuedBy;
  36. /**
  37. * @ORM\Column(name="created", type="datetime")
  38. */
  39. private DateTime $created;
  40. /**
  41. * @ORM\Column(name="active", type="boolean")
  42. */
  43. private bool $active;
  44. /**
  45. * @ORM\Column(name="role", type="string", length=32)
  46. */
  47. private string $role;
  48. /**
  49. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Institution")
  50. * @ORM\JoinColumn(name="institution", referencedColumnName="id", onDelete="CASCADE", nullable=false)
  51. */
  52. private Institution $institution;
  53. public function getId(): ?int
  54. {
  55. return $this->id ?? null; // To avoid property access error in EasyAdmin
  56. }
  57. public function setId(int $id): void
  58. {
  59. $this->id = $id;
  60. }
  61. public function getToken(): string
  62. {
  63. return $this->token;
  64. }
  65. public function setToken(string $token): void
  66. {
  67. $this->token = $token;
  68. }
  69. public function getEmail(): string
  70. {
  71. return $this->email;
  72. }
  73. public function setEmail(string $email): void
  74. {
  75. $this->email = $email;
  76. }
  77. public function getIssuedBy(): ?User
  78. {
  79. return $this->issuedBy;
  80. }
  81. public function setIssuedBy(?User $issuedBy): void
  82. {
  83. $this->issuedBy = $issuedBy;
  84. }
  85. public function getCreated(): DateTime
  86. {
  87. return $this->created;
  88. }
  89. public function setCreated(DateTime $created): void
  90. {
  91. $this->created = $created;
  92. }
  93. public function isActive(): bool
  94. {
  95. return $this->active;
  96. }
  97. public function setActive(bool $active): void
  98. {
  99. $this->active = $active;
  100. }
  101. public function getRole(): string
  102. {
  103. return $this->role;
  104. }
  105. public function setRole(string $role): void
  106. {
  107. $this->role = $role;
  108. }
  109. public function getInstitution(): Institution
  110. {
  111. return $this->institution;
  112. }
  113. public function setInstitution(Institution $institution): void
  114. {
  115. $this->institution = $institution;
  116. }
  117. /**
  118. * @throws Exception
  119. */
  120. public function isValid(): bool
  121. {
  122. if (!$this->isActive()) {
  123. return false;
  124. }
  125. $latestCreationTime = new DateTime('-' . self::EXPIRES_AFTER, new DateTimeZone('Europe/Copenhagen'));
  126. if ($this->getCreated() < $latestCreationTime) {
  127. return false;
  128. }
  129. if ($this->getIssuedBy() === null) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. /**
  135. * @throws Exception
  136. */
  137. public function generateToken(): void
  138. {
  139. $this->token = bin2hex(random_bytes(20));
  140. }
  141. }