@@ -33,24 +33,24 @@ class SaveAssetLinks
33
33
/**
34
34
* @var GetAssetsKeywordsInterface
35
35
*/
36
- private $ getAssetsKeywordsInterface ;
36
+ private $ getAssetsKeywords ;
37
37
38
38
/**
39
39
* @var LoggerInterface
40
40
*/
41
41
private $ logger ;
42
42
43
43
/**
44
- * @param GetAssetsKeywordsInterface $getAssetsKeywordsInterface
44
+ * @param GetAssetsKeywordsInterface $getAssetsKeywords
45
45
* @param ResourceConnection $resourceConnection
46
46
* @param LoggerInterface $logger
47
47
*/
48
48
public function __construct (
49
- GetAssetsKeywordsInterface $ getAssetsKeywordsInterface ,
49
+ GetAssetsKeywordsInterface $ getAssetsKeywords ,
50
50
ResourceConnection $ resourceConnection ,
51
51
LoggerInterface $ logger
52
52
) {
53
- $ this ->getAssetsKeywordsInterface = $ getAssetsKeywordsInterface ;
53
+ $ this ->getAssetsKeywords = $ getAssetsKeywords ;
54
54
$ this ->resourceConnection = $ resourceConnection ;
55
55
$ this ->logger = $ logger ;
56
56
}
@@ -66,8 +66,14 @@ public function __construct(
66
66
public function execute (int $ assetId , array $ keywordIds ): void
67
67
{
68
68
try {
69
- $ this ->deleteAssetKeywords ($ assetId , $ keywordIds );
70
- $ this ->insertAssetKeywords ($ assetId , $ keywordIds );
69
+ $ currentKeywordIds = $ this ->getCurrentKeywordIds ($ assetId );
70
+
71
+ $ obsoleteKeywordIds = array_diff ($ currentKeywordIds , $ keywordIds );
72
+ $ newKeywordIds = array_diff ($ keywordIds , $ currentKeywordIds );
73
+
74
+ $ this ->deleteAssetKeywords ($ assetId , $ obsoleteKeywordIds );
75
+ $ this ->insertAssetKeywords ($ assetId , $ newKeywordIds );
76
+
71
77
} catch (\Exception $ exception ) {
72
78
$ this ->logger ->critical ($ exception );
73
79
throw new CouldNotSaveException (
@@ -81,17 +87,16 @@ public function execute(int $assetId, array $keywordIds): void
81
87
* Save new asset keyword links
82
88
*
83
89
* @param int $assetId
84
- * @param array $keywordIds
90
+ * @param int[] $keywordIds
85
91
* @throws CouldNotSaveException
86
92
*/
87
93
private function insertAssetKeywords (int $ assetId , array $ keywordIds ): void
88
94
{
89
95
try {
90
96
if (!empty ($ keywordIds )) {
91
97
$ values = [];
92
- $ keywordsToInsert = array_diff ($ keywordIds , $ this ->getCurrentKeywords ($ assetId ));
93
98
94
- foreach ($ keywordsToInsert as $ keywordId ) {
99
+ foreach ($ keywordIds as $ keywordId ) {
95
100
$ values [] = [$ assetId , $ keywordId ];
96
101
}
97
102
@@ -119,14 +124,12 @@ private function insertAssetKeywords(int $assetId, array $keywordIds): void
119
124
* Delete obsolete asset keyword links
120
125
*
121
126
* @param int $assetId
122
- * @param array $keywords
127
+ * @param int[] $obsoleteKeywordIds
123
128
* @throws CouldNotDeleteException
124
129
*/
125
- private function deleteAssetKeywords (int $ assetId , array $ keywords ): void
130
+ private function deleteAssetKeywords (int $ assetId , array $ obsoleteKeywordIds ): void
126
131
{
127
132
try {
128
- $ obsoleteKeywordIds = array_diff ($ this ->getCurrentKeywords ($ assetId ), $ keywords );
129
-
130
133
if (!empty ($ obsoleteKeywordIds )) {
131
134
/** @var Mysql $connection */
132
135
$ connection = $ this ->resourceConnection ->getConnection ();
@@ -150,31 +153,29 @@ private function deleteAssetKeywords(int $assetId, array $keywords): void
150
153
}
151
154
152
155
/**
153
- * Get current keyword data of an asset
156
+ * Get current keyword ids of an asset
154
157
*
155
158
* @param int $assetId
156
- * @return array
159
+ * @return int[]
157
160
*/
158
- private function getCurrentKeywords (int $ assetId ): array
161
+ private function getCurrentKeywordIds (int $ assetId ): array
159
162
{
160
- $ currentKeywordsData = $ this ->getAssetsKeywordsInterface ->execute ([$ assetId ]);
163
+ $ currentKeywordsData = $ this ->getAssetsKeywords ->execute ([$ assetId ]);
161
164
162
- if (!empty ($ currentKeywordsData )) {
163
- $ currentKeywords = $ this ->getKeywordIdsFromKeywordData (
164
- $ currentKeywordsData [$ assetId ]->getKeywords ()
165
- );
166
-
167
- return $ currentKeywords ;
165
+ if (empty ($ currentKeywordsData )) {
166
+ return [];
168
167
}
169
168
170
- return [];
169
+ return $ this ->getKeywordIdsFromKeywordData (
170
+ $ currentKeywordsData [$ assetId ]->getKeywords ()
171
+ );
171
172
}
172
173
173
174
/**
174
175
* Get keyword ids from keyword data
175
176
*
176
- * @param array $keywordsData
177
- * @return array
177
+ * @param KeywordInterface[] $keywordsData
178
+ * @return int[]
178
179
*/
179
180
private function getKeywordIdsFromKeywordData (array $ keywordsData ): array
180
181
{
0 commit comments