Storage::deleteByPrefix()   B
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 56
rs 8.8333
1
<?php
2
3
namespace kalanis\kw_storage;
4
5
6
use kalanis\kw_storage\Interfaces\IStorage;
7
use kalanis\kw_storage\Interfaces\IStTranslations;
8
use kalanis\kw_storage\Traits\TLang;
9
use Traversable;
10
11
12
/**
13
 * Class Storage
14
 * @package kalanis\kw_storage
15
 * Main storage class
16
 * Technically also facade for all basic operations
17
 */
18
class Storage
19
{
20
    use TLang;
21
22
    protected ?IStorage $storage = null;
23
    protected Storage\Factory $storageFactory;
24
25 7
    public function __construct(Storage\Factory $storageFactory, ?IStTranslations $lang = null)
26
    {
27 7
        $this->storageFactory = $storageFactory;
28 7
        $this->setStLang($lang);
29
    }
30
31
    /**
32
     * @param object|array<string, string|object>|string|null $storageParams
33
     * @throws StorageException
34
     */
35 6
    public function init($storageParams): void
36
    {
37 6
        if (is_array($storageParams) && empty($storageParams['lang']) && $this->stLang) {
38 1
            $storageParams['lang'] = $this->stLang;
39
        }
40 6
        $this->storage = $this->storageFactory->getStorage($storageParams);
41
    }
42
43
    /**
44
     * If entry exists in storage
45
     * @param string $key
46
     * @throws StorageException
47
     * @return boolean
48
     */
49 2
    public function exists(string $key): bool
50
    {
51 2
        return $this->getStorage()->exists($key);
52
    }
53
54
    /**
55
     * Get data from storage
56
     * @param string $key
57
     * @throws StorageException
58
     * @return string|null
59
     */
60 1
    public function get(string $key): ?string
61
    {
62 1
        $content = $this->getStorage()->read($key);
63 1
        return empty($content) ? null : $content ;
64
    }
65
66
    /**
67
     * Set data to storage
68
     * @param string $key
69
     * @param string $value
70
     * @param int|null $expire
71
     * @throws StorageException
72
     * @return boolean
73
     */
74 1
    public function set(string $key, string $value, ?int $expire = 8600): bool
75
    {
76 1
        return $this->getStorage()->write($key, $value, $expire);
77
    }
78
79
    /**
80
     * Add data to storage
81
     * @param string $key
82
     * @param string $value
83
     * @param int|null $expire
84
     * @throws StorageException
85
     * @return boolean
86
     */
87 1
    public function add(string $key, string $value, ?int $expire = 8600): bool
88
    {
89
        // safeadd for multithread on any system
90 1
        if ($this->getStorage()->write($key, $value, $expire)) {
91 1
            return ( $value == $this->get($key) );
92
        }
93 1
        return false;
94
    }
95
96
    /**
97
     * Increment value by key
98
     * @param string $key
99
     * @throws StorageException
100
     * @return boolean
101
     */
102 2
    public function increment(string $key): bool
103
    {
104 2
        return $this->getStorage()->increment($key);
105
    }
106
107
    /**
108
     * Decrement value by key
109
     * @param string $key
110
     * @throws StorageException
111
     * @return boolean
112
     */
113 1
    public function decrement(string $key): bool
114
    {
115 1
        return $this->getStorage()->decrement($key);
116
    }
117
118
    /**
119
     * Return all active storage keys
120
     * @throws StorageException
121
     * @return Traversable<string>
122
     */
123 1
    public function getAllKeys(): Traversable
124
    {
125 1
        return $this->getMaskedKeys('');
126
    }
127
128
    /**
129
     * Return storage keys with mask
130
     * @param string $mask
131
     * @throws StorageException
132
     * @return Traversable<string>
133
     */
134 1
    public function getMaskedKeys(string $mask): Traversable
135
    {
136 1
        return $this->getStorage()->lookup($mask);
137
    }
138
139
    /**
140
     * Delete data by key from storage
141
     * @param string $key
142
     * @throws StorageException
143
     * @return boolean
144
     */
145 1
    public function delete(string $key): bool
146
    {
147 1
        return $this->getStorage()->remove($key);
148
    }
149
150
    /**
151
     * Delete multiple keys from storage
152
     * @param string[] $keys
153
     * @throws StorageException
154
     * @return array<int|string, bool>
155
     */
156 1
    public function deleteMulti(array $keys): array
157
    {
158 1
        return $this->getStorage()->removeMulti($keys);
159
    }
160
161
    /**
162
     * Delete all data from storage where key starts with prefix
163
     * @param string $prefix
164
     * @param boolean $inverse - if true remove all data where keys doesn't starts with prefix
165
     * @throws StorageException
166
     * @codeCoverageIgnore mock has no keys for now
167
     */
168
    public function deleteByPrefix(string $prefix, bool $inverse = false): void
169
    {
170
        $keysToDelete = [];
171
        foreach ($this->getAllKeys() as $memKey) {
172
            $find = strpos($memKey, $prefix);
173
            if ((! $inverse && 0 === $find) || ($inverse && (false === $find || 0 !== $find))) {
174
                $keysToDelete[] = $memKey;
175
            }
176
        }
177
        $this->deleteMulti($keysToDelete);
178
    }
179
180
    /**
181
     * Check connection status to storage
182
     * @throws StorageException
183
     * @return boolean
184
     */
185 2
    public function isConnected(): bool
186
    {
187 2
        return $this->getStorage()->canUse();
188
    }
189
190
    /**
191
     * @throws StorageException
192
     */
193 7
    public function getStorage(): IStorage
194
    {
195 7
        if (empty($this->storage)) {
196 1
            throw new StorageException($this->getStLang()->stStorageNotInitialized());
197
        }
198 6
        return $this->storage;
199
    }
200
}
201