Completed
Push — master ( 6a3797...0f1dca )
by Nikola
06:00 queued 04:04
created

RedisStorage::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
crap 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 4
    public function __construct(Redis $redis)
28
    {
29 4
        $this->redis = $redis;
30 4
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 3
    public function get(string $key, $default = false)
36
    {
37 3
        $value = $this->redis->get($key);
38
39 3
        if (false === $value) {
40 1
            return $default;
41
        }
42
43 2
        return $value;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function set(string $key, $value, int $ttl)
50
    {
51 3
        $this->redis->setex($key, $ttl, $value);
52 3
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function increment(string $key, int $by)
58
    {
59 1
        $this->redis->incrBy($key, $by);
60 1
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 1
    public function ttl(string $key) : int
66
    {
67 1
        return (int) $this->redis->ttl($key);
68
    }
69
}
70