MemcachedRateLimiter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 11
c 1
b 0
f 0
dl 0
loc 31
rs 10
ccs 7
cts 7
cp 1

2 Methods

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