MemcachedManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 47
rs 10
ccs 13
cts 14
cp 0.9286
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 3 1
A save() 0 4 2
A __construct() 0 13 1
A getKey() 0 3 1
1
<?php
2
namespace RazonYang\TokenBucket\Manager;
3
4
use Psr\Log\LoggerInterface;
5
use RazonYang\TokenBucket\Manager;
6
use RazonYang\TokenBucket\SerializerInterface;
7
8
class MemcachedManager extends Manager
9
{
10
    /**
11
     * @var int $ttl time to live.
12
     */
13
    private $ttl;
14
15
    /**
16
     * @var string $prefix key prefix.
17
     */
18
    private $prefix;
19
20
    /**
21
     * @var \Memcached $conn
22
     */
23
    private $conn;
24
25 2
    public function __construct(
26
        int $capacity,
27
        float $rate,
28
        LoggerInterface $logger,
29
        \Memcached $conn,
30
        int $ttl = 0,
31
        string $prefix = '',
32
        ?SerializerInterface $serializer = null
33
    ) {
34 2
        parent::__construct($capacity, $rate, $logger, $serializer);
35 2
        $this->conn = $conn;
36 2
        $this->ttl = $ttl;
37 2
        $this->prefix = $prefix;
38 2
    }
39
40 1
    protected function load(string $name)
41
    {
42 1
        return $this->conn->get($this->getKey($name));
43
    }
44
45 1
    protected function save(string $name, $data)
46
    {
47 1
        if (!$this->conn->set($this->getKey($name), $data, $this->ttl)) {
48
            throw new \RuntimeException($this->conn->getResultCode() . ':' . $this->conn->getResultMessage());
49
        }
50 1
    }
51
52 1
    protected function getKey(string $name): string
53
    {
54 1
        return $this->prefix . $name;
55
    }
56
}
57