Skip to content

Commit 193765d

Browse files
author
Gabriel Galvao da Gama
committed
Implemented suggestions
1 parent 377ed53 commit 193765d

File tree

2 files changed

+24
-57
lines changed

2 files changed

+24
-57
lines changed

app/code/Magento/MediaContentApi/Model/Composite/GetAssetIdsByContentField.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class GetAssetIdsByContentField implements GetAssetIdsByContentFieldApiInterface
1818
{
1919
/**
20-
* @var GetAssetIdsByContentFieldInterface[]
20+
* @var array
2121
*/
2222
private $fieldHandlers;
2323

@@ -26,7 +26,7 @@ class GetAssetIdsByContentField implements GetAssetIdsByContentFieldApiInterface
2626
*
2727
* @param array $fieldHandlers
2828
*/
29-
public function __construct($fieldHandlers = [])
29+
public function __construct(array $fieldHandlers = [])
3030
{
3131
$this->fieldHandlers = $fieldHandlers;
3232
}
@@ -40,10 +40,10 @@ public function execute(string $field, string $value): array
4040
throw new InvalidArgumentException(__('The field argument is invalid.'));
4141
}
4242
$ids = [];
43-
/** @var GetAssetIdsByContentFieldInterface $fieldHandlers */
44-
foreach ($this->fieldHandlers[$field] as $fieldHandlers) {
43+
/** @var GetAssetIdsByContentFieldInterface $fieldHandler */
44+
foreach ($this->fieldHandlers[$field] as $fieldHandler) {
4545
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
46-
$ids = array_merge($ids, $fieldHandlers->execute($value));
46+
$ids = array_merge($ids, $fieldHandler->execute($value));
4747
}
4848
return array_unique($ids);
4949
}

app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetAssetIdsByCategoryStore.php

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\MediaContentCatalog\Model\ResourceModel;
99

1010
use Magento\Catalog\Api\CategoryManagementInterface;
11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
1112
use Magento\Framework\App\ResourceConnection;
1213
use Magento\Framework\Exception\LocalizedException;
1314
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;
@@ -22,6 +23,7 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
2223
private const TABLE_CONTENT_ASSET = 'media_content_asset';
2324
private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity';
2425
private const ENTITY_TYPE = 'catalog_category';
26+
private const ID_COLUMN = 'entity_id';
2527

2628
/**
2729
* @var ResourceConnection
@@ -38,21 +40,29 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
3840
*/
3941
private $storeGroupRepository;
4042

43+
/**
44+
* @var CategoryRepositoryInterface
45+
*/
46+
private $categoryRepository;
47+
4148
/**
4249
* GetAssetIdsByCategoryStore constructor.
4350
*
4451
* @param ResourceConnection $resource
4552
* @param StoreRepositoryInterface $storeRepository
4653
* @param GroupRepositoryInterface $storeGroupRepository
54+
* @param CategoryRepositoryInterface $categoryRepository
4755
*/
4856
public function __construct(
4957
ResourceConnection $resource,
5058
StoreRepositoryInterface $storeRepository,
51-
GroupRepositoryInterface $storeGroupRepository
59+
GroupRepositoryInterface $storeGroupRepository,
60+
CategoryRepositoryInterface $categoryRepository
5261
) {
5362
$this->connection = $resource;
5463
$this->storeRepository = $storeRepository;
5564
$this->storeGroupRepository = $storeGroupRepository;
65+
$this->categoryRepository = $categoryRepository;
5666
}
5767

5868
/**
@@ -62,66 +72,23 @@ public function execute(string $value): array
6272
{
6373
$storeView = $this->storeRepository->getById($value);
6474
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
65-
$categoryIds = $this->getCategoryIdsByRootCategory((int) $storeGroup->getRootCategoryId());
75+
$rootCategory = $this->categoryRepository->get($storeGroup->getRootCategoryId());
76+
6677
$sql = $this->connection->getConnection()->select()->from(
6778
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
6879
['asset_id']
80+
)->joinInner(
81+
['category_table' => $this->connection->getTableName(self::TABLE_CATALOG_CATEGORY)],
82+
'asset_content_table.entity_id = category_table.' . self::ID_COLUMN,
83+
[]
6984
)->where(
7085
'entity_type = ?',
7186
self::ENTITY_TYPE
7287
)->where(
73-
'entity_id IN (?)',
74-
$categoryIds
88+
'path LIKE ?',
89+
$rootCategory->getPath() . '%'
7590
);
7691

7792
return $this->connection->getConnection()->fetchCol($sql);
7893
}
79-
80-
/**
81-
* This function returns an array of category ids that have content and are under the root parameter
82-
*
83-
* @param int $rootCategoryId
84-
* @return array
85-
*/
86-
private function getCategoryIdsByRootCategory(int $rootCategoryId): array
87-
{
88-
$result = $this->getCategoryIdsAndPath();
89-
90-
$result = array_filter($result, function ($item) use ($rootCategoryId) {
91-
$pathArray = explode('/', $item['path']);
92-
$isInPath = false;
93-
foreach ($pathArray as $id) {
94-
if ($id == $rootCategoryId) {
95-
$isInPath = true;
96-
}
97-
}
98-
return $isInPath;
99-
});
100-
101-
return array_map(function ($item) {
102-
return $item['entity_id'];
103-
}, $result);
104-
}
105-
106-
/**
107-
* This function returns an array of category_id and path of categories that have content
108-
*
109-
* @return array
110-
*/
111-
private function getCategoryIdsAndPath(): array
112-
{
113-
$contentCategoriesSql = $this->connection->getConnection()->select()->from(
114-
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
115-
['entity_id']
116-
)->where(
117-
'entity_type = ?',
118-
self::ENTITY_TYPE
119-
)->joinInner(
120-
['category_table' => $this->connection->getTableName(self::TABLE_CATALOG_CATEGORY)],
121-
'asset_content_table.entity_id = category_table.entity_id',
122-
['path']
123-
);
124-
125-
return $this->connection->getConnection()->fetchAll($contentCategoriesSql);
126-
}
12794
}

0 commit comments

Comments
 (0)