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

ArrayDriver::expired()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Support\Cache;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Arr;
7
use Psr\SimpleCache\CacheInterface;
8
9
class ArrayDriver extends CacheDriver implements CacheInterface
10
{
11
    /** @var array */
12
    protected $store = [];
13
14
    public function get($key, $default = null)
15
    {
16
        return Arr::get($this->store, "{$this->validateKey($key)}.value", $default);
17
    }
18
19
    public function set($key, $value, $ttl = null)
20
    {
21
        return is_array(Arr::set(
22
            $this->store,
23
            $this->validateKey($key),
24
            [
25
                'value' => $value,
26
                'expires' => is_null($ttl) ? null : Carbon::now()->addSeconds($ttl)
27
            ]
28
        ));
29
    }
30
31
    public function delete($key)
32
    {
33
        if ($this->has($key)) {
34
            Arr::forget($this->store, $key);
35
            return true;
36
        }
37
38
        return false;
39
    }
40
41
    public function clear()
42
    {
43
        $this->store = [];
44
45
        return true;
46
    }
47
48
    public function getMultiple($keys, $default = null)
49
    {
50
        $result = [];
51
52
        foreach ($keys as $key) {
53
            $result[$key] = $this->get($key, $default);
54
        }
55
56
        return $result;
57
    }
58
59
    public function setMultiple($values, $ttl = null)
60
    {
61
        foreach ($values as $key => $value) {
62
            $this->set($key, $value, $ttl);
63
        }
64
65
        return true;
66
    }
67
68
    public function deleteMultiple($keys)
69
    {
70
        foreach ($keys as $key) {
71
            if ($this->delete($key)) {
72
                continue;
73
            } else {
74
                return false;
75
            }
76
        }
77
78
        return true;
79
    }
80
81
    public function expired($key): bool
82
    {
83
        $expires = Arr::get($this->store, "{$key}.expires");
84
85
        return is_null($expires) ? false : Carbon::now()->greaterThan($expires);
86
    }
87
88
    public function empty(): bool
89
    {
90
        return empty($this->store);
91
    }
92
93
    public function has($key)
94
    {
95
        return Arr::has($this->store, $this->validateKey($key));
96
    }
97
}
98