Skip to content

Commit 7c2511a

Browse files
committed
magento/adobe-stock-integration#1391: SaveAssetsKeywordsInterface to delete obsolete keywords - Revised behaviour to delete obsolete asset keyword link based on updated arguments
1 parent a3e9fea commit 7c2511a

File tree

1 file changed

+58
-49
lines changed

1 file changed

+58
-49
lines changed

app/code/Magento/MediaGallery/Model/ResourceModel/Keyword/SaveAssetsKeywords.php

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
use Magento\Framework\DB\Adapter\AdapterInterface;
1212
use Magento\Framework\DB\Adapter\Pdo\Mysql;
1313
use Magento\Framework\Exception\CouldNotSaveException;
14+
use Magento\Framework\Exception\CouldNotDeleteException;
1415
use Magento\MediaGalleryApi\Api\Data\KeywordInterface;
16+
use Magento\MediaGalleryApi\Api\GetAssetsKeywordsInterface;
1517
use Magento\MediaGalleryApi\Api\SaveAssetsKeywordsInterface;
1618
use Psr\Log\LoggerInterface;
1719

@@ -35,6 +37,11 @@ class SaveAssetsKeywords implements SaveAssetsKeywordsInterface
3537
*/
3638
private $saveAssetLinks;
3739

40+
/**
41+
* @var GetAssetsKeywordsInterface
42+
*/
43+
private $getAssetsKeywordsInterface;
44+
3845
/**
3946
* @var LoggerInterface
4047
*/
@@ -45,15 +52,18 @@ class SaveAssetsKeywords implements SaveAssetsKeywordsInterface
4552
*
4653
* @param ResourceConnection $resourceConnection
4754
* @param SaveAssetLinks $saveAssetLinks
55+
* @param GetAssetsKeywordsInterface $getAssetsKeywordsInterface
4856
* @param LoggerInterface $logger
4957
*/
5058
public function __construct(
5159
ResourceConnection $resourceConnection,
5260
SaveAssetLinks $saveAssetLinks,
61+
GetAssetsKeywordsInterface $getAssetsKeywordsInterface,
5362
LoggerInterface $logger
5463
) {
5564
$this->resourceConnection = $resourceConnection;
5665
$this->saveAssetLinks = $saveAssetLinks;
66+
$this->getAssetsKeywordsInterface = $getAssetsKeywordsInterface;
5767
$this->logger = $logger;
5868
}
5969

@@ -72,8 +82,6 @@ public function execute(array $assetKeywords): void
7282
}
7383
}
7484

75-
$this->deleteObsoleteKeywords();
76-
7785
if (!empty($failedAssetIds)) {
7886
throw new CouldNotSaveException(
7987
__('Could not save keywords for asset ids: %ids', ['ids' => implode(' ,', $failedAssetIds)])
@@ -86,6 +94,7 @@ public function execute(array $assetKeywords): void
8694
*
8795
* @param KeywordInterface[] $keywords
8896
* @param int $assetId
97+
* @throws CouldNotDeleteException
8998
* @throws CouldNotSaveException
9099
* @throws \Zend_Db_Exception
91100
*/
@@ -100,6 +109,8 @@ private function saveAssetKeywords(array $keywords, int $assetId): void
100109
return;
101110
}
102111

112+
$this->deleteObsoleteAssetKeywords($data, $assetId);
113+
103114
/** @var Mysql $connection */
104115
$connection = $this->resourceConnection->getConnection();
105116
$connection->insertArray(
@@ -130,58 +141,56 @@ private function getKeywordIds(array $keywords): array
130141
}
131142

132143
/**
133-
* Delete keywords which has
134-
* no relation to any asset
144+
* Deletes obsolete asset keywords links
135145
*
136-
* @return void
146+
* @param array $newKeywords
147+
* @param int $assetId
148+
* @throws CouldNotDeleteException
137149
*/
138-
private function deleteObsoleteKeywords(): void
150+
private function deleteObsoleteAssetKeywords(array $newKeywords, int $assetId): void
139151
{
140-
$connection = $this->resourceConnection->getConnection();
141-
$select = $connection->select()
142-
->from(
143-
['k' => self::TABLE_KEYWORD],
144-
['k.id']
145-
)
146-
->joinLeft(
147-
['ak' => self::TABLE_ASSET_KEYWORD],
148-
'k.id = ak.keyword_id'
149-
)
150-
->where('ak.asset_id IS NULL');
151-
152-
$obsoleteKeywords = $connection->fetchCol($select);
153-
154-
if (!empty($obsoleteKeywords)) {
155-
try {
156-
$this->deleteKeywordsByIds($obsoleteKeywords);
157-
} catch (\Exception $exception) {
158-
$this->logger->critical($exception);
152+
$oldKeywordData = $this->getAssetsKeywordsInterface->execute([$assetId]);
153+
154+
if (empty($newKeywords) || empty($oldKeywordData)) {
155+
return;
156+
}
157+
158+
$oldKeywordData = $this->getAssetsKeywordsInterface->execute([$assetId]);
159+
$oldKeywords = $oldKeywordData[$assetId]->getKeywords();
160+
161+
foreach ($oldKeywords as $oldKeyword) {
162+
if (!in_array($oldKeyword->getKeyword(), $newKeywords)) {
163+
$obsoleteKeywords[] = $oldKeyword->getKeyword();
159164
}
160165
}
161-
}
162166

163-
/**
164-
* Delete keywords by ids
165-
*
166-
* @param array $keywordIds
167-
* @return void
168-
*/
169-
private function deleteKeywordsByIds(array $keywordIds): void
170-
{
171-
$connection = $this->resourceConnection->getConnection();
172-
173-
$whereConditions = [
174-
$connection->prepareSqlCondition(
175-
self::ID,
176-
['in' => [$keywordIds]]
177-
),
178-
];
179-
180-
$connection->delete(
181-
$connection->getTableName(
182-
self::TABLE_KEYWORD
183-
),
184-
$whereConditions
185-
);
167+
if (empty($obsoleteKeywords)) {
168+
return;
169+
}
170+
171+
$obsoleteKeywordIds = $this->getKeywordIds($obsoleteKeywords);
172+
173+
try {
174+
/** @var Mysql $connection */
175+
$connection = $this->resourceConnection->getConnection();
176+
$connection->delete(
177+
$connection->getTableName(
178+
self::TABLE_ASSET_KEYWORD
179+
),
180+
[
181+
'keyword_id in (?)' => $obsoleteKeywordIds,
182+
'asset_id = ?' => $assetId
183+
]
184+
);
185+
} catch (\Exception $exception) {
186+
$this->logger->critical($exception);
187+
$failedAssetId = $assetId;
188+
}
189+
190+
if (!empty($failedAssetId)) {
191+
throw new CouldNotDeleteException(
192+
__('Could not delete obsolete keyword relation for asset id: %id', ['id' => $assetId])
193+
);
194+
}
186195
}
187196
}

0 commit comments

Comments
 (0)