vendor/gesdinet/jwt-refresh-token-bundle/Doctrine/RefreshTokenManager.php line 80

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\Doctrine;
  11. use Doctrine\Persistence\ObjectManager;
  12. use Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGeneratorInterface;
  13. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenInterface;
  14. use Gesdinet\JWTRefreshTokenBundle\Model\RefreshTokenManagerInterface;
  15. class RefreshTokenManager implements RefreshTokenManagerInterface
  16. {
  17.     /**
  18.      * @var ObjectManager
  19.      */
  20.     protected $objectManager;
  21.     /**
  22.      * @var class-string<RefreshTokenInterface>
  23.      */
  24.     protected $class;
  25.     /**
  26.      * @var RefreshTokenRepositoryInterface<RefreshTokenInterface>
  27.      */
  28.     protected $repository;
  29.     /**
  30.      * @param class-string<RefreshTokenInterface> $class
  31.      *
  32.      * @throws \LogicException if the object repository does not implement `Gesdinet\JWTRefreshTokenBundle\Doctrine\RefreshTokenRepositoryInterface`
  33.      */
  34.     public function __construct(ObjectManager $om$class)
  35.     {
  36.         $this->objectManager $om;
  37.         $repository $om->getRepository($class);
  38.         if (!$repository instanceof RefreshTokenRepositoryInterface) {
  39.             throw new \LogicException(sprintf('Repository mapped for "%s" should implement %s.'$classRefreshTokenRepositoryInterface::class));
  40.         }
  41.         $this->repository $repository;
  42.         $metadata $om->getClassMetadata($class);
  43.         $this->class $metadata->getName();
  44.     }
  45.     /**
  46.      * Creates an empty RefreshTokenInterface instance.
  47.      *
  48.      * @return RefreshTokenInterface
  49.      *
  50.      * @deprecated to be removed in 2.0, use a `Gesdinet\JWTRefreshTokenBundle\Generator\RefreshTokenGeneratorInterface` instead.
  51.      */
  52.     public function create()
  53.     {
  54.         trigger_deprecation('gesdinet/jwt-refresh-token-bundle''1.0''%s() is deprecated and will be removed in 2.0, use a "%s" instance to create new %s objects.'__METHOD__RefreshTokenGeneratorInterface::class, RefreshTokenInterface::class);
  55.         $class $this->getClass();
  56.         return new $class();
  57.     }
  58.     /**
  59.      * @param string $refreshToken
  60.      *
  61.      * @return RefreshTokenInterface|null
  62.      */
  63.     public function get($refreshToken)
  64.     {
  65.         return $this->repository->findOneBy(['refreshToken' => $refreshToken]);
  66.     }
  67.     /**
  68.      * @param string $username
  69.      *
  70.      * @return RefreshTokenInterface|null
  71.      */
  72.     public function getLastFromUsername($username)
  73.     {
  74.         return $this->repository->findOneBy(['username' => $username], ['valid' => 'DESC']);
  75.     }
  76.     /**
  77.      * @param bool $andFlush
  78.      *
  79.      * @return void
  80.      */
  81.     public function save(RefreshTokenInterface $refreshToken$andFlush true)
  82.     {
  83.         $this->objectManager->persist($refreshToken);
  84.         if ($andFlush) {
  85.             $this->objectManager->flush();
  86.         }
  87.     }
  88.     /**
  89.      * @param bool $andFlush
  90.      *
  91.      * @return void
  92.      */
  93.     public function delete(RefreshTokenInterface $refreshToken$andFlush true)
  94.     {
  95.         $this->objectManager->remove($refreshToken);
  96.         if ($andFlush) {
  97.             $this->objectManager->flush();
  98.         }
  99.     }
  100.     /**
  101.      * @param \DateTimeInterface|null $datetime
  102.      * @param bool                    $andFlush
  103.      *
  104.      * @return RefreshTokenInterface[]
  105.      */
  106.     public function revokeAllInvalid($datetime null$andFlush true)
  107.     {
  108.         $invalidTokens $this->repository->findInvalid($datetime);
  109.         foreach ($invalidTokens as $invalidToken) {
  110.             $this->objectManager->remove($invalidToken);
  111.         }
  112.         if ($andFlush) {
  113.             $this->objectManager->flush();
  114.         }
  115.         return $invalidTokens;
  116.     }
  117.     /**
  118.      * Returns the RefreshToken fully qualified class name.
  119.      *
  120.      * @return class-string<RefreshTokenInterface>
  121.      */
  122.     public function getClass()
  123.     {
  124.         return $this->class;
  125.     }
  126. }