Skip to content

Commit

Permalink
Implemented suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Galvao da Gama committed Jul 17, 2020
1 parent 377ed53 commit 193765d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class GetAssetIdsByContentField implements GetAssetIdsByContentFieldApiInterface
{
/**
* @var GetAssetIdsByContentFieldInterface[]
* @var array
*/
private $fieldHandlers;

Expand All @@ -26,7 +26,7 @@ class GetAssetIdsByContentField implements GetAssetIdsByContentFieldApiInterface
*
* @param array $fieldHandlers
*/
public function __construct($fieldHandlers = [])
public function __construct(array $fieldHandlers = [])
{
$this->fieldHandlers = $fieldHandlers;
}
Expand All @@ -40,10 +40,10 @@ public function execute(string $field, string $value): array
throw new InvalidArgumentException(__('The field argument is invalid.'));
}
$ids = [];
/** @var GetAssetIdsByContentFieldInterface $fieldHandlers */
foreach ($this->fieldHandlers[$field] as $fieldHandlers) {
/** @var GetAssetIdsByContentFieldInterface $fieldHandler */
foreach ($this->fieldHandlers[$field] as $fieldHandler) {
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$ids = array_merge($ids, $fieldHandlers->execute($value));
$ids = array_merge($ids, $fieldHandler->execute($value));
}
return array_unique($ids);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\MediaContentCatalog\Model\ResourceModel;

use Magento\Catalog\Api\CategoryManagementInterface;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;
Expand All @@ -22,6 +23,7 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
private const TABLE_CONTENT_ASSET = 'media_content_asset';
private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity';
private const ENTITY_TYPE = 'catalog_category';
private const ID_COLUMN = 'entity_id';

/**
* @var ResourceConnection
Expand All @@ -38,21 +40,29 @@ class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
*/
private $storeGroupRepository;

/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;

/**
* GetAssetIdsByCategoryStore constructor.
*
* @param ResourceConnection $resource
* @param StoreRepositoryInterface $storeRepository
* @param GroupRepositoryInterface $storeGroupRepository
* @param CategoryRepositoryInterface $categoryRepository
*/
public function __construct(
ResourceConnection $resource,
StoreRepositoryInterface $storeRepository,
GroupRepositoryInterface $storeGroupRepository
GroupRepositoryInterface $storeGroupRepository,
CategoryRepositoryInterface $categoryRepository
) {
$this->connection = $resource;
$this->storeRepository = $storeRepository;
$this->storeGroupRepository = $storeGroupRepository;
$this->categoryRepository = $categoryRepository;
}

/**
Expand All @@ -62,66 +72,23 @@ public function execute(string $value): array
{
$storeView = $this->storeRepository->getById($value);
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
$categoryIds = $this->getCategoryIdsByRootCategory((int) $storeGroup->getRootCategoryId());
$rootCategory = $this->categoryRepository->get($storeGroup->getRootCategoryId());

$sql = $this->connection->getConnection()->select()->from(
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
['asset_id']
)->joinInner(
['category_table' => $this->connection->getTableName(self::TABLE_CATALOG_CATEGORY)],
'asset_content_table.entity_id = category_table.' . self::ID_COLUMN,
[]
)->where(
'entity_type = ?',
self::ENTITY_TYPE
)->where(
'entity_id IN (?)',
$categoryIds
'path LIKE ?',
$rootCategory->getPath() . '%'
);

return $this->connection->getConnection()->fetchCol($sql);
}

/**
* This function returns an array of category ids that have content and are under the root parameter
*
* @param int $rootCategoryId
* @return array
*/
private function getCategoryIdsByRootCategory(int $rootCategoryId): array
{
$result = $this->getCategoryIdsAndPath();

$result = array_filter($result, function ($item) use ($rootCategoryId) {
$pathArray = explode('/', $item['path']);
$isInPath = false;
foreach ($pathArray as $id) {
if ($id == $rootCategoryId) {
$isInPath = true;
}
}
return $isInPath;
});

return array_map(function ($item) {
return $item['entity_id'];
}, $result);
}

/**
* This function returns an array of category_id and path of categories that have content
*
* @return array
*/
private function getCategoryIdsAndPath(): array
{
$contentCategoriesSql = $this->connection->getConnection()->select()->from(
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
['entity_id']
)->where(
'entity_type = ?',
self::ENTITY_TYPE
)->joinInner(
['category_table' => $this->connection->getTableName(self::TABLE_CATALOG_CATEGORY)],
'asset_content_table.entity_id = category_table.entity_id',
['path']
);

return $this->connection->getConnection()->fetchAll($contentCategoriesSql);
}
}

0 comments on commit 193765d

Please sign in to comment.