Redis   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 18
dl 0
loc 89
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 0 3 1
A addPrefix() 0 3 1
A getValue() 0 3 1
A getKeys() 0 6 1
A save() 0 5 1
A __construct() 0 4 1
A removePrefix() 0 6 1
1
<?php
2
3
namespace Semaphoro\Storages;
4
5
6
use Predis\Client;
7
8
class Redis implements StorageInterface
9
{
10
    /**
11
     * @var \Predis\Client
12
     */
13
    private $redisClient;
14
15
    /**
16
     * @var string
17
     */
18
    private $prefixKey;
19
20
    /**
21
     * Redis constructor.
22
     *
23
     * @param Client $redis The redis instance
24
     * @param string $prefix
25
     */
26 5
    public function __construct(Client $redis, string $prefix = 'semaphoro')
27
    {
28 5
        $this->redisClient = $redis;
29 5
        $this->prefixKey = $prefix;
30 5
    }
31
32
    /**
33
     * @param string $key
34
     * @return array
35
     */
36 1
    public function getKeys(string $key): array
37
    {
38 1
        $keys = $this->redisClient->keys($this->addPrefix($key));
39
40
        return array_filter($keys, function ($key) {
41 1
            return $this->removePrefix($key);
42 1
        });
43
    }
44
45
    /**
46
     * @param string $key
47
     * @return string
48
     */
49 1
    public function getValue(string $key): string
50
    {
51 1
        return $this->redisClient->get($this->addPrefix($key));
52
    }
53
54
    /**
55
     * @param string $key
56
     * @param string $value
57
     * @return bool
58
     */
59 1
    public function save(string $key, string $value): bool
60
    {
61 1
        return (bool)$this->redisClient->set(
62 1
            $this->addPrefix($key),
63 1
            $value
64
        );
65
    }
66
67
    /**
68
     * @param string $key
69
     * @return bool
70
     */
71 1
    public function remove(string $key): bool
72
    {
73 1
        return (bool)$this->redisClient->del([$this->addPrefix($key)]);
74
    }
75
76
77
    /**
78
     * @param string $key
79
     * @return string
80
     */
81 1
    private function removePrefix(string $key): string
82
    {
83 1
        return str_replace(
84 1
            sprintf('%s:', $this->prefixKey),
85 1
            '',
86 1
            $key
87
        );
88
    }
89
90
    /**
91
     * @param string $key
92
     * @return string
93
     */
94 4
    private function addPrefix(string $key): string
95
    {
96 4
        return sprintf('%s:%s', $this->prefixKey, $key);
97
    }
98
}