vendor/symfonycasts/reset-password-bundle/src/Model/ResetPasswordToken.php line 16

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the SymfonyCasts ResetPasswordBundle package.
  4.  * Copyright (c) SymfonyCasts <https://symfonycasts.com/>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace SymfonyCasts\Bundle\ResetPassword\Model;
  9. /**
  10.  * @author Jesse Rushlow <jr@rushlow.dev>
  11.  * @author Ryan Weaver   <ryan@symfonycasts.com>
  12.  */
  13. final class ResetPasswordToken
  14. {
  15.     /**
  16.      * @var string|null selector + non-hashed verifier token
  17.      */
  18.     private $token;
  19.     /**
  20.      * @var \DateTimeInterface
  21.      */
  22.     private $expiresAt;
  23.     /**
  24.      * @var int|null timestamp when the token was created
  25.      */
  26.     private $generatedAt;
  27.     /**
  28.      * @var int expiresAt translator interval
  29.      */
  30.     private $transInterval 0;
  31.     public function __construct(string $token\DateTimeInterface $expiresAtint $generatedAt null)
  32.     {
  33.         $this->token $token;
  34.         $this->expiresAt $expiresAt;
  35.         $this->generatedAt $generatedAt;
  36.         if (null === $generatedAt) {
  37.             $this->triggerDeprecation();
  38.         }
  39.     }
  40.     /**
  41.      * Returns the full token the user should use.
  42.      *
  43.      * Internally, this consists of two parts - the selector and
  44.      * the hashed token - but that's an implementation detail
  45.      * of how the token will later be parsed.
  46.      */
  47.     public function getToken(): string
  48.     {
  49.         if (null === $this->token) {
  50.             throw new \RuntimeException('The token property is not set. Calling getToken() after calling clearToken() is not allowed.');
  51.         }
  52.         return $this->token;
  53.     }
  54.     /**
  55.      * Allow the token object to be safely persisted in a session.
  56.      */
  57.     public function clearToken(): void
  58.     {
  59.         $this->token null;
  60.     }
  61.     public function getExpiresAt(): \DateTimeInterface
  62.     {
  63.         return $this->expiresAt;
  64.     }
  65.     /**
  66.      * Get the translation message for when a token expires.
  67.      *
  68.      * This is used in conjunction with getExpirationMessageData() method.
  69.      * Example usage in a Twig template:
  70.      *
  71.      * <p>{{ components.expirationMessageKey|trans(components.expirationMessageData) }}</p>
  72.      *
  73.      * symfony/translation is required to translate into a non-English locale.
  74.      *
  75.      * @throws \LogicException
  76.      */
  77.     public function getExpirationMessageKey(): string
  78.     {
  79.         $interval $this->getExpiresAtIntervalInstance();
  80.         switch ($interval) {
  81.             case $interval->0:
  82.                 $this->transInterval $interval->y;
  83.                 return '%count% year|%count% years';
  84.             case $interval->0:
  85.                 $this->transInterval $interval->m;
  86.                 return '%count% month|%count% months';
  87.             case $interval->0:
  88.                 $this->transInterval $interval->d;
  89.                 return '%count% day|%count% days';
  90.             case $interval->0:
  91.                 $this->transInterval $interval->h;
  92.                 return '%count% hour|%count% hours';
  93.             default:
  94.                 $this->transInterval $interval->i;
  95.                 return '%count% minute|%count% minutes';
  96.         }
  97.     }
  98.     /**
  99.      * @throws \LogicException
  100.      */
  101.     public function getExpirationMessageData(): array
  102.     {
  103.         $this->getExpirationMessageKey();
  104.         return ['%count%' => $this->transInterval];
  105.     }
  106.     /**
  107.      * Get the interval that the token is valid for.
  108.      *
  109.      * @throws \LogicException
  110.      *
  111.      * @psalm-suppress PossiblyFalseArgument
  112.      */
  113.     public function getExpiresAtIntervalInstance(): \DateInterval
  114.     {
  115.         if (null === $this->generatedAt) {
  116.             throw new \LogicException(sprintf('%s initialized without setting the $generatedAt timestamp.'self::class));
  117.         }
  118.         $createdAtTime \DateTimeImmutable::createFromFormat('U', (string) $this->generatedAt);
  119.         return $this->expiresAt->diff($createdAtTime);
  120.     }
  121.     private function triggerDeprecation(): void
  122.     {
  123.         trigger_deprecation(
  124.             'symfonycasts/reset-password-bundle',
  125.             '1.2',
  126.             'Initializing the %s without setting the "$generatedAt" constructor argument is deprecated. The default "null" will be removed in the future.',
  127.             self::class
  128.         );
  129.     }
  130. }