Passed
Push — master ( ae0cd8...4570a8 )
by Petr
02:19
created

Storage::checkStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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