Skip to content

Commit 11082e7

Browse files
author
Gabriel Galvao da Gama
committed
Refactor of category store class and removed unecessary stuff
1 parent 279cf2d commit 11082e7

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

app/code/Magento/MediaContent/Model/GetAssetIdByContentFieldComposite.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function execute(string $value): array
3636
{
3737
$ids = [];
3838
foreach ($this->getAssetIdByContentFieldArray as $getAssetIdByContentField) {
39+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
3940
$ids = array_merge($ids, $getAssetIdByContentField->execute($value));
4041
}
4142
return array_unique($ids);

app/code/Magento/MediaContentCatalog/Model/GetAssetIdByCategoryStore.php

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77

88
namespace Magento\MediaContentCatalog\Model;
99

10+
use Magento\Catalog\Api\CategoryManagementInterface;
1011
use Magento\Framework\App\ResourceConnection;
1112
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\MediaContentApi\Model\GetAssetIdByContentFieldInterface;
1314
use Magento\Store\Api\GroupRepositoryInterface;
1415
use Magento\Store\Api\StoreRepositoryInterface;
1516

1617
/**
17-
* Class responsible to return Asset id by content field
18+
* Class responsible to return Asset id by category store
1819
*/
1920
class GetAssetIdByCategoryStore implements GetAssetIdByContentFieldInterface
2021
{
2122
private const TABLE_CONTENT_ASSET = 'media_content_asset';
23+
private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity';
2224

2325
/**
2426
* @var ResourceConnection
@@ -67,7 +69,7 @@ public function execute(string $value): array
6769
{
6870
$storeView = $this->storeRepository->getById($value);
6971
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
70-
$categoryIds = $this->getCategoryIdsByRootCategory($storeGroup->getRootCategoryId());
72+
$categoryIds = $this->getCategoryIdsByRootCategory((int) $storeGroup->getRootCategoryId());
7173
$sql = $this->connection->getConnection()->select()->from(
7274
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
7375
['asset_id']
@@ -86,21 +88,15 @@ public function execute(string $value): array
8688
}, $result);
8789
}
8890

89-
private function getCategoryIdsByRootCategory($rootCategoryId)
91+
/**
92+
* This function returns an array of category ids that have content and are under the root parameter
93+
*
94+
* @param $rootCategoryId
95+
* @return array
96+
*/
97+
private function getCategoryIdsByRootCategory(int $rootCategoryId): array
9098
{
91-
$contentCategoriesSql = $this->connection->getConnection()->select()->from(
92-
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
93-
['entity_id']
94-
)->where(
95-
'entity_type = ?',
96-
$this->entityType
97-
)->joinInner(
98-
['category_table' => $this->connection->getTableName('catalog_category_entity')],
99-
'asset_content_table.entity_id = category_table.entity_id',
100-
['path']
101-
);
102-
103-
$result = $this->connection->getConnection()->fetchAll($contentCategoriesSql);
99+
$result = $this->getCategoryIdsAndPath();
104100

105101
$result = array_filter($result, function ($item) use ($rootCategoryId) {
106102
$pathArray = explode("/", $item['path']);
@@ -117,4 +113,26 @@ private function getCategoryIdsByRootCategory($rootCategoryId)
117113
return $item['entity_id'];
118114
}, $result);
119115
}
116+
117+
/**
118+
* This function returns an array of category_id and path of categories that have content
119+
*
120+
* @return array
121+
*/
122+
private function getCategoryIdsAndPath(): array
123+
{
124+
$contentCategoriesSql = $this->connection->getConnection()->select()->from(
125+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
126+
['entity_id']
127+
)->where(
128+
'entity_type = ?',
129+
$this->entityType
130+
)->joinInner(
131+
['category_table' => $this->connection->getTableName(self::TABLE_CATALOG_CATEGORY)],
132+
'asset_content_table.entity_id = category_table.entity_id',
133+
['path']
134+
);
135+
136+
return $this->connection->getConnection()->fetchAll($contentCategoriesSql);
137+
}
120138
}

app/code/Magento/MediaContentCatalog/Model/GetAssetIdByProductStore.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@
1313
use Magento\Store\Api\StoreRepositoryInterface;
1414

1515
/**
16-
* Class responsible to return Asset id by content field
16+
* Class responsible to return Asset ids by product store
1717
*/
1818
class GetAssetIdByProductStore implements GetAssetIdByContentFieldInterface
1919
{
2020
private const TABLE_CONTENT_ASSET = 'media_content_asset';
21-
private const ENTITY_STOREVIEW_RELATION = 'store_view';
22-
private const ENTITY_STOREGROUP_RELATION = 'store_group';
23-
private const ENTITY_WEBSITE_RELATION = 'website';
2421

2522
/**
2623
* @var ResourceConnection

0 commit comments

Comments
 (0)