<?php

declare(strict_types=1);

namespace CpCreation\AltchaBundle\Tests\Double;

use CpCreation\AltchaBundle\Security\LocalRateLimiter;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;

final class LocalRateLimiterFactory
{
    public static function create(int $challengeLimit = 1_000, int $submissionLimit = 1_000): LocalRateLimiter
    {
        $storage = new InMemoryStorage();

        return new LocalRateLimiter(
            self::factory('challenge', $challengeLimit, $storage),
            self::factory('submission', $submissionLimit, $storage),
        );
    }

    private static function factory(string $id, int $limit, InMemoryStorage $storage): RateLimiterFactory
    {
        return new RateLimiterFactory([
            'id' => 'test_'.$id,
            'policy' => 'fixed_window',
            'limit' => $limit,
            'interval' => '1 minute',
        ], $storage);
    }
}
