Completed
Push — master ( 628328...e6a348 )
by Martin
8s
created

MemoryCacheItemPool::commit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
namespace Psr6NullCache\Adapter;
3
4
use Psr\Cache\CacheItemPoolInterface;
5
use Psr\Cache\CacheItemInterface;
6
use DateTime;
7
use Psr6NullCache\CacheItem;
8
9
final class MemoryCacheItemPool implements CacheItemPoolInterface
10
{
11
12
    /**
13
     *
14
     * @var array
15
     */
16
    private $data = [];
17
18
    private $deferred = [];
19
20
    /**
21
     * Returns a Cache Item representing the specified key.
22
     *
23
     * This method must always return a CacheItemInterface object, even in case of
24
     * a cache miss. It MUST NOT return null.
25
     *
26
     * @param string $key
27
     *            The key for which to return the corresponding Cache Item.
28
     *            
29
     * @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
30
     *         MUST be thrown.
31
     *        
32
     * @return CacheItemInterface
33
     */
34
    public function getItem($key)
35
    {
36
        if ($this->hasItem($key) !== true) {
37
            $this->data[$key] = new CacheItem($key, null, false);
38
        }
39
        
40
        return $this->data[$key];
41
    }
42
43
    /**
44
     * Returns a traversable set of cache items.
45
     *
46
     * @param array $keys
47
     *            An indexed array of keys of items to retrieve.
48
     *            
49
     * @throws InvalidArgumentException If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
50
     *         MUST be thrown.
51
     *        
52
     * @return array|\Traversable A traversable collection of Cache Items keyed by the cache keys of
53
     *         each item. A Cache item will be returned for each key, even if that
54
     *         key is not found. However, if no keys are specified then an empty
55
     *         traversable MUST be returned instead.
56
     */
57 View Code Duplication
    public function getItems(array $keys = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
58
    {
59
        $result = [];
60
        
61
        foreach ($keys as $key) {
62
            $result[$key] = $this->getItem($key);
63
        }
64
        
65
        return $result;
66
    }
67
68
    /**
69
     * Confirms if the cache contains specified cache item.
70
     *
71
     * Note: This method MAY avoid retrieving the cached value for performance reasons.
72
     * This could result in a race condition with CacheItemInterface::get(). To avoid
73
     * such situation use CacheItemInterface::isHit() instead.
74
     *
75
     * @param string $key
76
     *            The key for which to check existence.
77
     *            
78
     * @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
79
     *         MUST be thrown.
80
     *        
81
     * @return bool True if item exists in the cache, false otherwise.
82
     */
83
    public function hasItem($key)
84
    {
85
        if (isset($this->data[$key])) {
86
            
87
            /* @var $item \Psr6NullCache\CacheItem */
88
            $item = $this->data[$key];
89
            
90
            if ($item->isHit() === true && ($item->getExpires() === null || $item->getExpires() > new DateTime())) {
91
                return true;
92
            }
93
        }
94
        
95
        return false;
96
    }
97
98
    /**
99
     * Deletes all items in the pool.
100
     *
101
     * @return bool True if the pool was successfully cleared. False if there was an error.
102
     */
103
    public function clear()
104
    {
105
        $this->data = [];
106
        
107
        return true;
108
    }
109
110
    /**
111
     * Removes the item from the pool.
112
     *
113
     * @param string $key
114
     *            The key for which to delete
115
     *            
116
     * @throws InvalidArgumentException If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
117
     *         MUST be thrown.
118
     *        
119
     * @return bool True if the item was successfully removed. False if there was an error.
120
     */
121
    public function deleteItem($key)
122
    {
123
        unset($this->data[$key]);
124
        
125
        return true;
126
    }
127
128
    /**
129
     * Removes multiple items from the pool.
130
     *
131
     * @param array $keys
132
     *            An array of keys that should be removed from the pool.
133
     *            
134
     * @throws InvalidArgumentException If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
135
     *         MUST be thrown.
136
     *        
137
     * @return bool True if the items were successfully removed. False if there was an error.
138
     */
139
    public function deleteItems(array $keys)
140
    {
141
        foreach ($keys as $key) {
142
            $this->deleteItem($key);
143
        }
144
        
145
        return true;
146
    }
147
148
    /**
149
     * Persists a cache item immediately.
150
     *
151
     * @param CacheItemInterface $item
152
     *            The cache item to save.
153
     *            
154
     * @return bool True if the item was successfully persisted. False if there was an error.
155
     */
156
    public function save(CacheItemInterface $item)
157
    {
158
        $item->setIsHit(true);
159
        
160
        $this->data[$item->getKey()] = $item;
161
        
162
        return true;
163
    }
164
165
    /**
166
     * Sets a cache item to be persisted later.
167
     *
168
     * @param CacheItemInterface $item
169
     *            The cache item to save.
170
     *            
171
     * @return bool False if the item could not be queued or if a commit was attempted and failed. True otherwise.
172
     */
173
    public function saveDeferred(CacheItemInterface $item)
174
    {
175
        $this->deferred[$item->getKey()] = $item;
176
        
177
        return true;
178
    }
179
180
    /**
181
     * Persists any deferred cache items.
182
     *
183
     * @return bool True if all not-yet-saved items were successfully saved or there were none. False otherwise.
184
     */
185
    public function commit()
186
    {
187
        foreach ($this->deferred as $item) {
188
            /* @var $item \Psr6NullCache\CacheItem */
189
            $this->save($item);
190
        }
191
        
192
        $this->deferred = [];
193
        
194
        return true;
195
    }
196
}
197