Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3fd7ea8

Browse filesBrowse files
committed
feature #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 | #59925 | License | MIT First step for #59925. Commits ------- aae5b46 [RateLimiter] add `CompoundRateLimiterFactory`
2 parents 272c580 + aae5b46 commit 3fd7ea8
Copy full SHA for 3fd7ea8

File tree

3 files changed

+78
-0
lines changed
Filter options

3 files changed

+78
-0
lines changed

‎src/Symfony/Component/RateLimiter/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/RateLimiter/CHANGELOG.md
+1
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
---
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+
}
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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.