Passed
Push — master ( 72c7cb...d162d5 )
by Razon
08:19
created

Manager::load()   A

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
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace RazonYang\Yii2\RateLimiter\Redis;
3
4
use yii\redis\Connection;
5
use Psr\Log\LoggerInterface;
6
use RazonYang\TokenBucket\Manager as BaseManager;
7
use RazonYang\TokenBucket\SerializerInterface;
8
9
class Manager extends BaseManager
10
{
11
    /**
12
     * @var Connection $conn
13
     */
14
    private $conn;
15
16
    private $ttl = 0;
17
18
    private $prefix = '0';
19
20 5
    public function __construct(
21
        int $capacity,
22
        float $rate,
23
        LoggerInterface $logger,
24
        Connection $conn,
25
        int $ttl = 0,
26
        string $prefix = '',
27
        ?SerializerInterface $serializer = null
28
    ) {
29 5
        parent::__construct($capacity, $rate, $logger, $serializer);
30 5
        $this->conn = $conn;
31 5
        $this->ttl = $ttl;
32 5
        $this->prefix = $prefix;
33 5
    }
34
35 1
    protected function load(string $name)
36
    {
37 1
        return $this->conn->get($this->getKey($name));
38
    }
39
40 2
    protected function save(string $name, $value)
41
    {
42 2
        if ($this->ttl > 0) {
43 1
            $this->conn->setex($this->getKey($name), $this->ttl, $value);
44
        } else {
45 1
            $this->conn->set($this->getKey($name), $value);
46
        }
47 2
    }
48
49 2
    protected function getKey(string $name): string
50
    {
51 2
        return $this->prefix . $name;
52
    }
53
}
54