Completed
Pull Request — master (#2)
by Nikola
01:44
created

RedisStorage   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 0
dl 0
loc 50
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 10 2
A set() 0 4 1
A increment() 0 4 1
A ttl() 0 4 1
1
<?php
2
/**
3
 * This file is part of the Rate Limit package.
4
 *
5
 * Copyright (c) Nikola Posa
6
 *
7
 * For full copyright and license information, please refer to the LICENSE file,
8
 * located at the package root folder.
9
 */
10
11
declare(strict_types=1);
12
13
namespace RateLimit\Storage;
14
15
use Redis;
16
17
/**
18
 * @author Nikola Posa <[email protected]>
19
 */
20
final class RedisStorage implements StorageInterface
21
{
22
    /**
23
     * @var Redis
24
     */
25
    private $redis;
26
27
    public function __construct(Redis $redis)
28
    {
29
        $this->redis = $redis;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function get(string $key, $default = false)
36
    {
37
        $value = $this->redis->get($key);
38
39
        if (false === $value) {
40
            return $default;
41
        }
42
43
        return $value;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function set(string $key, $value, int $ttl)
50
    {
51
        $this->redis->setex($key, $ttl, $value);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function increment(string $key, int $by)
58
    {
59
        $this->redis->incrBy($key, $by);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function ttl(string $key) : int
66
    {
67
        return (int) $this->redis->ttl($key);
68
    }
69
}
70