cache = new LoginCache($context, (int)$interval * 60); $this->maxCount = (int) $maxCount; $this->interval = (int) $interval; } /** * @return int */ public function getInterval() { return $this->interval; } /** * Check if user has hit rate limiter. Remember to use registerRateLimitedAction() before doing the check. * * @param string $key * @param string $type * @return bool */ public function isRateLimited($key, $type = 'username') { if (!$key || !$this->interval) { return false; } return $this->maxCount && count($this->getAttempts($key, $type)) > $this->maxCount; } /** * * * @param string $key * @param string $type * @return array */ public function getAttempts($key, $type = 'username') { return (array) $this->cache->get($type . $key, []); } /** * Register rate limited action. * * @param string $key * @param string $type * @return $this */ public function registerRateLimitedAction($key, $type = 'username') { if ($key && $this->interval) { $tries = (array)$this->cache->get($type . $key, []); $tries[] = time(); $this->cache->set($type . $key, $tries); } return $this; } /** * Reset the user rate limit counter. * * @param string $key * @param string $type * @return $this */ public function resetRateLimit($key, $type = 'username') { if ($key) { $this->cache->delete($type . $key); } return $this; } }