vendor/gesdinet/jwt-refresh-token-bundle/Model/AbstractRefreshToken.php line 82

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the GesdinetJWTRefreshTokenBundle package.
  4.  *
  5.  * (c) Gesdinet <http://www.gesdinet.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Gesdinet\JWTRefreshTokenBundle\Model;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. abstract class AbstractRefreshToken implements RefreshTokenInterface
  13. {
  14.     /**
  15.      * @var int|string|null
  16.      */
  17.     protected $id;
  18.     /**
  19.      * @var string|null
  20.      */
  21.     protected $refreshToken;
  22.     /**
  23.      * @var string|null
  24.      */
  25.     protected $username;
  26.     /**
  27.      * @var \DateTimeInterface|null
  28.      */
  29.     protected $valid;
  30.     /**
  31.      * Creates a new model instance based on the provided details.
  32.      */
  33.     public static function createForUserWithTtl(string $refreshTokenUserInterface $userint $ttl): RefreshTokenInterface
  34.     {
  35.         $valid = new \DateTime();
  36.         // Explicitly check for a negative number based on a behavior change in PHP 8.2, see https://github.com/php/php-src/issues/9950
  37.         if ($ttl 0) {
  38.             $valid->modify('+'.$ttl.' seconds');
  39.         } elseif ($ttl 0) {
  40.             $valid->modify($ttl.' seconds');
  41.         }
  42.         $model = new static();
  43.         $model->setRefreshToken($refreshToken);
  44.         $model->setUsername(method_exists($user'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername());
  45.         $model->setValid($valid);
  46.         return $model;
  47.     }
  48.     /**
  49.      * @return string Refresh Token
  50.      */
  51.     public function __toString()
  52.     {
  53.         return $this->getRefreshToken() ?: '';
  54.     }
  55.     /**
  56.      * {@inheritdoc}
  57.      */
  58.     public function getId()
  59.     {
  60.         return $this->id;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function setRefreshToken($refreshToken null)
  66.     {
  67.         if (null === $refreshToken || '' === $refreshToken) {
  68.             trigger_deprecation('gesdinet/jwt-refresh-token-bundle''1.0''Passing an empty token to %s() to automatically generate a token is deprecated.'__METHOD__);
  69.             $refreshToken bin2hex(random_bytes(64));
  70.         }
  71.         $this->refreshToken $refreshToken;
  72.         return $this;
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function getRefreshToken()
  78.     {
  79.         return $this->refreshToken;
  80.     }
  81.     /**
  82.      * {@inheritdoc}
  83.      */
  84.     public function setValid($valid)
  85.     {
  86.         $this->valid $valid;
  87.         return $this;
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function getValid()
  93.     {
  94.         return $this->valid;
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function setUsername($username)
  100.     {
  101.         $this->username $username;
  102.         return $this;
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     public function getUsername()
  108.     {
  109.         return $this->username;
  110.     }
  111.     /**
  112.      * {@inheritdoc}
  113.      */
  114.     public function isValid()
  115.     {
  116.         return null !== $this->valid && $this->valid >= new \DateTime();
  117.     }
  118. }