RedisRateLimiter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 39
rs 10
ccs 8
cts 9
cp 0.8889

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedis() 0 11 3
A initManager() 0 3 1
1
<?php
2
namespace RazonYang\Yii2\RateLimiter;
3
4
use RazonYang\TokenBucket\ManagerInterface;
5
use RazonYang\TokenBucket\Manager\RedisManager;
6
use RazonYang\Yii2\RateLimiter\BaseRateLimiter;
7
8
class RedisRateLimiter extends BaseRateLimiter
9
{
10
    /**
11
     * @var string $hostname
12
     */
13
    public $hostname = 'localhost';
14
15
    /**
16
     * @var int $port
17
     */
18
    public $port = 6379;
19
20
    /**
21
     * @var string $password
22
     */
23
    public $password = '';
24
25
    public $ttl = 3600;
26
27
    public $prefix = 'rate_limiter:';
28
29 2
    protected function initManager(): ManagerInterface
30
    {
31 2
        return new RedisManager($this->capacity, $this->rate, $this->getLogger(), $this->getRedis(), $this->ttl, $this->prefix);
32
    }
33
34
    private $redis;
35
36 2
    protected function getRedis(): \Redis
37
    {
38 2
        if ($this->redis === null) {
39 2
            $this->redis = new \Redis();
40 2
            $this->redis->connect($this->hostname, $this->port);
41 2
            if ($this->password !== '') {
42
                $this->redis->auth($this->password);
43
            }
44
        }
45
46 2
        return $this->redis;
47
    }
48
}
49