InMemoryThrottleStore.php 864 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Auth;
  4. /**
  5. * Process-local {@see ThrottleStore}. State is held in a private array;
  6. * lost when the worker (or process) exits. Used by unit tests and as the
  7. * default for environments that don't need cross-worker persistence.
  8. */
  9. final class InMemoryThrottleStore implements ThrottleStore
  10. {
  11. /**
  12. * @var array{
  13. * ip: array<string, array{count: int, lockedUntil: int}>,
  14. * username: array<string, array{count: int, lockedUntil: int}>
  15. * }
  16. */
  17. private array $state = ['ip' => [], 'username' => []];
  18. public function load(): array
  19. {
  20. return $this->state;
  21. }
  22. public function mutate(callable $fn): void
  23. {
  24. $this->state = $fn($this->state);
  25. }
  26. public function reset(): void
  27. {
  28. $this->state = ['ip' => [], 'username' => []];
  29. }
  30. }