StiphleRateLimiterAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 34
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A throttle() 0 17 2
A __construct() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lamoda\Tactician\RateLimit\Adapter\Stiphle;
6
7
use Lamoda\Tactician\RateLimit\RateLimiter\RateLimit;
8
use Lamoda\Tactician\RateLimit\RateLimiter\RateLimiterInterface;
9
use Psr\Log\LoggerInterface;
10
use Psr\Log\NullLogger;
11
use Stiphle\Throttle\ThrottleInterface;
12
13
final class StiphleRateLimiterAdapter implements RateLimiterInterface
14
{
15
    /**
16
     * @var ThrottleInterface
17
     */
18
    private $throttle;
19
    /**
20
     * @var LoggerInterface
21
     */
22
    private $logger;
23
24 1
    public function __construct(ThrottleInterface $throttle, LoggerInterface $logger = null)
25
    {
26 1
        $this->throttle = $throttle;
27 1
        $this->logger = $logger ?: new NullLogger();
28 1
    }
29
30 1
    public function throttle(RateLimit $rateLimit): void
31
    {
32 1
        $estimation = $this->throttle->getEstimate(
33 1
            $rateLimit->getKey(),
34 1
            $rateLimit->getLimit(),
35 1
            $rateLimit->getMilliseconds()
36
        );
37
38 1
        $this->throttle->throttle(
39 1
            $rateLimit->getKey(),
40 1
            $rateLimit->getLimit(),
41 1
            $rateLimit->getMilliseconds()
42
        );
43
44 1
        if ($estimation > 0) {
45 1
            $this->logger->info('Rate limiter throttled execution for {time} ms', [
46 1
                'time' => $estimation,
47
            ]);
48
        }
49 1
    }
50
}
51