Skip to content

Navigation Menu

Sign in
Appearance settings

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 9dac513

Browse filesBrowse files
committed
[RateLimiter] add CompoundRateLimiterFactory
1 parent 6a029ec commit 9dac513
Copy full SHA for 9dac513

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
+1Lines 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
---
+36Lines 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 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+
}
+41Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.