Redis::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Benrowe\Laravel\Config\Storage;
4
5
use Predis\Client;
6
7
/**
8
 * Redis store for config
9
 *
10
 * @package Benrowe\Laravel\Config\Storage
11
 */
12
class Redis implements StorageInterface
13
{
14
    private $redis;
15
    private $hash;
16
17
    /**
18
     * Constructor
19
     *
20
     * @param Client $redis [description]
21
     * @param string $hash  [description]
22
     */
23
    public function __construct(Client $redis, $hash = 'config')
24
    {
25
        $this->redis = $redis;
26
        $this->hash = $hash;
27
    }
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function save($key, $value)
33
    {
34
        $this->delKey($key);
35
36
        $this->redis->hset($this->hash, $key, $value);
37
38
        if (is_array($value)) {
39
            foreach ($value as $i => $arrValue) {
40
                $k = $key.'['.$i.']';
41
                $this->redis->hset($this->hash, $k, $arrValue);
42
            }
43
            return;
44
        }
45
        $this->redis->hset($this->hash, $key, $value);
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function load()
52
    {
53
        return $this->redis->hgetall($this->hash);
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public function clear()
60
    {
61
        $this->redis->del($this->hash);
62
    }
63
64
    private function delKey($key)
65
    {
66
        $keys = $this->redis->hkeys($this->hash);
67
        foreach ($keys as $k) {
68
            if (strpos($k, $key) === 0) {
69
                $this->redis->hdel($k);
70
            }
71
        }
72
    }
73
}
74