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

RedisStorage::ttl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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