src/Entity/Abonnent/Abonnenttoken.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Abonnent;
  3. use Doctrine\ORM\Mapping as ORM;
  4. #[ORM\Entity]
  5. #[ORM\Table(name: 'abonnenttoken')]
  6. class Abonnenttoken
  7. {
  8. #[ORM\Id]
  9. #[ORM\GeneratedValue(strategy: 'NONE')]
  10. #[ORM\Column(name: 'token', type: 'string', length: 128, nullable: false)]
  11. private string $token;
  12. #[ORM\Column(name: 'create_at', type: 'datetime', nullable: false)]
  13. private \DateTimeInterface $createAt;
  14. /**
  15. * @throws \Exception
  16. */
  17. public function __construct(
  18. #[ORM\ManyToOne(targetEntity: Abonnent::class, inversedBy:'tokens')]
  19. private readonly Abonnent $abonnent,
  20. #[ORM\Column(name: 'expire_at', type: 'datetime', nullable: false)]
  21. private readonly \DateTimeInterface $expireAt
  22. )
  23. {
  24. // Token bisher
  25. // $this->token = sha1($this->abonnent->getUsername().':'.$this->abonnent->getId().':'.date('d-m-Y-H:i:s'));
  26. // Token neu - es kam sporadisch zu einem Problem, wo zweimal versucht wurde dasselbe Token zu setzen
  27. // [2024-10-30T12:57:00.551733+01:00] [109.115.154.98] [www.urnerwochenblatt.ch] [/abo/login] [???] [request.CRITICAL] [Uncaught PHP Exception Doctrine\DBAL\Exception\UniqueConstraintViolationException: "An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '94b4b973877a2eec35601c5def1d10cf3d394925' for key 'abonnenttoken.PRIMARY'" at /home/www-data/www.urnerwochenblatt.ch/releases/20241023072727/vendor/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php line 62]
  28. $this->token = bin2hex(random_bytes(20));
  29. $this->createAt = new \DateTime('now');
  30. }
  31. public function getToken(): string
  32. {
  33. return $this->token;
  34. }
  35. public function getAbonnent(): Abonnent
  36. {
  37. return $this->abonnent;
  38. }
  39. public function getCreateAt(): \DateTimeInterface
  40. {
  41. return $this->createAt;
  42. }
  43. public function getExpireAt(): \DateTimeInterface
  44. {
  45. return $this->expireAt;
  46. }
  47. public function hasExpired(): bool
  48. {
  49. $now = new \DateTime();
  50. return $this->expireAt < $now;
  51. }
  52. }