8000 feature #59929 [RateLimiter] Add `CompoundRateLimiterFactory` (kbond) · simPod/symfony@3fd7ea8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fd7ea8

Browse files
committed
feature symfony#59929 [RateLimiter] Add CompoundRateLimiterFactory (kbond)
This PR was merged into the 7.3 branch. Discussion ---------- [RateLimiter] Add `CompoundRateLimiterFactory` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | symfony#59925 | License | MIT First step for symfony#59925. Commits ------- aae5b46 [RateLimiter] add `CompoundRateLimiterFactory`
2 parents 272c580 + aae5b46 commit 3fd7ea8

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/Symfony/Component/RateLimiter/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `RateLimiterFactoryInterface`
8+
* Add `CompoundRateLimiterFactory`
89

910
6.4
1011
---
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\RateLimiter;
13+
14+
/**
15+
* @author Kevin Bond <kevinbond@gmail.com>
16+
*/
17+
final class CompoundRateLimiterFactory implements RateLimiterFactoryInterface
18+
{
19+
/**
20+
* @param iterable<RateLimiterFactoryInterface> $rateLimiterFactories
21+
*/
22+
public function __construct(private iterable $rateLimiterFactories)
23+
{
24+
}
25+
26+
public function create(?string $key = null): LimiterInterface
27+
{
28+
$rateLimiters = [];
29+
30+
foreach ($this->rateLimiterFactories as $rateLimiterFactory) {
31+
$rateLimiters[] = $rateLimiterFactory->create($key);
32+
}
33+
34+
return new CompoundLimiter($rateLimiters);
35+
}
36+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\RateLimiter\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\RateLimiter\CompoundLimiter;
16+
use Symfony\Component\RateLimiter\CompoundRateLimiterFactory;
17+
use Symfony\Component\RateLimiter\LimiterInterface;
18+
use Symfony\Component\RateLimiter\RateLimiterFactoryInterface;
19+
20+
class CompoundRateLimiterFactoryTest extends TestCase
21+
{
22+
public function testCreate()
23+
{
24+
$factory1 = $this->createMock(RateLimiterFactoryInterface::class);
25+
$factory1
26+
->method('create')
27+
->with('foo')
28+
->willReturn($this->createMock(LimiterInterface::class))
29+
;
30+
$factory2 = $this->createMock(RateLimiterFactoryInterface::class);
31+
$factory2
32+
->method('create')
33+
->with('foo')
34+
->willReturn($this->createMock(LimiterInterface::class))
35+
;
36+
37+
$compoundFactory = new CompoundRateLimiterFactory([$factory1, $factory2]);
38+
39+
$this->assertInstanceOf(CompoundLimiter::class, $compoundFactory->create('foo'));
40+
}
41+
}

0 commit comments

Comments
 (0)
0