ExpiredLoginLinkStorage.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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\Http\LoginLink;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. /**
  13. * @final
  14. */
  15. class ExpiredLoginLinkStorage
  16. {
  17. private $cache;
  18. private $lifetime;
  19. public function __construct(CacheItemPoolInterface $cache, int $lifetime)
  20. {
  21. $this->cache = $cache;
  22. $this->lifetime = $lifetime;
  23. }
  24. public function countUsages(string $hash): int
  25. {
  26. $key = rawurlencode($hash);
  27. if (!$this->cache->hasItem($key)) {
  28. return 0;
  29. }
  30. return $this->cache->getItem($key)->get();
  31. }
  32. public function incrementUsages(string $hash): void
  33. {
  34. $item = $this->cache->getItem(rawurlencode($hash));
  35. if (!$item->isHit()) {
  36. $item->expiresAfter($this->lifetime);
  37. }
  38. $item->set($this->countUsages($hash) + 1);
  39. $this->cache->save($item);
  40. }
  41. }