Skip to content

Commit d5cdd0b

Browse files
committed
#27536: Introduced ContentIdentity
1 parent e66c9a9 commit d5cdd0b

28 files changed

+494
-267
lines changed

app/code/Magento/Cms/etc/module.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
<module name="Magento_Store"/>
1212
<module name="Magento_Theme"/>
1313
<module name="Magento_Variable"/>
14-
<module name="Magento_MediaContent"/>
15-
<module name="Magento_MediaContentApi"/>
1614
</sequence>
1715
</module>
1816
</config>

app/code/Magento/MediaContent/Model/AssignAsset.php renamed to app/code/Magento/MediaContent/Model/AssignAssets.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Exception\CouldNotSaveException;
12-
use Magento\MediaContentApi\Api\AssignAssetInterface;
12+
use Magento\MediaContentApi\Api\AssignAssetsInterface;
13+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
1617
* Used for saving relation between the media asset and media content where the media asset is used
1718
*/
18-
class AssignAsset implements AssignAssetInterface
19+
class AssignAssets implements AssignAssetsInterface
1920
{
2021
private const MEDIA_CONTENT_ASSET_TABLE_NAME = 'media_content_asset';
2122
private const ASSET_ID = 'asset_id';
@@ -34,8 +35,6 @@ class AssignAsset implements AssignAssetInterface
3435
private $logger;
3536

3637
/**
37-
* AssignAsset constructor.
38-
*
3938
* @param ResourceConnection $resourceConnection
4039
* @param LoggerInterface $logger
4140
*/
@@ -46,20 +45,23 @@ public function __construct(ResourceConnection $resourceConnection, LoggerInterf
4645
}
4746

4847
/**
49-
* @inheritDoc
48+
* @inheritdoc
5049
*/
51-
public function execute(int $assetId, string $contentType, string $contentEntityId, string $contentField): void
50+
public function execute(ContentIdentityInterface $contentIdentity, array $assetIds): void
5251
{
5352
try {
5453
$connection = $this->resourceConnection->getConnection();
5554
$tableName = $this->resourceConnection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME);
56-
$saveData = [
57-
self::ASSET_ID => $assetId,
58-
self::TYPE => $contentType,
59-
self::ENTITY_ID => $contentEntityId,
60-
self::FIELD => $contentField
61-
];
62-
$connection->insert($tableName, $saveData);
55+
$data = [];
56+
foreach ($assetIds as $assetId) {
57+
$data[] = [
58+
self::ASSET_ID => $assetId,
59+
self::TYPE => $contentIdentity->getEntityType(),
60+
self::ENTITY_ID => $contentIdentity->getEntityId(),
61+
self::FIELD => $contentIdentity->getField()
62+
];
63+
}
64+
$connection->insertMultiple($tableName, $data);
6365
} catch (\Exception $exception) {
6466
$this->logger->critical($exception);
6567
$message = __('An error occurred while saving relation between media asset and media content.');
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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\MediaContent\Model;
9+
10+
use Magento\Framework\Model\AbstractExtensibleModel;
11+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
12+
13+
/**
14+
* @inheritdoc
15+
*/
16+
class ContentIdentity extends AbstractExtensibleModel implements ContentIdentityInterface
17+
{
18+
private const TYPE = 'entity_type';
19+
private const ENTITY_ID = 'entity_id';
20+
private const FIELD = 'field';
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function getEntityType(): string
26+
{
27+
return (string) $this->getData(self::TYPE);
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function getEntityId(): string
34+
{
35+
return (string) $this->getData(self::ENTITY_ID);
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getField(): string
42+
{
43+
return (string) $this->getData(self::FIELD);
44+
}
45+
}

app/code/Magento/MediaContent/Model/GetAssetsUsedInContent.php renamed to app/code/Magento/MediaContent/Model/GetAssetIdsUsedInContent.php

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Exception\IntegrationException;
12-
use Magento\MediaContentApi\Api\GetAssetsUsedInContentInterface;
12+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
13+
use Magento\MediaContentApi\Api\GetAssetIdsUsedInContentInterface;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
1617
* Used to return media asset id list which is used in the specified media content
1718
*/
18-
class GetAssetsUsedInContent implements GetAssetsUsedInContentInterface
19+
class GetAssetIdsUsedInContent implements GetAssetIdsUsedInContentInterface
1920
{
2021
private const MEDIA_CONTENT_ASSET_TABLE_NAME = 'media_content_asset';
2122
private const ASSET_ID = 'asset_id';
@@ -46,25 +47,26 @@ public function __construct(ResourceConnection $resourceConnection, LoggerInterf
4647
}
4748

4849
/**
49-
* @inheritDoc
50+
* @inheritdoc
5051
*/
51-
public function execute(string $contentType, string $contentEntityId = null, string $contentField = null): array
52+
public function execute(ContentIdentityInterface $contentIdentity): array
5253
{
5354
try {
5455
$connection = $this->resourceConnection->getConnection();
5556
$select = $connection->select()
5657
->from(
5758
$this->resourceConnection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME),
5859
self::ASSET_ID
59-
)->where(self::TYPE . ' = ?', $contentType);
60-
61-
if (null !== $contentEntityId) {
62-
$select = $select->where(self::ENTITY_ID . '= ?', $contentEntityId);
63-
}
64-
65-
if (null !== $contentField) {
66-
$select = $select->where(self::FIELD . '= ?', $contentField);
67-
}
60+
)->where(
61+
self::TYPE . ' = ?',
62+
$contentIdentity->getEntityType()
63+
)->where(
64+
self::ENTITY_ID . '= ?',
65+
$contentIdentity->getEntityId()
66+
)->where(
67+
self::FIELD . '= ?',
68+
$contentIdentity->getField()
69+
);
6870

6971
return $connection->fetchAssoc($select);
7072
} catch (\Exception $exception) {

app/code/Magento/MediaContent/Model/GetContentWithAsset.php renamed to app/code/Magento/MediaContent/Model/GetContentWithAssets.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Exception\IntegrationException;
12-
use Magento\MediaContentApi\Api\GetContentWithAssetInterface;
12+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
13+
use Magento\MediaContentApi\Api\GetContentWithAssetsInterface;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
1617
* Used to return media asset list for the specified asset.
1718
*/
18-
class GetContentWithAsset implements GetContentWithAssetInterface
19+
class GetContentWithAssets implements GetContentWithAssetsInterface
1920
{
2021
private const MEDIA_CONTENT_ASSET_TABLE_NAME = 'media_content_asset';
2122
private const ASSET_ID = 'asset_id';
@@ -31,33 +32,46 @@ class GetContentWithAsset implements GetContentWithAssetInterface
3132
private $logger;
3233

3334
/**
34-
* GetAssetsUsedInContent constructor.
35-
*
35+
* @var ContentIdentityInterfaceFactory
36+
*/
37+
private $factory;
38+
39+
/**
40+
* @param ContentIdentityInterfaceFactory $factory
3641
* @param ResourceConnection $resourceConnection
3742
* @param LoggerInterface $logger
3843
*/
39-
public function __construct(ResourceConnection $resourceConnection, LoggerInterface $logger)
40-
{
44+
public function __construct(
45+
ContentIdentityInterfaceFactory $factory,
46+
ResourceConnection $resourceConnection,
47+
LoggerInterface $logger
48+
) {
49+
$this->factory = $factory;
4150
$this->resourceConnection = $resourceConnection;
4251
$this->logger = $logger;
4352
}
4453

4554
/**
4655
* @inheritDoc
4756
*/
48-
public function execute(int $assetId): array
57+
public function execute(array $assetIds): array
4958
{
5059
try {
5160
$connection = $this->resourceConnection->getConnection();
5261
$select = $connection->select()
5362
->from($this->resourceConnection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME))
54-
->where(self::ASSET_ID . '= ?', $assetId);
63+
->where(self::ASSET_ID . 'IN (?)', $assetIds);
5564

56-
return $connection->fetchAssoc($select);
65+
$contentIdentities = [];
66+
foreach ($connection->fetchAssoc($select) as $contentIdentityData) {
67+
$contentIdentities[] = $this->factory->create(['data' => $contentIdentityData]);
68+
}
69+
return $contentIdentities;
5770
} catch (\Exception $exception) {
5871
$this->logger->critical($exception);
59-
$message = __('An error occurred at getting media asset to content relation by media asset id.');
60-
throw new IntegrationException($message);
72+
throw new IntegrationException(
73+
__('An error occurred at getting media asset to content relation by media asset id.')
74+
);
6175
}
6276
}
6377
}

app/code/Magento/MediaContent/Model/UnassignAsset.php renamed to app/code/Magento/MediaContent/Model/UnassignAssets.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@
99

1010
use Magento\Framework\App\ResourceConnection;
1111
use Magento\Framework\Exception\CouldNotDeleteException;
12-
use Magento\MediaContentApi\Api\UnassignAssetInterface;
12+
use Magento\MediaContentApi\Api\Data\ContentIdentityInterface;
13+
use Magento\MediaContentApi\Api\UnassignAssetsInterface;
1314
use Psr\Log\LoggerInterface;
1415

1516
/**
1617
* Used to unassign relation of the media asset to the media content where the media asset is used
1718
*/
18-
class UnassignAsset implements UnassignAssetInterface
19+
class UnassignAssets implements UnassignAssetsInterface
1920
{
2021
private const MEDIA_CONTENT_ASSET_TABLE_NAME = 'media_content_asset';
2122
private const ASSET_ID = 'asset_id';
@@ -46,26 +47,27 @@ public function __construct(ResourceConnection $resourceConnection, LoggerInterf
4647
}
4748

4849
/**
49-
* @inheritDoc
50+
* @inheritdoc
5051
*/
51-
public function execute(int $assetId, string $contentType, string $contentEntityId, string $contentField): void
52+
public function execute(ContentIdentityInterface $contentIdentity, array $assetIds): void
5253
{
5354
try {
5455
$connection = $this->resourceConnection->getConnection();
5556
$tableName = $this->resourceConnection->getTableName(self::MEDIA_CONTENT_ASSET_TABLE_NAME);
5657
$connection->delete(
5758
$tableName,
5859
[
59-
self::ASSET_ID . ' = ?' => $assetId,
60-
self::TYPE . ' = ?' => $contentType,
61-
self::ENTITY_ID . ' = ?' => $contentEntityId,
62-
self::FIELD . ' = ?' => $contentField
60+
self::ASSET_ID . ' IN (?)' => $assetIds,
61+
self::TYPE . ' = ?' => $contentIdentity->getEntityType(),
62+
self::ENTITY_ID . ' = ?' => $contentIdentity->getEntityId(),
63+
self::FIELD . ' = ?' => $contentIdentity->getField()
6364
]
6465
);
6566
} catch (\Exception $exception) {
6667
$this->logger->critical($exception);
67-
$message = __('An error occurred at unassign relation between the media asset and media content.');
68-
throw new CouldNotDeleteException($message);
68+
throw new CouldNotDeleteException(
69+
__('An error occurred at unassign relation between the media asset and media content.')
70+
);
6971
}
7072
}
7173
}

0 commit comments

Comments
 (0)