RateLimiterMiddleware::perMinute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\GuzzleRateLimiterMiddleware;
4
5
use Psr\Http\Message\RequestInterface;
6
7
class RateLimiterMiddleware
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
8
{
9
    /** @var \Spatie\GuzzleRateLimiterMiddleware\RateLimiter */
10
    protected $rateLimiter;
11
12
    private function __construct(RateLimiter $rateLimiter)
13
    {
14
        $this->rateLimiter = $rateLimiter;
15
    }
16
17 View Code Duplication
    public static function perSecond(int $limit, Store $store = null): RateLimiterMiddleware
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
    {
19
        $rateLimiter = new RateLimiter(
20
            $limit,
21
            RateLimiter::TIME_FRAME_SECOND,
22
            $store ?? new InMemoryStore(),
23
            new SleepDeferrer()
24
        );
25
26
        return new static($rateLimiter);
27
    }
28
29 View Code Duplication
    public static function perMinute(int $limit, Store $store = null): RateLimiterMiddleware
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $rateLimiter = new RateLimiter(
32
            $limit,
33
            RateLimiter::TIME_FRAME_MINUTE,
34
            $store ?? new InMemoryStore(),
35
            new SleepDeferrer()
36
        );
37
38
        return new static($rateLimiter);
39
    }
40
41
    public function __invoke(callable $handler)
42
    {
43
        return function (RequestInterface $request, array $options) use ($handler) {
44
            return $this->rateLimiter->handle(function () use ($request, $handler, $options) {
45
                return $handler($request, $options);
46
            });
47
        };
48
    }
49
}
50