Skip to content

Commit d985c50

Browse files
authored
The inner cacheItem should always be encrypted (#135)
* The inner cacheItem should always be encrypted * cs
1 parent 3344529 commit d985c50

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

EncryptedCachePool.php

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,9 @@ public function __construct(PhpCachePool $cachePool, Key $key)
4949
*/
5050
public function getItem($key)
5151
{
52-
$item = $this->cachePool->getItem($key);
52+
$items = $this->getItems([$key]);
5353

54-
if (!($item instanceof EncryptedItemDecorator)) {
55-
return new EncryptedItemDecorator($item, $this->key);
56-
}
57-
58-
return $item;
54+
return reset($items);
5955
}
6056

6157
/**
@@ -109,31 +105,23 @@ public function deleteItems(array $keys)
109105
*/
110106
public function save(CacheItemInterface $item)
111107
{
112-
if (!$item instanceof PhpCacheItem) {
113-
throw new InvalidArgumentException('Cache items are not transferable between pools. Item MUST implement PhpCacheItem.');
114-
}
115-
116-
if (!($item instanceof EncryptedItemDecorator)) {
117-
$item = new EncryptedItemDecorator($item, $this->key);
108+
if (!$item instanceof EncryptedItemDecorator) {
109+
throw new InvalidArgumentException('Cache items are not transferable between pools. Item MUST implement EncryptedItemDecorator.');
118110
}
119111

120-
return $this->cachePool->save($item);
112+
return $this->cachePool->save($item->getCacheItem());
121113
}
122114

123115
/**
124116
* {@inheritdoc}
125117
*/
126118
public function saveDeferred(CacheItemInterface $item)
127119
{
128-
if (!$item instanceof PhpCacheItem) {
129-
throw new InvalidArgumentException('Cache items are not transferable between pools. Item MUST implement PhpCacheItem.');
130-
}
131-
132-
if (!($item instanceof EncryptedItemDecorator)) {
133-
$item = new EncryptedItemDecorator($item, $this->key);
120+
if (!$item instanceof EncryptedItemDecorator) {
121+
throw new InvalidArgumentException('Cache items are not transferable between pools. Item MUST implement EncryptedItemDecorator.');
134122
}
135123

136-
return $this->cachePool->saveDeferred($item);
124+
return $this->cachePool->saveDeferred($item->getCacheItem());
137125
}
138126

139127
/**

EncryptedItemDecorator.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
class EncryptedItemDecorator implements PhpCacheItem
2525
{
2626
/**
27+
* The cacheItem should always contain encrypted data.
28+
*
2729
* @type PhpCacheItem
2830
*/
2931
private $cacheItem;
@@ -51,6 +53,14 @@ public function getKey()
5153
return $this->cacheItem->getKey();
5254
}
5355

56+
/**
57+
* @return PhpCacheItem
58+
*/
59+
public function getCacheItem()
60+
{
61+
return $this->cacheItem;
62+
}
63+
5464
/**
5565
* {@inheritdoc}
5666
*/
@@ -176,9 +186,9 @@ private function transform(array $item)
176186
}
177187

178188
/**
179-
* @internal This function should never be used and considered private.
189+
* Move tags from $tags to $prevTags.
180190
*
181-
* Move tags from $tags to $prevTags
191+
* @internal This function should never be used and considered private.
182192
*/
183193
public function moveTagsToPrevious()
184194
{

0 commit comments

Comments
 (0)