Skip to content

Commit 69946cc

Browse files
committed
Added getScopeCacheKey in order to validate the store ID and return the right cache key
1 parent 5a9fcab commit 69946cc

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
use Magento\Framework\Exception\CouldNotSaveException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\Exception\SerializationException;
1213
use Magento\Framework\Exception\StateException;
1314
use Magento\Catalog\Api\Data\CategoryInterface;
15+
use Magento\Framework\Phrase;
1416

1517
/**
1618
* Repository for categories.
@@ -128,10 +130,11 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
128130

129131
/**
130132
* @inheritdoc
133+
* @throws SerializationException
131134
*/
132135
public function get($categoryId, $storeId = null)
133136
{
134-
$cacheKey = $storeId ?? 'all';
137+
$cacheKey = $this->getScopeCacheKey($storeId);
135138
if (!isset($this->instances[$categoryId][$cacheKey])) {
136139
/** @var Category $category */
137140
$category = $this->categoryFactory->create();
@@ -238,4 +241,26 @@ private function getMetadataPool()
238241
}
239242
return $this->metadataPool;
240243
}
244+
245+
/**
246+
* Returns a cache key based on scope
247+
*
248+
* @param string|integer|null $storeId
249+
*
250+
* @throws SerializationException
251+
* @return integer
252+
*/
253+
private function getScopeCacheKey($storeId = null)
254+
{
255+
if (null !== $storeId && !is_numeric($storeId)) {
256+
throw new SerializationException(
257+
new Phrase(
258+
'The "%value" value\'s type is invalid. The "%type" type was expected. '
259+
. 'Verify and try again.',
260+
['value' => $storeId, 'type' => 'int']
261+
)
262+
);
263+
}
264+
return $storeId ?? 'all';
265+
}
241266
}

0 commit comments

Comments
 (0)