Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
NullDriver
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 delete
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 deleteMultiple
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 flush
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 expiresAt
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/**
3 * Banker
4 *
5 * A Caching library implementing psr/cache (PSR 6) and psr/simple-cache (PSR 16)
6 *
7 * PHP version 8+
8 *
9 * @package     Banker
10 * @author      Timothy J. Warren <tim@timshomepage.net>
11 * @copyright   2016 - 2023  Timothy J. Warren
12 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13 * @version     4.1.0
14 * @link        https://git.timshomepage.net/timw4mail/banker
15 */
16namespace Aviat\Banker\Driver;
17
18use DateInterval;
19
20/**
21 * Cache backend for use without a cache server. Only does transient
22 * in-memory caching
23 */
24class NullDriver extends AbstractDriver {
25
26    /**
27     * In memory store
28     *
29     * @var array
30     */
31    protected array $store = [];
32
33    /**
34     * NullDriver constructor
35     */
36    public function __construct()
37    {
38        $this->store = [];
39    }
40
41    /**
42     * See if a key currently exists in the cache
43     *
44     * @param string $key
45     * @return bool
46     */
47    public function exists(string $key): bool
48    {
49        return array_key_exists($key, $this->store);
50    }
51
52    /**
53     * Get the value for the selected cache key
54     *
55     * @param string $key
56     * @return mixed
57     */
58    public function get(string $key): mixed
59    {
60        return $this->exists($key)
61            ? $this->store[$key]
62            : NULL;
63    }
64
65    /**
66     * Set a cached value
67     *
68     * @param string $key
69     * @param mixed $value
70     * @param DateInterval|int|null $expires
71     * @return bool
72     */
73    public function set(string $key, mixed $value, DateInterval|int|null $expires = NULL): bool
74    {
75        $this->store[$key] = $value;
76        return $this->store[$key] === $value;
77    }
78
79    /**
80     * Remove an item from the cache
81     *
82     * @param string $key
83     * @return boolean
84     */
85    public function delete(string $key): bool
86    {
87        // Don't return true if the key didn't exist to begin with
88        if ( ! array_key_exists($key, $this->store))
89        {
90            return FALSE;
91        }
92
93        unset($this->store[$key]);
94        return ( ! array_key_exists($key, $this->store));
95    }
96
97    /**
98     * Remove multiple items from the cache
99     *
100     * @param string[] $keys
101     * @return boolean
102     */
103    public function deleteMultiple(array $keys = []): bool
104    {
105        $this->validateKeys($keys);
106
107        $res = TRUE;
108
109        foreach($keys as $key)
110        {
111            $res = $res && $this->delete($key);
112        }
113
114        return $res;
115    }
116
117    /**
118     * Empty the cache
119     *
120     * @return boolean
121     */
122    public function flush(): bool
123    {
124        $this->store = [];
125        return TRUE;
126    }
127
128    /**
129     * Set the specified key to expire at the given time
130     *
131     * @param string $key
132     * @param int $expires
133     * @return boolean
134     */
135    public function expiresAt(string $key, int $expires): bool
136    {
137        //noop
138        return array_key_exists($key, $this->store);
139    }
140}