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 all 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 array
*/
private $fieldHandlers;

/**
* GetAssetIdsByContentField constructor.
*
* @param array $fieldHandlers
*/
public function __construct(array $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 $fieldHandler */
foreach ($this->fieldHandlers[$field] as $fieldHandler) {
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$ids = array_merge($ids, $fieldHandler->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,99 @@
<?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\Catalog\Api\CategoryRepositoryInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
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';
private const ID_COLUMN = 'entity_id';

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

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

/**
* @var GroupRepositoryInterface
*/
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,
CategoryRepositoryInterface $categoryRepository
) {
$this->connection = $resource;
$this->storeRepository = $storeRepository;
$this->storeGroupRepository = $storeGroupRepository;
$this->categoryRepository = $categoryRepository;
}

/**
* @inheritDoc
*/
public function execute(string $value): array
{
try {
$storeView = $this->storeRepository->getById($value);
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
$rootCategory = $this->categoryRepository->get($storeGroup->getRootCategoryId());
} catch (NoSuchEntityException $exception) {
return [];
}

$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(
'path LIKE ?',
$rootCategory->getPath() . '%'
);

return $this->connection->getConnection()->fetchCol($sql);
}
}
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