File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed
src/Symfony/Component/RateLimiter Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ CHANGELOG
55---
66
77 * Add ` RateLimiterFactoryInterface `
8+ * Add ` CompoundRateLimiterFactory `
89
9106.4
1011---
Original file line number Diff line number Diff line change 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 number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments