Passed
Push — drivers ( 5f8646...aadeb1 )
by Joe
01:53
created

CacheProvider::setEventProvider()   A

Complexity

Conditions 1
Paths 1

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 1
nc 1
nop 1
1
<?php
2
3
namespace PhpWinTools\WmiScripting\Support\Cache;
4
5
use Closure;
6
use DateInterval;
7
use Psr\SimpleCache\CacheInterface;
8
use Psr\SimpleCache\InvalidArgumentException;
9
use PhpWinTools\WmiScripting\Configuration\Config;
10
use PhpWinTools\WmiScripting\Support\Events\Event;
11
use PhpWinTools\WmiScripting\Support\Events\EventProvider;
12
use function PhpWinTools\WmiScripting\Support\reduce_value;
13
use PhpWinTools\WmiScripting\Support\Cache\Events\CacheHit;
14
use PhpWinTools\WmiScripting\Support\Cache\Events\CacheMissed;
15
use PhpWinTools\WmiScripting\Support\Cache\Events\CacheCleared;
16
use PhpWinTools\WmiScripting\Support\Cache\Events\CacheKeyStored;
17
use PhpWinTools\WmiScripting\Support\Cache\Events\CacheKeyExpired;
18
use PhpWinTools\WmiScripting\Support\Cache\Events\CacheKeyForgotten;
19
use PhpWinTools\WmiScripting\Exceptions\CacheInvalidArgumentException;
20
21
class CacheProvider implements CacheInterface
22
{
23
    /** @var Config */
24
    protected $config;
25
26
    /** @var CacheDriver */
27
    protected $driver;
28
29
    /** @var EventProvider */
30
    protected $events;
31
32
    public function __construct(Config $config = null, CacheDriver $driver = null, EventProvider $events = null)
33
    {
34
        $this->config = $config ?? Config::instance();
35
        $this->driver = $driver ?? $this->config->getCacheDriver();
36
        $this->events = $events ?? $this->config->eventProvider();
37
    }
38
39
    /**
40
     * @param string|array       $key
41
     * @param Closure|null|mixed $default
42
     *
43
     * @throws InvalidArgumentException|CacheInvalidArgumentException
44
     *
45
     * @return iterable|mixed|null
46
     */
47
    public function get($key, $default = null)
48
    {
49
        if (is_array($key)) {
50
            return $this->getMultiple($key);
51
        }
52
53
        $value = $this->driver()->get($key, null);
54
55
        if ($this->driver()->expired($key)) {
56
            $this->events(new CacheKeyExpired($this->driver(), $key, $value));
57
            $this->delete($key);
58
            $value = null;
59
        }
60
61
        if (is_null($value)) {
62
            $this->events(new CacheMissed($this->driver(), $key));
63
            $value = reduce_value($value);
64
        } else {
65
            $this->events(new CacheHit($this->driver(), $key, $value));
66
        }
67
68
        return $value;
69
    }
70
71
    /**
72
     * @param string|array          $key
73
     * @param mixed                 $value
74
     * @param null|int|DateInterval $ttl
75
     *
76
     * @throws InvalidArgumentException|CacheInvalidArgumentException
77
     *
78
     * @return bool
79
     */
80
    public function set($key, $value, $ttl = null)
81
    {
82
        if (is_array($key)) {
83
            return $this->setMultiple($key, $ttl);
84
        }
85
86
        $result = $this->driver()->set($key, $value, $ttl);
87
88
        if ($result) {
89
            $this->events(new CacheKeyStored($this->driver(), $key, $value, $ttl));
90
        }
91
92
        return $result;
93
    }
94
95
    public function delete($key)
96
    {
97
        if (is_array($key)) {
98
            return $this->deleteMultiple($key);
99
        }
100
101
        $result = $this->driver()->delete($key);
102
103
        if ($result) {
104
            $this->events(new CacheKeyForgotten($this->driver(), $key));
105
        }
106
107
        return $result;
108
    }
109
110
    public function clear()
111
    {
112
        $result = $this->driver->clear();
113
114
        if ($result) {
115
            $this->events(new CacheCleared($this->driver));
116
        }
117
118
        return $result;
119
    }
120
121
    public function getMultiple($keys, $default = null)
122
    {
123
        $this->multiple('get', $keys);
124
125
        return $this->driver()->getMultiple($keys, $default);
126
    }
127
128
    public function setMultiple($values, $ttl = null)
129
    {
130
        $this->multiple('set', $values, $ttl);
131
132
        return $this->driver()->setMultiple($values, $ttl);
133
    }
134
135
    public function deleteMultiple($keys)
136
    {
137
        $this->multiple('delete', $keys);
138
139
        return $this->driver()->deleteMultiple($keys);
140
    }
141
142
    public function has($key)
143
    {
144
        return $this->driver()->has($key);
145
    }
146
147
    public function driver()
148
    {
149
        return $this->driver;
150
    }
151
152
    public function setEventProvider(EventProvider $event)
153
    {
154
        $this->events = $event;
155
156
        return $this;
157
    }
158
159
    protected function multiple(string $method, $keys, $ttl = null)
160
    {
161
        foreach ($keys as $key => $value) {
162
            if ($method === 'get' && $this->driver()->canGet($key)) {
163
                $this->events(new CacheHit($this->driver, $key, $this->driver()->get($key)));
164
            }
165
166
            if ($method === 'set' && $this->driver()->canSet($key, $value)) {
167
                $this->events(new CacheKeyStored($this->driver, $key, $value, $ttl));
168
            }
169
170
            if ($method === 'delete' && $this->driver()->canDelete($key)) {
171
                $this->events(new CacheKeyForgotten($this->driver, $key));
172
            }
173
        }
174
    }
175
176
    protected function events(Event $event, string $key = null)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

176
    protected function events(Event $event, /** @scrutinizer ignore-unused */ string $key = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
    {
178
        $this->events->fire($event);
179
180
        return $this;
181
    }
182
}
183