Skip to content

Commit e3471b8

Browse files
authored
ENGCOM-7858: Added GetAssetIdByContentFieldInterface and implementations #29058
2 parents 2841088 + 3787390 commit e3471b8

File tree

15 files changed

+944
-0
lines changed

15 files changed

+944
-0
lines changed

app/code/Magento/MediaContent/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<preference for="Magento\MediaContentApi\Api\Data\ContentIdentityInterface" type="Magento\MediaContent\Model\ContentIdentity"/>
1717
<preference for="Magento\MediaContentApi\Api\Data\ContentAssetLinkInterface" type="Magento\MediaContent\Model\ContentAssetLink"/>
1818
<preference for="Magento\MediaContentApi\Model\SearchPatternConfigInterface" type="Magento\MediaContent\Model\Content\SearchPatternConfig"/>
19+
<preference for="Magento\MediaContentApi\Api\GetAssetIdsByContentFieldInterface" type="Magento\MediaContentApi\Model\Composite\GetAssetIdsByContentField"/>
1920
<type name="Magento\MediaGalleryApi\Api\DeleteAssetsByPathsInterface">
2021
<plugin name="remove_media_content_after_asset_is_removed_by_path" type="Magento\MediaContent\Plugin\MediaGalleryAssetDeleteByPath" />
2122
</type>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentApi\Api;
9+
10+
use Magento\Framework\Exception\InvalidArgumentException;
11+
12+
/**
13+
* Interface used to return Asset id by content field.
14+
*/
15+
interface GetAssetIdsByContentFieldInterface
16+
{
17+
/**
18+
* This function returns asset ids by content field
19+
*
20+
* @param string $field
21+
* @param string $value
22+
* @throws InvalidArgumentException
23+
* @return int[]
24+
*/
25+
public function execute(string $field, string $value): array;
26+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentApi\Model\Composite;
9+
10+
use Magento\Framework\Exception\InvalidArgumentException;
11+
use Magento\MediaContentApi\Api\GetAssetIdsByContentFieldInterface as GetAssetIdsByContentFieldApiInterface;
12+
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;
13+
14+
/**
15+
* Class responsible to return Asset ids by content field
16+
*/
17+
class GetAssetIdsByContentField implements GetAssetIdsByContentFieldApiInterface
18+
{
19+
/**
20+
* @var array
21+
*/
22+
private $fieldHandlers;
23+
24+
/**
25+
* GetAssetIdsByContentField constructor.
26+
*
27+
* @param array $fieldHandlers
28+
*/
29+
public function __construct(array $fieldHandlers = [])
30+
{
31+
$this->fieldHandlers = $fieldHandlers;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function execute(string $field, string $value): array
38+
{
39+
if (!array_key_exists($field, $this->fieldHandlers)) {
40+
throw new InvalidArgumentException(__('The field argument is invalid.'));
41+
}
42+
$ids = [];
43+
/** @var GetAssetIdsByContentFieldInterface $fieldHandler */
44+
foreach ($this->fieldHandlers[$field] as $fieldHandler) {
45+
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
46+
$ids = array_merge($ids, $fieldHandler->execute($value));
47+
}
48+
return array_unique($ids);
49+
}
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentApi\Model;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\Exception\NoSuchEntityException;
12+
13+
/**
14+
* Interface used to return Asset id by content field.
15+
*/
16+
interface GetAssetIdsByContentFieldInterface
17+
{
18+
/**
19+
* This function returns asset ids by content field
20+
*
21+
* @param string $value
22+
* @return int[]
23+
* @throws LocalizedException
24+
* @throws NoSuchEntityException
25+
*/
26+
public function execute(string $value): array;
27+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentCatalog\Model\ResourceModel;
9+
10+
use Magento\Catalog\Api\CategoryManagementInterface;
11+
use Magento\Catalog\Api\CategoryRepositoryInterface;
12+
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;
16+
use Magento\Store\Api\GroupRepositoryInterface;
17+
use Magento\Store\Api\StoreRepositoryInterface;
18+
19+
/**
20+
* Class responsible to return Asset id by category store
21+
*/
22+
class GetAssetIdsByCategoryStore implements GetAssetIdsByContentFieldInterface
23+
{
24+
private const TABLE_CONTENT_ASSET = 'media_content_asset';
25+
private const TABLE_CATALOG_CATEGORY = 'catalog_category_entity';
26+
private const ENTITY_TYPE = 'catalog_category';
27+
private const ID_COLUMN = 'entity_id';
28+
29+
/**
30+
* @var ResourceConnection
31+
*/
32+
private $connection;
33+
34+
/**
35+
* @var StoreRepositoryInterface
36+
*/
37+
private $storeRepository;
38+
39+
/**
40+
* @var GroupRepositoryInterface
41+
*/
42+
private $storeGroupRepository;
43+
44+
/**
45+
* @var CategoryRepositoryInterface
46+
*/
47+
private $categoryRepository;
48+
49+
/**
50+
* GetAssetIdsByCategoryStore constructor.
51+
*
52+
* @param ResourceConnection $resource
53+
* @param StoreRepositoryInterface $storeRepository
54+
* @param GroupRepositoryInterface $storeGroupRepository
55+
* @param CategoryRepositoryInterface $categoryRepository
56+
*/
57+
public function __construct(
58+
ResourceConnection $resource,
59+
StoreRepositoryInterface $storeRepository,
60+
GroupRepositoryInterface $storeGroupRepository,
61+
CategoryRepositoryInterface $categoryRepository
62+
) {
63+
$this->connection = $resource;
64+
$this->storeRepository = $storeRepository;
65+
$this->storeGroupRepository = $storeGroupRepository;
66+
$this->categoryRepository = $categoryRepository;
67+
}
68+
69+
/**
70+
* @inheritDoc
71+
*/
72+
public function execute(string $value): array
73+
{
74+
try {
75+
$storeView = $this->storeRepository->getById($value);
76+
$storeGroup = $this->storeGroupRepository->get($storeView->getStoreGroupId());
77+
$rootCategory = $this->categoryRepository->get($storeGroup->getRootCategoryId());
78+
} catch (NoSuchEntityException $exception) {
79+
return [];
80+
}
81+
82+
$sql = $this->connection->getConnection()->select()->from(
83+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
84+
['asset_id']
85+
)->joinInner(
86+
['category_table' => $this->connection->getTableName(self::TABLE_CATALOG_CATEGORY)],
87+
'asset_content_table.entity_id = category_table.' . self::ID_COLUMN,
88+
[]
89+
)->where(
90+
'entity_type = ?',
91+
self::ENTITY_TYPE
92+
)->where(
93+
'path LIKE ?',
94+
$rootCategory->getPath() . '%'
95+
);
96+
97+
return $this->connection->getConnection()->fetchCol($sql);
98+
}
99+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\MediaContentCatalog\Model\ResourceModel;
9+
10+
use Magento\Eav\Model\Config;
11+
use Magento\Framework\App\ResourceConnection;
12+
use Magento\MediaContentApi\Model\GetAssetIdsByContentFieldInterface;
13+
14+
/**
15+
* Class responsible to return Asset id by eav content field
16+
*/
17+
class GetAssetIdsByEavContentField implements GetAssetIdsByContentFieldInterface
18+
{
19+
private const TABLE_CONTENT_ASSET = 'media_content_asset';
20+
21+
/**
22+
* @var ResourceConnection
23+
*/
24+
private $connection;
25+
26+
/**
27+
* @var Config
28+
*/
29+
private $config;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $attributeCode;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $entityType;
40+
41+
/**
42+
* @var string
43+
*/
44+
private $entityTable;
45+
46+
/**
47+
* @var array
48+
*/
49+
private $valueMap;
50+
51+
/**
52+
* GetAssetIdsByEavContentField constructor.
53+
*
54+
* @param ResourceConnection $resource
55+
* @param Config $config
56+
* @param string $attributeCode
57+
* @param string $entityType
58+
* @param string $entityTable
59+
* @param array $valueMap
60+
*/
61+
public function __construct(
62+
ResourceConnection $resource,
63+
Config $config,
64+
string $attributeCode,
65+
string $entityType,
66+
string $entityTable,
67+
array $valueMap = []
68+
) {
69+
$this->connection = $resource;
70+
$this->config = $config;
71+
$this->attributeCode = $attributeCode;
72+
$this->entityType = $entityType;
73+
$this->entityTable = $entityTable;
74+
$this->valueMap = $valueMap;
75+
}
76+
77+
/**
78+
* @inheritDoc
79+
*/
80+
public function execute(string $value): array
81+
{
82+
$attribute = $this->config->getAttribute($this->entityType, $this->attributeCode);
83+
84+
$sql = $this->connection->getConnection()->select()->from(
85+
['asset_content_table' => $this->connection->getTableName(self::TABLE_CONTENT_ASSET)],
86+
['asset_id']
87+
)->where(
88+
'entity_type = ?',
89+
$this->entityType
90+
)->joinInner(
91+
['entity_table' => $this->connection->getTableName($this->entityTable)],
92+
'asset_content_table.entity_id = entity_table.entity_id',
93+
[]
94+
)->joinInner(
95+
['entity_eav_type' => $this->connection->getTableName($attribute->getBackendTable())],
96+
'entity_table.' . $attribute->getEntityIdField() . ' = entity_eav_type.' . $attribute->getEntityIdField() .
97+
' AND entity_eav_type.attribute_id = ' . $attribute->getAttributeId(),
98+
[]
99+
)->where(
100+
'entity_eav_type.value = ?',
101+
$this->getValueFromMap($value)
102+
);
103+
104+
return $this->connection->getConnection()->fetchCol($sql);
105+
}
106+
107+
/**
108+
* Get a value from a value map
109+
*
110+
* @param string $value
111+
* @return string
112+
*/
113+
private function getValueFromMap(string $value): string
114+
{
115+
return $this->valueMap[$value] ?? $value;
116+
}
117+
}

0 commit comments

Comments
 (0)