11
11
use Magento \Framework \DB \Adapter \AdapterInterface ;
12
12
use Magento \Framework \DB \Adapter \Pdo \Mysql ;
13
13
use Magento \Framework \Exception \CouldNotSaveException ;
14
+ use Magento \Framework \Exception \CouldNotDeleteException ;
14
15
use Magento \MediaGalleryApi \Api \Data \KeywordInterface ;
16
+ use Magento \MediaGalleryApi \Api \GetAssetsKeywordsInterface ;
15
17
use Magento \MediaGalleryApi \Api \SaveAssetsKeywordsInterface ;
16
18
use Psr \Log \LoggerInterface ;
17
19
@@ -35,6 +37,11 @@ class SaveAssetsKeywords implements SaveAssetsKeywordsInterface
35
37
*/
36
38
private $ saveAssetLinks ;
37
39
40
+ /**
41
+ * @var GetAssetsKeywordsInterface
42
+ */
43
+ private $ getAssetsKeywordsInterface ;
44
+
38
45
/**
39
46
* @var LoggerInterface
40
47
*/
@@ -45,15 +52,18 @@ class SaveAssetsKeywords implements SaveAssetsKeywordsInterface
45
52
*
46
53
* @param ResourceConnection $resourceConnection
47
54
* @param SaveAssetLinks $saveAssetLinks
55
+ * @param GetAssetsKeywordsInterface $getAssetsKeywordsInterface
48
56
* @param LoggerInterface $logger
49
57
*/
50
58
public function __construct (
51
59
ResourceConnection $ resourceConnection ,
52
60
SaveAssetLinks $ saveAssetLinks ,
61
+ GetAssetsKeywordsInterface $ getAssetsKeywordsInterface ,
53
62
LoggerInterface $ logger
54
63
) {
55
64
$ this ->resourceConnection = $ resourceConnection ;
56
65
$ this ->saveAssetLinks = $ saveAssetLinks ;
66
+ $ this ->getAssetsKeywordsInterface = $ getAssetsKeywordsInterface ;
57
67
$ this ->logger = $ logger ;
58
68
}
59
69
@@ -72,8 +82,6 @@ public function execute(array $assetKeywords): void
72
82
}
73
83
}
74
84
75
- $ this ->deleteObsoleteKeywords ();
76
-
77
85
if (!empty ($ failedAssetIds )) {
78
86
throw new CouldNotSaveException (
79
87
__ ('Could not save keywords for asset ids: %ids ' , ['ids ' => implode (' , ' , $ failedAssetIds )])
@@ -86,6 +94,7 @@ public function execute(array $assetKeywords): void
86
94
*
87
95
* @param KeywordInterface[] $keywords
88
96
* @param int $assetId
97
+ * @throws CouldNotDeleteException
89
98
* @throws CouldNotSaveException
90
99
* @throws \Zend_Db_Exception
91
100
*/
@@ -100,6 +109,8 @@ private function saveAssetKeywords(array $keywords, int $assetId): void
100
109
return ;
101
110
}
102
111
112
+ $ this ->deleteObsoleteAssetKeywords ($ data , $ assetId );
113
+
103
114
/** @var Mysql $connection */
104
115
$ connection = $ this ->resourceConnection ->getConnection ();
105
116
$ connection ->insertArray (
@@ -130,58 +141,56 @@ private function getKeywordIds(array $keywords): array
130
141
}
131
142
132
143
/**
133
- * Delete keywords which has
134
- * no relation to any asset
144
+ * Deletes obsolete asset keywords links
135
145
*
136
- * @return void
146
+ * @param array $newKeywords
147
+ * @param int $assetId
148
+ * @throws CouldNotDeleteException
137
149
*/
138
- private function deleteObsoleteKeywords ( ): void
150
+ private function deleteObsoleteAssetKeywords ( array $ newKeywords , int $ assetId ): void
139
151
{
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 ();
159
164
}
160
165
}
161
- }
162
166
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
+ }
186
195
}
187
196
}
0 commit comments