Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetAssetIdByContentFieldInterface and implementations #29058

Merged
Merged
Show file tree
Hide file tree
Changes from 18 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
1 change: 1 addition & 0 deletions app/code/Magento/MediaContent/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<preference for="Magento\MediaContentApi\Api\Data\ContentIdentityInterface" type="Magento\MediaContent\Model\ContentIdentity"/>
<preference for="Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface" type="Magento\MediaContent\Model\ContentAssetLink"/>
<preference for="Magento\MediaContentApi\Model\SearchPatternConfigInterface" type="Magento\MediaContent\Model\Content\SearchPatternConfig"/>
<preference for="Magento\MediaContentApi\Api\GetAssetIdsByContentFieldInterface" type="Magento\MediaContentApi\Model\Composite\GetAssetIdsByContentField"/>
<type name="Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface">
<plugin name="remove_media_content_after_asset_is_removed_by_path" type="Magento\MediaContent\Plugin\MediaGalleryAssetDeleteByPath" />
</type>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentApi\Api;

use Magento\Framework\Exception\InvalidArgumentException;

/**
* Interface used to return Asset id by content field.
*/
interface GetAssetIdsByContentFieldInterface
{
/**
* This function returns asset ids by content field
*
* @param string $field
* @param string $value
* @throws InvalidArgumentException
* @return int[]
*/
public function execute(string $field, string $value): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentApi\Model\Composite;

use Magento\Framework\Exception\InvalidArgumentException;
use Magento\MediaContentApi\Api\GetAssetIdsByContentFieldInterface as GetAssetIdsByContentFieldApiInterface;
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;

/**
* Class responsible to return Asset ids by content field
*/
class GetAssetIdsByContentField implements GetAssetIdsByContentFieldApiInterface
{
/**
* @var GetAssetIdsByContentFieldInterface[]
*/
private $fieldHandlers;

/**
* GetAssetIdsByContentField constructor.
*
* @param array $fieldHandlers
*/
public function __construct($fieldHandlers = [])
{
$this->fieldHandlers = $fieldHandlers;
}

/**
* @inheritDoc
*/
public function execute(string $field, string $value): array
{
if (!array_key_exists($field, $this->fieldHandlers)) {
throw new InvalidArgumentException(__('The field argument is invalid.'));
}
$ids = [];
/** @var GetAssetIdsByContentFieldInterface $fieldHandlers */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary annotation (the type is already declared on property)

Copy link
Contributor Author

@gabrieldagama gabrieldagama Jul 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will actually update the property, that is not correct, it is a multidimensional array, instead of an array of GetAssetIdsByContentFieldInterface.

foreach ($this->fieldHandlers[$field] as $fieldHandlers) {
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$ids = array_merge($ids, $fieldHandlers->execute($value));
}
return array_unique($ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentApi\Model;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Interface used to return Asset id by content field.
*/
interface GetAssetIdsByContentFieldInterface
{
/**
* This function returns asset ids by content field
*
* @param string $value
* @return int[]
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function execute(string $value): array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCatalog\Model\ResourceModel;

use Magento\Catalog\Api\CategoryManagementInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;
use Magento\Store\Api\GroupRepositoryInterface;
use Magento\Store\Api\StoreRepositoryInterface;

/**
* Class responsible to return Asset id by category store
*/
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';

/**
* @var ResourceConnection
*/
private $connection;

/**
* @var StoreRepositoryInterface
*/
private $storeRepository;

/**
* @var GroupRepositoryInterface
*/
private $storeGroupRepository;

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

/**
* @inheritDoc
*/
public function execute(string $value): array
{
$storeView = $this->storeRepository->getById($value);
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
$categoryIds = $this->getCategoryIdsByRootCategory((int) $storeGroup->getRootCategoryId());
$sql = $this->connection->getConnection()->select()->from(
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
['asset_id']
)->where(
'entity_type = ?',
self::ENTITY_TYPE
)->where(
'entity_id IN (?)',
$categoryIds
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we simplify the algorithm and abovid additional query to retrieve the category ids?

Suggested change
)->where(
'entity_id IN (?)',
$categoryIds
);
)->where(
'path like ?',
$storeGroup->getRootCategoryId() . '%'
);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch, I guess it looks way better now, thanks!


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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentCatalog\Model\ResourceModel;

use Magento\Eav\Model\Config;
use Magento\Framework\App\ResourceConnection;
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;

/**
* Class responsible to return Asset id by eav content field
*/
class GetAssetIdsByEavContentField implements GetAssetIdsByContentFieldInterface
{
private const TABLE_CONTENT_ASSET = 'media_content_asset';

/**
* @var ResourceConnection
*/
private $connection;

/**
* @var Config
*/
private $config;

/**
* @var string
*/
private $attributeCode;

/**
* @var string
*/
private $entityType;

/**
* @var string
*/
private $entityTable;

/**
* @var array
*/
private $valueMap;

/**
* GetAssetIdsByEavContentField constructor.
*
* @param ResourceConnection $resource
* @param Config $config
* @param string $attributeCode
* @param string $entityType
* @param string $entityTable
* @param array $valueMap
*/
public function __construct(
ResourceConnection $resource,
Config $config,
string $attributeCode,
string $entityType,
string $entityTable,
array $valueMap = []
) {
$this->connection = $resource;
$this->config = $config;
$this->attributeCode = $attributeCode;
$this->entityType = $entityType;
$this->entityTable = $entityTable;
$this->valueMap = $valueMap;
}

/**
* @inheritDoc
*/
public function execute(string $value): array
{
$attribute = $this->config->getAttribute($this->entityType, $this->attributeCode);

$sql = $this->connection->getConnection()->select()->from(
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
['asset_id']
)->where(
'entity_type = ?',
$this->entityType
)->joinInner(
['entity_table' => $this->connection->getTableName($this->entityTable)],
'asset_content_table.entity_id = entity_table.entity_id',
[]
)->joinInner(
['entity_eav_type' => $this->connection->getTableName($attribute->getBackendTable())],
'entity_table.' . $attribute->getEntityIdField() . ' = entity_eav_type.' . $attribute->getEntityIdField() .
' AND entity_eav_type.attribute_id = ' . $attribute->getAttributeId(),
[]
)->where(
'entity_eav_type.value = ?',
$this->getValueFromMap($value)
);

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

/**
* Get a value from a value map
*
* @param string $value
* @return string
*/
private function getValueFromMap(string $value): string
{
return $this->valueMap[$value] ?? $value;
}
}
Loading