RedisCache   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Test Coverage

Coverage 85.37%

Importance

Changes 0
Metric Value
wmc 20
eloc 33
dl 0
loc 168
ccs 35
cts 41
cp 0.8537
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A flushAll() 0 9 2
A get() 0 7 2
A search() 0 3 1
A getDirName() 0 9 2
A setPrefixSeparator() 0 3 1
A has() 0 7 2
A set() 0 13 4
A remove() 0 7 2
A ttl() 0 7 2
A getHashPrefix() 0 3 1
1
<?php
2
3
namespace Matecat\SimpleS3\Components\Cache;
4
5
use Matecat\SimpleS3\Helpers\File;
6
use Predis\Client as Redis;
7
8
class RedisCache implements CacheInterface
0 ignored issues
show
Bug introduced by
The type Matecat\SimpleS3\Components\Cache\CacheInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
{
10
    /**
11
     * @var string
12
     */
13
    private string $prefixSeparator = DIRECTORY_SEPARATOR;
14
15
    /**
16
     * @var Redis
17
     */
18
    private Redis $redisClient;
19
20
    /**
21
     * RedisCache constructor.
22
     *
23
     * @param Redis $redisClient
24
     */
25 34
    public function __construct(Redis $redisClient)
26
    {
27 34
        $this->redisClient = $redisClient;
28
    }
29
30
    /**
31
     * @return bool
32
     */
33
    public function flushAll(): bool
34
    {
35
        $flush = $this->redisClient->flushall();
36
37
        if ($flush->getPayload() === 'OK') {
38
            return true;
39
        }
40
41
        return false;
42
    }
43
44
    /**
45
     * @param string      $bucket
46
     * @param string      $keyname
47
     * @param string|null $version
48
     *
49
     * @return mixed
50
     */
51 11
    public function get(string $bucket, string $keyname, ?string $version = null): mixed
52
    {
53 11
        if (null != $version) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $version of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
54 2
            $keyname .= '<VERSION_ID:' . $version . '>';
55
        }
56
57 11
        return unserialize($this->redisClient->hget($this->getHashPrefix($bucket, $keyname), $keyname));
58
    }
59
60
    /**
61
     * @param string      $bucket
62
     * @param string      $keyname
63
     * @param string|null $version
64
     *
65
     * @return bool
66
     */
67 12
    public function has(string $bucket, string $keyname, ?string $version = null): bool
68
    {
69 12
        if (null != $version) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $version of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
70 2
            $keyname .= '<VERSION_ID:' . $version . '>';
71
        }
72
73 12
        return 1 === $this->redisClient->hexists($this->getHashPrefix($bucket, $keyname), $keyname);
74
    }
75
76
    /**
77
     * @param string      $bucket
78
     * @param string      $keyname
79
     * @param string|null $version
80
     *
81
     * @return bool
82
     */
83 6
    public function remove(string $bucket, string $keyname, ?string $version = null): bool
84
    {
85 6
        if (null != $version) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $version of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
86 1
            $keyname .= '<VERSION_ID:' . $version . '>';
87
        }
88
89 6
        return $this->redisClient->hdel($this->getHashPrefix($bucket, $keyname), [$keyname]) === 1;
90
    }
91
92
    /**
93
     * @param string $bucket
94
     * @param string $keyname
95
     *
96
     * @return array
97
     */
98 10
    public function search(string $bucket, string $keyname): array
99
    {
100 10
        return $this->redisClient->hkeys($this->getHashPrefix($bucket, $keyname));
101
    }
102
103
    /**
104
     * @param string      $bucket
105
     * @param string      $keyname
106
     * @param mixed       $content
107
     * @param string|null $version
108
     * @param int         $ttl
109
     *
110
     * @return int
111
     */
112 17
    public function set(string $bucket, string $keyname, mixed $content, ?string $version = null, int $ttl = 0): int
113
    {
114 17
        if (null != $version) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $version of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
115 3
            $keyname .= '<VERSION_ID:' . $version . '>';
116
        }
117
118 17
        $res = $this->redisClient->hset($this->getHashPrefix($bucket, $keyname), $keyname, serialize($content));
119
120 17
        if ($this->ttl($bucket, $keyname) === -1) {
121 9
            return $this->redisClient->expire($this->getHashPrefix($bucket, $keyname), (null != $ttl) ? $ttl * 60 : self::TTL_STANDARD);
122
        }
123
124 17
        return $res;
125
    }
126
127
    /**
128
     * @param string $separator
129
     */
130 31
    public function setPrefixSeparator(string $separator): void
131
    {
132 31
        $this->prefixSeparator = $separator;
133
    }
134
135
    /**
136
     * @param string      $bucket
137
     * @param string      $keyname
138
     * @param string|null $version
139
     *
140
     * @return int
141
     */
142 17
    public function ttl(string $bucket, string $keyname, ?string $version = null): int
143
    {
144 17
        if (null != $version) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $version of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
145
            $keyname .= '<VERSION_ID:' . $version . '>';
146
        }
147
148 17
        return $this->redisClient->ttl($this->getHashPrefix($bucket, $keyname));
149
    }
150
151
    /**
152
     * @param string $bucketName
153
     * @param string $keyName
154
     *
155
     * @return string
156
     */
157 23
    private function getHashPrefix(string $bucketName, string $keyName): string
158
    {
159 23
        return hash(self::HASH_ALGORITHM, $bucketName . self::HASH_SAFE_SEPARATOR . $this->getDirName($keyName));
160
    }
161
162
    /**
163
     * @param string $item
164
     *
165
     * @return string
166
     */
167 23
    private function getDirName(string $item): string
168
    {
169 23
        if (File::endsWith($item, $this->prefixSeparator)) {
170 10
            return $item;
171
        }
172
173 20
        $fileInfo = File::getPathInfo($item);
174
175 20
        return $fileInfo[ 'dirname' ] . $this->prefixSeparator;
176
    }
177
}
178