Completed
Pull Request — final (#299)
by Georges
03:16
created

StandardPsr6StructureTrait::setItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Core;
16
17
use phpFastCache\Cache\ExtendedCacheItemInterface;
18
use phpFastCache\CacheManager;
19
use Psr\Cache\CacheItemInterface;
20
21
/**
22
 * Trait StandardPsr6StructureTrait
23
 * @package phpFastCache\Core
24
 */
25
trait StandardPsr6StructureTrait
26
{
27
    use ClassNamespaceResolverTrait;
28
29
    /**
30
     * @var array
31
     */
32
    protected $deferredList = [];
33
34
    /**
35
     * @var ExtendedCacheItemInterface[]
36
     */
37
    protected $itemInstances = [];
38
39
    /**
40
     * @param string $key
41
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface
42
     * @throws \InvalidArgumentException
43
     */
44
    public function getItem($key)
45
    {
46
        if (is_string($key)) {
47
            if (!array_key_exists($key, $this->itemInstances)) {
48
49
                /**
50
                 * @var $item ExtendedCacheItemInterface
51
                 */
52
                CacheManager::$ReadHits++;
53
                $class = new \ReflectionClass((new \ReflectionObject($this))->getNamespaceName() . '\Item');
54
                $item = $class->newInstanceArgs([$this, $key]);
55
                $driverArray = $this->driverRead($item);
56
57
                if ($driverArray) {
58
                    $item->set($this->driverUnwrapData($driverArray));
59
                    $item->expiresAt($this->driverUnwrapTime($driverArray));
60
                    $item->setTags($this->driverUnwrapTags($driverArray));
61
                    if ($item->isExpired()) {
62
                        /**
63
                         * Using driverDelete() instead of delete()
64
                         * to avoid infinite loop caused by
65
                         * getItem() call in delete() method
66
                         */
67
                        $this->driverDelete($item);
68
                    } else {
69
                        $item->setHit(true);
70
                    }
71
                }
72
73
            }
74
        } else {
75
            throw new \InvalidArgumentException(sprintf('$key must be a string, got type "%s" instead.', gettype($key)));
76
        }
77
78
        return $this->itemInstances[ $key ];
79
    }
80
81
    /**
82
     * @param \Psr\Cache\CacheItemInterface $item
83
     * @return $this
84
     * @throws \InvalidArgumentException
85
     */
86
    public function setItem(CacheItemInterface $item)
87
    {
88
        if ($this->getClassNamespace() . '\\Item' === get_class($item)) {
89
            $this->itemInstances[ $item->getKey() ] = $item;
90
91
            return $this;
92
        } else {
93
            throw new \InvalidArgumentException(sprintf('Invalid Item Class "%s" for this driver.', get_class($item)));
94
        }
95
    }
96
97
    /**
98
     * @param array $keys
99
     * @return CacheItemInterface[]
100
     * @throws \InvalidArgumentException
101
     */
102
    public function getItems(array $keys = [])
103
    {
104
        $collection = [];
105
        foreach ($keys as $key) {
106
            $collection[ $key ] = $this->getItem($key);
107
        }
108
109
        return $collection;
110
    }
111
112
    /**
113
     * @param string $key
114
     * @return bool
115
     * @throws \InvalidArgumentException
116
     */
117
    public function hasItem($key)
118
    {
119
        CacheManager::$ReadHits++;
120
121
        return $this->getItem($key)->isHit();
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function clear()
128
    {
129
        CacheManager::$WriteHits++;
130
        $this->itemInstances = [];
131
132
        return $this->driverClear();
133
    }
134
135
    /**
136
     * @param string $key
137
     * @return bool
138
     * @throws \InvalidArgumentException
139
     */
140
    public function deleteItem($key)
141
    {
142
        $item = $this->getItem($key);
143
        if ($this->hasItem($key) && $this->driverDelete($item)) {
144
            $item->setHit(false);
145
            CacheManager::$WriteHits++;
146
            unset($this->itemInstances[ $key ]);
147
148
            return true;
149
        }
150
151
        return false;
152
    }
153
154
    /**
155
     * @param array $keys
156
     * @return bool
157
     * @throws \InvalidArgumentException
158
     */
159
    public function deleteItems(array $keys)
160
    {
161
        $return = null;
162
        foreach ($keys as $key) {
163
            $result = $this->deleteItem($key);
164
            if ($result !== false) {
165
                $return = $result;
166
            }
167
        }
168
169
        return (bool) $return;
170
    }
171
172
    /**
173
     * @param \Psr\Cache\CacheItemInterface $item
174
     * @return mixed
175
     * @throws \InvalidArgumentException
176
     */
177
    public function save(CacheItemInterface $item)
178
    {
179
        /**
180
         * @var ExtendedCacheItemInterface $item
181
         */
182
        if (!array_key_exists($item->getKey(), $this->itemInstances)) {
183
            $this->itemInstances[ $item->getKey() ] = $item;
184
        }
185
        if ($this->driverWrite($item) && $this->driverWriteTags($item)) {
186
            $item->setHit(true);
187
            CacheManager::$WriteHits++;
188
189
            return true;
190
        }
191
192
        return false;
193
    }
194
195
    /**
196
     * @param \Psr\Cache\CacheItemInterface $item
197
     * @return \Psr\Cache\CacheItemInterface
198
     */
199
    public function saveDeferred(CacheItemInterface $item)
200
    {
201
        if (!array_key_exists($item->getKey(), $this->itemInstances)) {
202
            $this->itemInstances[ $item->getKey() ] = $item;
203
        }
204
205
        return $this->deferredList[ $item->getKey() ] = $item;
206
    }
207
208
    /**
209
     * @return mixed|null
210
     * @throws \InvalidArgumentException
211
     */
212
    public function commit()
213
    {
214
        $return = null;
215
        foreach ($this->deferredList as $key => $item) {
216
            $result = $this->save($item);
217
            if ($return !== false) {
218
                unset($this->deferredList[ $key ]);
219
                $return = $result;
220
            }
221
        }
222
223
        return (bool) $return;
224
    }
225
}