vendor/symfony/security-core/Authentication/Token/Storage/UsageTrackingTokenStorage.php line 43

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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 Symfony\Component\Security\Core\Authentication\Token\Storage;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  15. /**
  16.  * A token storage that increments the session usage index when the token is accessed.
  17.  *
  18.  * @author Nicolas Grekas <p@tchwork.com>
  19.  */
  20. final class UsageTrackingTokenStorage implements TokenStorageInterfaceServiceSubscriberInterface
  21. {
  22.     private $storage;
  23.     private $sessionLocator;
  24.     private $enableUsageTracking false;
  25.     public function __construct(TokenStorageInterface $storageContainerInterface $sessionLocator)
  26.     {
  27.         $this->storage $storage;
  28.         $this->sessionLocator $sessionLocator;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function getToken(): ?TokenInterface
  34.     {
  35.         if ($this->enableUsageTracking) {
  36.             // increments the internal session usage index
  37.             $this->sessionLocator->get('session')->getMetadataBag();
  38.         }
  39.         return $this->storage->getToken();
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function setToken(TokenInterface $token null): void
  45.     {
  46.         $this->storage->setToken($token);
  47.         if ($token && $this->enableUsageTracking) {
  48.             // increments the internal session usage index
  49.             $this->sessionLocator->get('session')->getMetadataBag();
  50.         }
  51.     }
  52.     public function enableUsageTracking(): void
  53.     {
  54.         $this->enableUsageTracking true;
  55.     }
  56.     public function disableUsageTracking(): void
  57.     {
  58.         $this->enableUsageTracking false;
  59.     }
  60.     public static function getSubscribedServices(): array
  61.     {
  62.         return [
  63.             'session' => SessionInterface::class,
  64.         ];
  65.     }
  66. }