45 lines
1.1 KiB
PHP
45 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Support;
|
|
|
|
use App\Service\PublicFormRateLimiter;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
class TestPublicFormRateLimiter extends PublicFormRateLimiter
|
|
{
|
|
/**
|
|
* @var array<string, int>
|
|
*/
|
|
private array $counters = [];
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function isRegistrationAllowed(string $email, Request $request): bool
|
|
{
|
|
return $this->consume($email, $request);
|
|
}
|
|
|
|
public function isPasswordResetAllowed(string $email, Request $request): bool
|
|
{
|
|
return $this->consume($email, $request);
|
|
}
|
|
|
|
public function createBucketKey(string $email, ?string $clientIp): string
|
|
{
|
|
return hash('sha256', mb_strtolower(trim($email)).'|'.($clientIp ?? 'unknown'));
|
|
}
|
|
|
|
private function consume(string $email, Request $request): bool
|
|
{
|
|
$bucketKey = $this->createBucketKey($email, $request->getClientIp());
|
|
$current = $this->counters[$bucketKey] ?? 0;
|
|
$this->counters[$bucketKey] = ++$current;
|
|
|
|
return 5 >= $current;
|
|
}
|
|
}
|