@@ -130,7 +130,6 @@ public function addNameToIds(array $nameToIds): void
130
130
*
131
131
* @return void
132
132
* @throws LocalizedException
133
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
134
133
*/
135
134
private function processLinkBunches (
136
135
Product $ importEntity ,
@@ -151,13 +150,8 @@ private function processLinkBunches(
151
150
$ productId = $ this ->skuProcessor ->getNewSku ($ sku )[$ linkField ];
152
151
$ productIds [] = $ productId ;
153
152
$ productLinkKeys = $ this ->fetchProductLinks ($ importEntity , $ resource , $ productId );
154
- $ linkNameToId = array_filter (
155
- $ this ->linkNameToId ,
156
- function ($ linkName ) use ($ rowData ) {
157
- return isset ($ rowData [$ linkName . 'sku ' ]);
158
- },
159
- ARRAY_FILTER_USE_KEY
160
- );
153
+ $ linkNameToId = $ this ->filterProvidedLinkTypes ($ rowData );
154
+
161
155
foreach ($ linkNameToId as $ linkName => $ linkId ) {
162
156
$ linkSkus = explode ($ importEntity ->getMultipleValueSeparator (), $ rowData [$ linkName . 'sku ' ]);
163
157
@@ -171,18 +165,8 @@ function ($linkName) use ($rowData) {
171
165
? explode ($ importEntity ->getMultipleValueSeparator (), $ rowData [$ linkName . 'position ' ])
172
166
: [];
173
167
174
- $ linkSkus = array_filter (
175
- $ linkSkus ,
176
- function ($ linkedSku ) use ($ sku , $ importEntity ) {
177
- $ linkedSku = trim ($ linkedSku );
168
+ $ linkSkus = $ this ->filterValidLinks ($ importEntity , $ sku , $ linkSkus );
178
169
179
- return (
180
- $ this ->skuProcessor ->getNewSku ($ linkedSku ) !== null
181
- || $ this ->isSkuExist ($ importEntity , $ linkedSku )
182
- )
183
- && strcasecmp ($ linkedSku , $ sku ) !== 0 ;
184
- }
185
- );
186
170
foreach ($ linkSkus as $ linkedKey => $ linkedSku ) {
187
171
$ linkedId = $ this ->getProductLinkedId ($ importEntity , $ linkedSku );
188
172
if ($ linkedId == null ) {
@@ -373,4 +357,45 @@ private function composeLinkKey(int $productId, int $linkedId, int $linkTypeId):
373
357
{
374
358
return "{$ productId }- {$ linkedId }- {$ linkTypeId }" ;
375
359
}
360
+
361
+ /**
362
+ * Filter out link types which are not provided in the rowData
363
+ *
364
+ * @param array $rowData
365
+ * @return array
366
+ */
367
+ private function filterProvidedLinkTypes (array $ rowData )
368
+ {
369
+ return array_filter (
370
+ $ this ->linkNameToId ,
371
+ function ($ linkName ) use ($ rowData ) {
372
+ return isset ($ rowData [$ linkName . 'sku ' ]);
373
+ },
374
+ ARRAY_FILTER_USE_KEY
375
+ );
376
+ }
377
+
378
+ /**
379
+ * Filter out invalid links
380
+ *
381
+ * @param Product $importEntity
382
+ * @param string $sku
383
+ * @param array $linkSkus
384
+ * @return array
385
+ */
386
+ private function filterValidLinks (Product $ importEntity , string $ sku , array $ linkSkus )
387
+ {
388
+ return array_filter (
389
+ $ linkSkus ,
390
+ function ($ linkedSku ) use ($ sku , $ importEntity ) {
391
+ $ linkedSku = trim ($ linkedSku );
392
+
393
+ return (
394
+ $ this ->skuProcessor ->getNewSku ($ linkedSku ) !== null
395
+ || $ this ->isSkuExist ($ importEntity , $ linkedSku )
396
+ )
397
+ && strcasecmp ($ linkedSku , $ sku ) !== 0 ;
398
+ }
399
+ );
400
+ }
376
401
}
0 commit comments