15
15
use Psr \Log \LoggerInterface ;
16
16
17
17
/**
18
- * Class LinkProcessor
18
+ * Processor for links between products
19
+ *
20
+ * Remark: Via DI it is possible to supply additional link types.
19
21
*/
20
22
class LinkProcessor
21
23
{
@@ -76,7 +78,7 @@ public function __construct(
76
78
* @param Product $importEntity
77
79
* @param Data $dataSourceModel
78
80
* @param string $linkField
79
- * @return $this
81
+ * @return void
80
82
* @throws LocalizedException
81
83
*/
82
84
public function saveLinks (
@@ -128,6 +130,7 @@ public function addNameToIds(array $nameToIds): void
128
130
*
129
131
* @return void
130
132
* @throws LocalizedException
133
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
131
134
*/
132
135
private function processLinkBunches (
133
136
Product $ importEntity ,
@@ -163,6 +166,7 @@ function ($linkName) use ($rowData) {
163
166
$ linksToDelete [$ linkId ][] = $ productId ;
164
167
continue ;
165
168
}
169
+
166
170
$ linkPositions = ! empty ($ rowData [$ linkName . 'position ' ])
167
171
? explode ($ importEntity ->getMultipleValueSeparator (), $ rowData [$ linkName . 'position ' ])
168
172
: [];
@@ -180,7 +184,7 @@ function ($linkedSku) use ($sku, $importEntity) {
180
184
}
181
185
);
182
186
foreach ($ linkSkus as $ linkedKey => $ linkedSku ) {
183
- $ linkedId = $ this ->getProductLinkedId ($ linkedSku );
187
+ $ linkedId = $ this ->getProductLinkedId ($ importEntity , $ linkedSku );
184
188
if ($ linkedId == null ) {
185
189
// Import file links to a SKU which is skipped for some reason, which leads to a "NULL"
186
190
// link causing fatal errors.
@@ -259,6 +263,19 @@ private function isSkuExist(Product $importEntity, string $sku): bool
259
263
return isset ($ importEntity ->getOldSku ()[$ sku ]);
260
264
}
261
265
266
+ /**
267
+ * Get existing SKU record
268
+ *
269
+ * @param Product $importEntity
270
+ * @param string $sku
271
+ * @return mixed
272
+ */
273
+ private function getExistingSku (Product $ importEntity , string $ sku )
274
+ {
275
+ $ sku = strtolower ($ sku );
276
+ return $ importEntity ->getOldSku ()[$ sku ];
277
+ }
278
+
262
279
/**
263
280
* Fetches Product Links
264
281
*
@@ -289,15 +306,18 @@ private function fetchProductLinks(Product $importEntity, Link $resource, int $p
289
306
/**
290
307
* Gets the Id of the Sku
291
308
*
309
+ * @param Product $importEntity
292
310
* @param string $linkedSku
293
- *
294
311
* @return int|null
295
312
*/
296
- private function getProductLinkedId (string $ linkedSku ): ?int
313
+ private function getProductLinkedId (Product $ importEntity , string $ linkedSku ): ?int
297
314
{
298
315
$ linkedSku = trim ($ linkedSku );
299
316
$ newSku = $ this ->skuProcessor ->getNewSku ($ linkedSku );
300
- $ linkedId = ! empty ($ newSku ) ? $ newSku ['entity_id ' ] : $ this ->getExistingSku ($ linkedSku )['entity_id ' ];
317
+
318
+ $ linkedId = ! empty ($ newSku ) ?
319
+ $ newSku ['entity_id ' ] :
320
+ $ this ->getExistingSku ($ importEntity , $ linkedSku )['entity_id ' ];
301
321
302
322
return $ linkedId ;
303
323
}
0 commit comments