Passed
Push — feature/0.7.0 ( 0c7d59...ae5b22 )
by Ryuichi
78:01 queued 33:04
created

Redis::clear()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 5
nop 0
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
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 . '.';
0 ignored issues
show
Bug Best Practice introduced by
The property cachePrefix does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property classPrefix does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function add($key, $value, $ttl = 0, $overwrite = false): bool
40
    {
41
        if (!$this->isAvailableCacheLibrary()) {
42
            return false;
43
        }
44
45
        if (is_array($value)) {
46
            $value = json_encode($value);
47
        }
48
49
        $result = false;
50
        if ($ttl > 0) {
51
            if ($overwrite) {
52
                $result = $this->cacheContainer->driver->setEx($key, $ttl, $value);
0 ignored issues
show
Bug Best Practice introduced by
The property driver does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method setEx() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
                /** @scrutinizer ignore-call */ 
53
                $result = $this->cacheContainer->driver->setEx($key, $ttl, $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            } else {
54
                $result = $this->cacheContainer->driver->set($key, $value, ['nx', 'ex' => $ttl]);
55
            }
56
        } else {
57
            if ($overwrite) {
58
                $result = $this->cacheContainer->driver->set($key, $value);
59
            } else {
60
                $result = $this->cacheContainer->driver->setNx($key, $value);
61
            }
62
        }
63
64
        $this->logger->info("Execute cache save: " . $key);
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Cache\Driver\Redis. Since you implemented __get, consider adding a @property annotation.
Loading history...
65
66
        return $result;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function get($key)
73
    {
74
        if (!$this->isAvailableCacheLibrary()) {
75
            return null;
76
        }
77
78
        $result = $this->cacheContainer->driver->get($key);
0 ignored issues
show
Bug Best Practice introduced by
The property driver does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
79
        $this->logger->info("Execute cache read: " . $key);
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Cache\Driver\Redis. Since you implemented __get, consider adding a @property annotation.
Loading history...
80
81
        if ($result !== false) {
82
            $this->logger->info("Execute cache read: " . $key);
83
        } else {
84
            $this->logger->warn("Failed to read cache: " . $key);
85
            $result = null;
86
        }
87
88
        return $result;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function delete($key): bool
95
    {
96
        if (!$this->isAvailableCacheLibrary()) {
97
            return false;
98
        }
99
100
        if ($this->cacheContainer->driver->delete($key) > 0) {
0 ignored issues
show
Bug Best Practice introduced by
The property driver does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
101
            $this->logger->info("Execute cache cleared: " . $key);
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Cache\Driver\Redis. Since you implemented __get, consider adding a @property annotation.
Loading history...
102
            return true;
103
        } else {
104
            $this->logger->warn("Failed to clear cache: " . $key);
105
            return false;
106
        }
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function clear(): bool
113
    {
114
        if (!$this->isAvailableCacheLibrary()) {
115
            return false;
116
        }
117
118
        $it = null;
119
        $result = 1;
120
        $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, "");
0 ignored issues
show
Bug Best Practice introduced by
The property driver does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property redisOptPrefix does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
121
        while ($keys = $this->cacheContainer->driver->scan($it, "*")) {
122
            $result *= $this->cacheContainer->driver->delete($keys);
123
        }
124
        $this->cacheContainer->driver->setOption($this->cacheContainer->redisOptPrefix, $this->cachePrefix);
125
126
        if ($result > 0) {
127
            $this->logger->info("Execute all cache cleared: " . $this->cachePrefix . "*");
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Cache\Driver\Redis. Since you implemented __get, consider adding a @property annotation.
Loading history...
128
            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
    private function isAvailableCacheLibrary(): bool
140
    {
141
        if ($this->cacheContainer->available) {
0 ignored issues
show
Bug Best Practice introduced by
The property available does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
142
            return true;
143
        }
144
145
        $this->logger->warn("Redis cache library is unavailable.");
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist on WebStream\Cache\Driver\Redis. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
147
        return false;
148
    }
149
}
150