Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion app/code/Magento/Catalog/Model/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\SerializationException;
use Magento\Framework\Exception\StateException;
use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Phrase;

/**
* Repository for categories.
Expand Down Expand Up @@ -144,10 +146,12 @@ public function save(CategoryInterface $category)

/**
* @inheritdoc
*
* @throws SerializationException
*/
public function get($categoryId, $storeId = null)
{
$cacheKey = $storeId ?? 'all';
$cacheKey = $this->getScopeCacheKey($storeId);
if (!isset($this->instances[$categoryId][$cacheKey])) {
/** @var Category $category */
$category = $this->categoryFactory->create();
Expand Down Expand Up @@ -254,4 +258,26 @@ private function getMetadataPool()
}
return $this->metadataPool;
}

/**
* Returns a cache key based on scope
*
* @param string|int|null $storeId
*
* @throws SerializationException
* @return int|string
*/
private function getScopeCacheKey($storeId = null)
{
if (null !== $storeId && !is_numeric($storeId)) {
throw new SerializationException(
new Phrase(
'The "%value" value\'s type is invalid. The "%type" type was expected. '
. 'Verify and try again.',
['value' => $storeId, 'type' => 'int']
)
);
}
return $storeId === null ? 'all' : (int)$storeId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ public function testGetWhenCategoryDoesNotExist()
$this->assertEquals($categoryMock, $this->model->get($categoryId));
}

public function testGetWithStoreCodeException()
{
$categoryId = 5;
$categoryMock = $this->createMock(CategoryModel::class);
$this->expectException('\Magento\Framework\Exception\SerializationException');
$this->expectExceptionMessage(
'The "default" value\'s type is invalid. The "int" type was expected. Verify and try again.'
);
$this->assertEquals($categoryMock, $this->model->get($categoryId, 'default'));
}

/**
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Cms\Api\GetBlockByIdentifierInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\SerializationException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Api\StoreManagementInterface;
use Magento\Store\Model\StoreManagerInterface;
Expand Down Expand Up @@ -143,6 +144,17 @@ public function testGetCategoryForProvidedStore(): void
$fixtureStoreId = $this->storeManager->getStore('fixturestore')->getId();
$categorySecondStore = $this->categoryRepository->get($categoryId, $fixtureStoreId);
$this->assertSame('category-fixturestore', $categorySecondStore->getUrlKey());

$caughtException = false;
try {
$categoryRepository->get(
categoryId,
'default'
);
} catch (SerializationException $exception) {
$caughtException = true;
}
$this->assertTrue($caughtException);
}

/**
Expand Down