AbstractRequestRateLimiter::consume()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 17
ccs 0
cts 10
cp 0
crap 30
rs 9.6111
1
<?php
2
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <[email protected]>
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 Biurad\Security\RateLimiter;
13
14
use Psr\Http\Message\ServerRequestInterface;
15
use Symfony\Component\RateLimiter\LimiterInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\RateLimiter\LimiterInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Symfony\Component\RateLimiter\Policy\NoLimiter;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\RateLimiter\Policy\NoLimiter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\RateLimiter\RateLimit;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\RateLimiter\RateLimit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
/**
20
 * An implementation of RequestRateLimiterInterface that fits most use-cases.
21
 *
22
 * @author Wouter de Jong <[email protected]>
23
 * @author Divine Niiquaye Ibok <[email protected]>
24
 */
25
abstract class AbstractRequestRateLimiter
26
{
27
    public function consume(ServerRequestInterface $request): RateLimit
28
    {
29
        $limiters = $this->getLimiters($request);
30
        if (0 === \count($limiters)) {
31
            $limiters = [new NoLimiter()];
32
        }
33
34
        $minimalRateLimit = null;
35
        foreach ($limiters as $limiter) {
36
            $rateLimit = $limiter->consume(1);
37
38
            if (null === $minimalRateLimit || $rateLimit->getRemainingTokens() < $minimalRateLimit->getRemainingTokens()) {
39
                $minimalRateLimit = $rateLimit;
40
            }
41
        }
42
43
        return $minimalRateLimit;
44
    }
45
46
    public function reset(ServerRequestInterface $request): void
47
    {
48
        foreach ($this->getLimiters($request) as $limiter) {
49
            $limiter->reset();
50
        }
51
    }
52
53
    /**
54
     * @return array<int,LimiterInterface> a set of limiters using keys extracted from the request
55
     */
56
    abstract protected function getLimiters(ServerRequestInterface $request): array;
57
}
58