Skip to content

Commit

Permalink
Allow method overrule
Browse files Browse the repository at this point in the history
  • Loading branch information
francoism90 committed Apr 16, 2024
1 parent c33c7c1 commit f0a654a
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/Support/Concerns/WithRateLimiting.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ trait WithRateLimiting

protected function rateLimit(): void
{
if (static::$maxAttempts < 1) {
$maxAttempts = static::getMaxAttempts();

if ($maxAttempts < 1) {
return;
}

$key = static::getRateLimitKey();

if (RateLimiter::tooManyAttempts($key, static::$maxAttempts)) {
if (RateLimiter::tooManyAttempts($key, $maxAttempts)) {
throw new RateLimitedException(
request()->ip(),
RateLimiter::availableIn($key)
Expand All @@ -31,7 +33,10 @@ protected function rateLimit(): void

protected static function incrementRateLimiter(): void
{
RateLimiter::increment(static::getRateLimitKey(), static::$decaySeconds);
RateLimiter::increment(
static::getRateLimitKey(),
static::getDecaySeconds()
);
}

protected static function clearRateLimiter(): void
Expand All @@ -45,4 +50,14 @@ protected static function getRateLimitKey(): string
get_called_class(), request()->ip(),
]));
}

protected static function getMaxAttempts(): int
{
return static::$maxAttempts;
}

protected static function getDecaySeconds(): int
{
return static::$decaySeconds;
}
}

0 comments on commit f0a654a

Please sign in to comment.