Completed
Push — drivers ( aadeb1...dadabd )
by Joe
02:08
created

CacheDriver::exists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Support\Cache;
4
5
use Closure;
6
use Psr\SimpleCache\CacheInterface;
7
use PhpWinTools\WmiScripting\Configuration\Config;
8
use PhpWinTools\WmiScripting\Exceptions\CacheInvalidArgumentException;
9
10
abstract class CacheDriver implements CacheInterface
11
{
12
    /** @var Config */
13
    protected $config;
14
15
    /** @var mixed */
16
    protected $store;
17
18
    /** @var string|null */
19
    protected $name = null;
20
21
    public function __construct(Config $config = null, string $name = null)
22
    {
23
        $this->config = $config ?? Config::instance();
24
        $this->name = $name;
25
    }
26
27
    /**
28
     * @param string             $key
29
     * @param null|Closure|mixed $default
30
     *
31
     * @throws CacheInvalidArgumentException
32
     *
33
     * @return mixed
34
     */
35
    abstract public function get($key, $default = null);
36
37
    /**
38
     * @param iterable           $keys
39
     * @param null|Closure|mixed $default
40
     *
41
     * @throws CacheInvalidArgumentException
42
     *
43
     * @return iterable
44
     */
45
    abstract public function getMultiple($keys, $default = null);
46
47
    /**
48
     * @param string         $key
49
     * @param mixed          $value
50
     * @param null|int|mixed $ttl
51
     *
52
     * @throws CacheInvalidArgumentException
53
     *
54
     * @return bool
55
     */
56
    abstract public function set($key, $value, $ttl = null);
57
58
    /**
59
     * @param iterable       $values
60
     * @param null|int|mixed $ttl
61
     *
62
     * @throws CacheInvalidArgumentException
63
     *
64
     * @return bool
65
     */
66
    abstract  public function setMultiple($values, $ttl = null);
67
68
    /**
69
     * @param string $key
70
     *
71
     * @throws CacheInvalidArgumentException
72
     *
73
     * @return bool
74
     */
75
    abstract public function delete($key);
76
77
    /**
78
     * @param iterable $keys
79
     *
80
     * @throws CacheInvalidArgumentException
81
     *
82
     * @return bool
83
     */
84
    abstract public function deleteMultiple($keys);
85
86
87
    /**
88
     * @param string $key
89
     *
90
     * @return bool
91
     *
92
     * @throws CacheInvalidArgumentException
93
     */
94
    abstract public function has($key);
95
96
    /**
97
     * @param string $key
98
     *
99
     * @return bool
100
     */
101
    abstract public function expired($key): bool;
102
103
    /**
104
     * @param string $key
105
     *
106
     * @return bool
107
     */
108
    public function notExpired($key)
109
    {
110
        return $this->expired($key) === false;
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    abstract public function empty(): bool;
117
118
    /**
119
     * @return bool
120
     */
121
    public function notEmpty(): bool
122
    {
123
        return $this->empty() === false;
124
    }
125
126
    /**
127
     * @return string|null
128
     */
129
    public function name()
130
    {
131
        return $this->name;
132
    }
133
134
    /**
135
     * @param string $key
136
     *
137
     * @return bool
138
     */
139
    public function canGet($key): bool
140
    {
141
        try {
142
            return $this->has($key) && $this->notExpired($key);
143
        } catch (CacheInvalidArgumentException $exception) {
144
            return false;
145
        }
146
    }
147
148
    /**
149
     * @param string         $key
150
     * @param mixed          $value
151
     * @param null|int|mixed $ttl
152
     *
153
     * @return bool
154
     */
155
    public function canSet($key, $value, $ttl = null): bool
156
    {
157
        try {
158
            return $this->validateKey($key) === $key && $this->isValidValue($value) && $this->isValidTtl($ttl);
159
        } catch (CacheInvalidArgumentException $exception) {
160
            return false;
161
        }
162
    }
163
164
    /**
165
     * @param string $key
166
     *
167
     * @return bool
168
     */
169
    public function canDelete($key): bool
170
    {
171
        try {
172
            return $this->has($key);
173
        } catch (CacheInvalidArgumentException $exception) {
174
            return false;
175
        }
176
    }
177
178
    /**
179
     * @param null|int|mixed $ttl
180
     *
181
     * @return bool
182
     */
183
    protected function isValidTtl($ttl): bool
184
    {
185
        return is_null($ttl) || is_int($ttl);
186
    }
187
188
    /**
189
     * @param mixed $value
190
     *
191
     * @return bool
192
     */
193
    protected function isValidValue($value): bool
194
    {
195
        return $value === $value;
196
    }
197
198
    /**
199
     * @param string $key
200
     *
201
     * @return string
202
     */
203
    protected function validateKey($key)
204
    {
205
        if (is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always true.
Loading history...
206
            return $key;
207
        }
208
209
        throw new CacheInvalidArgumentException("Not a valid key.");
210
    }
211
}
212