Completed
Push — master ( 27c785...736581 )
by Ryuichi
02:59
created

Redis::clear()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.1574

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 11
cts 14
cp 0.7856
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 15
nc 5
nop 0
crap 4.1574
1
<?php
2
namespace WebStream\Cache\Driver;
3
4
use WebStream\DI\Injector;
5
use WebStream\Container\Container;
6
7
/**
8
 * Redis
9
 * @author Ryuichi Tanaka
10
 * @since 2015/07/09
11
 * @version 0.7
12
 */
13
class Redis implements ICache
14
{
15
    use injector;
16
17
    /**
18
     * @var Container キャッシュ依存コンテナ
19
     */
20
    private $cacheContainer;
21
22
    /**
23
     * @var string キャッシュ接頭辞
24
     */
25
    private $cachePrefix;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct(Container $cacheContainer)
31
    {
32
        $this->cacheContainer = $cacheContainer;
33
        $this->cachePrefix = $this->cacheContainer->cachePrefix . '.' . $this->cacheContainer->classPrefix . '.';
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 4
    public function add($key, $value, $ttl = 0, $overwrite = false): bool
40
    {
41 4
        if (!$this->isAvailableCacheLibrary()) {
42
            return false;
43
        }
44
45 4
        if (is_array($value)) {
46
            $value = json_encode($value);
47
        }
48
49 4
        $result = false;
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50 4
        if ($ttl > 0) {
51
            if ($overwrite) {
52
                $result = $this->cacheContainer->driver->setEx($key, $ttl, $value);
53
            } else {
54
                $result = $this->cacheContainer->driver->set($key, $value, ['nx', 'ex' => $ttl]);
55
            }
56
        } else {
57 4
            if ($overwrite) {
58 3
                $result = $this->cacheContainer->driver->set($key, $value);
59
            } else {
60 1
                $result = $this->cacheContainer->driver->setNx($key, $value);
61
            }
62
        }
63
64 4
        $this->logger->info("Execute cache save: " . $key);
65
66 4
        return $result;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 4
    public function get($key)
73
    {
74 4
        if (!$this->isAvailableCacheLibrary()) {
75
            return null;
76
        }
77
78 4
        $result = $this->cacheContainer->driver->get($key);
79 4
        $this->logger->info("Execute cache read: " . $key);
80
81 4
        if ($result !== false) {
82 2
            $this->logger->info("Execute cache read: " . $key);
83
        } else {
84 2
            $this->logger->warn("Failed to read cache: " . $key);
85 2
            $result = null;
86
        }
87
88 4
        return $result;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 1
    public function delete($key): bool
95
    {
96 1
        if (!$this->isAvailableCacheLibrary()) {
97
            return false;
98
        }
99
100 1
        if ($this->cacheContainer->driver->delete($key) > 0) {
101 1
            $this->logger->info("Execute cache cleared: " . $key);
102 1
            return true;
103
        } else {
104
            $this->logger->warn("Failed to clear cache: " . $key);
105
            return false;
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function clear(): bool
113
    {
114 1
        if (!$this->isAvailableCacheLibrary()) {
115
            return false;
116
        }
117
118 1
        $it = null;
119 1
        $result = 1;
120 1
        $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, "");
121 1
        while ($keys = $this->cacheContainer->driver->scan($it, "*")) {
122 1
            $result *= $this->cacheContainer->driver->delete($keys);
123
        }
124 1
        $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, $this->cachePrefix);
125
126 1
        if ($result > 0) {
127 1
            $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*");
128 1
            return true;
129
        } else {
130
            $this->logger->warn("Failed to clear all cache: " . $this->cachePrefix . "*");
131
            return false;
132
        }
133
    }
134
135
    /**
136
     * キャッシュライブラリが使用可能か検査する
137
     * @return bool 使用可能でtrue
138
     */
139 4
    private function isAvailableCacheLibrary(): bool
140
    {
141 4
        if ($this->cacheContainer->available) {
142 4
            return true;
143
        }
144
145
        $this->logger->warn("Redis cache library is unavailable.");
146
147
        return false;
148
    }
149
}
150