Replace the static Engine call (which silently failed) with Engine::default() and a MySQL named lock. Add engine->overlay integration test. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
22 lines
595 B
PHP
22 lines
595 B
PHP
<?php
|
|
declare( strict_types=1 );
|
|
|
|
namespace StreamGoals\PayPal;
|
|
|
|
/** Fixed-window counter. Best-effort (non-atomic) which is fine for abuse throttling. */
|
|
final class RateLimiter {
|
|
|
|
public function __construct( private Cache $cache ) {}
|
|
|
|
/** Returns true if the call is allowed, false if the limit is exceeded. */
|
|
public function allow( string $key, int $limit, int $windowSeconds ): bool {
|
|
$k = 'sg_rl_' . md5( $key );
|
|
$count = (int) $this->cache->get( $k );
|
|
if ( $count >= $limit ) {
|
|
return false;
|
|
}
|
|
$this->cache->set( $k, $count + 1, $windowSeconds );
|
|
return true;
|
|
}
|
|
}
|