RedisTaggedCacheTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 166
Duplicated Lines 16.87 %

Coupling/Cohesion

Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
cbo 4
dl 28
loc 166
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedisInstance() 0 6 1
A setRedisInstance() 0 4 1
A addTaggedValue() 0 17 2
A removeTaggedValue() 14 14 2
A invalidateTag() 14 14 2
A evalScript() 0 18 2
A getInternalKeysPrefix() 0 4 1
A setInternalKeysPrefix() 0 4 1
A getInternalTagsPrefix() 0 4 1
A setInternalTagsPrefix() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace Limoncello\RedisTaggedCache;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\RedisTaggedCache\Exceptions\RedisTaggedCacheException;
22
use Limoncello\RedisTaggedCache\Scripts\RedisTaggedScripts;
23
use Redis;
24
use RuntimeException;
25
use function array_values;
26
use function assert;
27
use function json_encode;
28
29
/**
30
 * @package Limoncello\RedisTaggedCache
31
 */
32
trait RedisTaggedCacheTrait
33
{
34
    /**
35
     * @var Redis
36
     */
37
    private $redisInstance;
38
39
    /**
40
     * @var string
41
     */
42
    private $internalKeysPrefix = '_:k:';
43
44
    /**
45
     * @var string
46
     */
47
    private $internalTagsPrefix = '_:t:';
48
    /**
49
     * @return Redis
50
     */
51 6
    protected function getRedisInstance(): Redis
52
    {
53 6
        assert($this->redisInstance !== null, 'Redis instance should be set before usage.');
54
55 6
        return $this->redisInstance;
56
    }
57
58
    /**
59
     * @param Redis $redisInstance
60
     */
61 6
    protected function setRedisInstance(Redis $redisInstance): void
62
    {
63 6
        $this->redisInstance = $redisInstance;
64
    }
65
66
    /** @noinspection PhpDocRedundantThrowsInspection
67
     * @param string $key
68
     * @param string $value
69
     * @param array  $tags
70
     * @param int    $ttl
71
     *
72
     * @return void
73
     *
74
     * @throws RedisTaggedCacheException
75
     */
76 2
    public function addTaggedValue(string $key, string $value, array $tags, $ttl = 0): void
77
    {
78
        // if array keys are non-consecutive it will be encoded to object instead of an array.
79
        // to be on a safe side we gonna use `array_values`
80 2
        $jsonTags = json_encode(array_values($tags));
81 2
        $isOk     = $this->evalScript(
82 2
            RedisTaggedScripts::ADD_VALUE_SCRIPT_INDEX,
83 2
            [$key, $value, $jsonTags, $this->getInternalKeysPrefix(), $this->getInternalTagsPrefix(), $ttl],
84 2
            1
85
        );
86
87 2
        if ($isOk === false) {
88
            throw new class extends RuntimeException implements RedisTaggedCacheException
89
            {
90
            };
91
        }
92
    }
93
94
    /** @noinspection PhpDocRedundantThrowsInspection
95
     * @param string $key
96
     *
97
     * @return void
98
     *
99
     * @throws RedisTaggedCacheException
100
     */
101 2 View Code Duplication
    public function removeTaggedValue(string $key): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103 2
        $isOk = $this->evalScript(
104 2
            RedisTaggedScripts::REMOVE_VALUE_SCRIPT_INDEX,
105 2
            [$key, $this->getInternalKeysPrefix(), $this->getInternalTagsPrefix()],
106 2
            1
107
        );
108
109 2
        if ($isOk === false) {
110
            throw new class extends RuntimeException implements RedisTaggedCacheException
111
            {
112
            };
113
        }
114
    }
115
116
    /** @noinspection PhpDocRedundantThrowsInspection
117
     * @param string $tag
118
     *
119
     * @return void
120
     *
121
     * @throws RedisTaggedCacheException
122
     */
123 2 View Code Duplication
    public function invalidateTag(string $tag): void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125 2
        $isOk = $this->evalScript(
126 2
            RedisTaggedScripts::INVALIDATE_TAG_SCRIPT_INDEX,
127 2
            [$tag, $this->getInternalKeysPrefix(), $this->getInternalTagsPrefix()],
128 2
            1
129
        );
130
131 2
        if ($isOk === false) {
132
            throw new class extends RuntimeException implements RedisTaggedCacheException
133
            {
134
            };
135
        }
136
    }
137
138
    /**
139
     * @param int   $scriptIndex
140
     * @param array $arguments
141
     * @param int   $keysInArgs
142
     *
143
     * @return bool
144
     *
145
     * @SuppressWarnings(PHPMD.StaticAccess)
146
     */
147 6
    protected function evalScript(int $scriptIndex, array $arguments, int $keysInArgs): bool
148
    {
149 6
        $retValue = $this->getRedisInstance()
150 6
            ->evalSha(RedisTaggedScripts::getScriptDigest($scriptIndex), $arguments, $keysInArgs);
151
152 6
        if ($retValue === false) {
153
            // script not loaded yet
154 6
            $script = RedisTaggedScripts::getScriptBody($scriptIndex);
155 6
            $digest = $this->getRedisInstance()->script('load', $script);
156 6
            assert($digest === RedisTaggedScripts::getScriptDigest($scriptIndex));
157
158
            // eval one more time
159 6
            $retValue = $this->getRedisInstance()
160 6
                ->evalSha(RedisTaggedScripts::getScriptDigest($scriptIndex), $arguments, $keysInArgs);
161
        }
162
163 6
        return $retValue === 0;
164
    }
165
166
    /**
167
     * @return string
168
     */
169 6
    protected function getInternalKeysPrefix(): string
170
    {
171 6
        return $this->internalKeysPrefix;
172
    }
173
174
    /**
175
     * @param string $internalKeysPrefix
176
     */
177 6
    protected function setInternalKeysPrefix(string $internalKeysPrefix): void
178
    {
179 6
        $this->internalKeysPrefix = $internalKeysPrefix;
180
    }
181
182
    /**
183
     * @return string
184
     */
185 6
    protected function getInternalTagsPrefix(): string
186
    {
187 6
        return $this->internalTagsPrefix;
188
    }
189
190
    /**
191
     * @param string $internalTagsPrefix
192
     */
193 6
    protected function setInternalTagsPrefix(string $internalTagsPrefix): void
194
    {
195 6
        $this->internalTagsPrefix = $internalTagsPrefix;
196
    }
197
}
198