MemcachedRateLimiter::initManager()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 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