@@ -190,6 +190,70 @@ public function saveChild(
190
190
return true ;
191
191
}
192
192
193
+ /**
194
+ * Linked product processing
195
+ *
196
+ * @param LinkInterface $linkedProduct
197
+ * @param array $selections
198
+ * @param int $optionId
199
+ * @param ProductInterface $product
200
+ * @param string $linkField
201
+ * @param Bundle $resource
202
+ * @return int
203
+ * @throws CouldNotSaveException
204
+ * @throws InputException
205
+ * @throws NoSuchEntityException
206
+ */
207
+ private function processLinkedProduct (
208
+ LinkInterface $ linkedProduct ,
209
+ array $ selections ,
210
+ int $ optionId ,
211
+ ProductInterface $ product ,
212
+ string $ linkField ,
213
+ Bundle $ resource
214
+ ): int {
215
+ $ linkProductModel = $ this ->productRepository ->get ($ linkedProduct ->getSku ());
216
+ if ($ linkProductModel ->isComposite ()) {
217
+ throw new InputException (__ ('The bundle product can \'t contain another composite product. ' ));
218
+ }
219
+
220
+ if ($ selections ) {
221
+ foreach ($ selections as $ selection ) {
222
+ if ($ selection ['option_id ' ] == $ optionId &&
223
+ $ selection ['product_id ' ] == $ linkProductModel ->getEntityId () &&
224
+ $ selection ['parent_product_id ' ] == $ product ->getData ($ linkField )) {
225
+ if (!$ product ->getCopyFromView ()) {
226
+ throw new CouldNotSaveException (
227
+ __ (
228
+ 'Child with specified sku: "%1" already assigned to product: "%2" ' ,
229
+ [$ linkedProduct ->getSku (), $ product ->getSku ()]
230
+ )
231
+ );
232
+ }
233
+ }
234
+ }
235
+ }
236
+
237
+ $ selectionModel = $ this ->bundleSelection ->create ();
238
+ $ selectionModel = $ this ->mapProductLinkToBundleSelectionModel (
239
+ $ selectionModel ,
240
+ $ linkedProduct ,
241
+ $ product ,
242
+ (int )$ linkProductModel ->getEntityId ()
243
+ );
244
+
245
+ $ selectionModel ->setOptionId ($ optionId );
246
+
247
+ try {
248
+ $ selectionModel ->save ();
249
+ $ resource ->addProductRelation ($ product ->getData ($ linkField ), $ linkProductModel ->getEntityId ());
250
+ } catch (\Exception $ e ) {
251
+ throw new CouldNotSaveException (__ ('Could not save child: "%1" ' , $ e ->getMessage ()), $ e );
252
+ }
253
+
254
+ return (int )$ selectionModel ->getId ();
255
+ }
256
+
193
257
/**
194
258
* Fill selection model with product link data
195
259
*
@@ -291,8 +355,6 @@ private function mapProductLinkToBundleSelectionModel(
291
355
292
356
/**
293
357
* @inheritDoc
294
- *
295
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
296
358
*/
297
359
public function addChild (
298
360
ProductInterface $ product ,
@@ -326,49 +388,7 @@ public function addChild(
326
388
/* @var $resource Bundle */
327
389
$ resource = $ this ->bundleFactory ->create ();
328
390
$ selections = $ resource ->getSelectionsData ($ product ->getData ($ linkField ));
329
- /** @var Product $linkProductModel */
330
- $ linkProductModel = $ this ->productRepository ->get ($ linkedProduct ->getSku ());
331
- if ($ linkProductModel ->isComposite ()) {
332
- throw new InputException (__ ('The bundle product can \'t contain another composite product. ' ));
333
- }
334
-
335
- if ($ selections ) {
336
- foreach ($ selections as $ selection ) {
337
- if ($ selection ['option_id ' ] == $ optionId &&
338
- $ selection ['product_id ' ] == $ linkProductModel ->getEntityId () &&
339
- $ selection ['parent_product_id ' ] == $ product ->getData ($ linkField )) {
340
- if (!$ product ->getCopyFromView ()) {
341
- throw new CouldNotSaveException (
342
- __ (
343
- 'Child with specified sku: "%1" already assigned to product: "%2" ' ,
344
- [$ linkedProduct ->getSku (), $ product ->getSku ()]
345
- )
346
- );
347
- }
348
-
349
- return $ this ->bundleSelection ->create ()->load ($ linkProductModel ->getEntityId ());
350
- }
351
- }
352
- }
353
-
354
- $ selectionModel = $ this ->bundleSelection ->create ();
355
- $ selectionModel = $ this ->mapProductLinkToBundleSelectionModel (
356
- $ selectionModel ,
357
- $ linkedProduct ,
358
- $ product ,
359
- (int )$ linkProductModel ->getEntityId ()
360
- );
361
-
362
- $ selectionModel ->setOptionId ($ optionId );
363
-
364
- try {
365
- $ selectionModel ->save ();
366
- $ resource ->addProductRelation ($ product ->getData ($ linkField ), $ linkProductModel ->getEntityId ());
367
- } catch (\Exception $ e ) {
368
- throw new CouldNotSaveException (__ ('Could not save child: "%1" ' , $ e ->getMessage ()), $ e );
369
- }
370
-
371
- return (int )$ selectionModel ->getId ();
391
+ return $ this ->processLinkedProduct ($ linkedProduct , $ selections , $ optionId , $ product , $ linkField , $ resource );
372
392
}
373
393
374
394
/**
@@ -405,44 +425,7 @@ public function addChildren(
405
425
$ selections = $ resource ->getSelectionsData ($ product ->getData ($ linkField ));
406
426
407
427
foreach ($ linkedProducts as $ linkedProduct ) {
408
- $ linkProductModel = $ this ->productRepository ->get ($ linkedProduct ->getSku ());
409
- if ($ linkProductModel ->isComposite ()) {
410
- throw new InputException (__ ('The bundle product can \'t contain another composite product. ' ));
411
- }
412
-
413
- if ($ selections ) {
414
- foreach ($ selections as $ selection ) {
415
- if ($ selection ['option_id ' ] == $ optionId &&
416
- $ selection ['product_id ' ] == $ linkProductModel ->getEntityId () &&
417
- $ selection ['parent_product_id ' ] == $ product ->getData ($ linkField )) {
418
- if (!$ product ->getCopyFromView ()) {
419
- throw new CouldNotSaveException (
420
- __ (
421
- 'Child with specified sku: "%1" already assigned to product: "%2" ' ,
422
- [$ linkedProduct ->getSku (), $ product ->getSku ()]
423
- )
424
- );
425
- }
426
- }
427
- }
428
- }
429
-
430
- $ selectionModel = $ this ->bundleSelection ->create ();
431
- $ selectionModel = $ this ->mapProductLinkToBundleSelectionModel (
432
- $ selectionModel ,
433
- $ linkedProduct ,
434
- $ product ,
435
- (int )$ linkProductModel ->getEntityId ()
436
- );
437
-
438
- $ selectionModel ->setOptionId ($ optionId );
439
-
440
- try {
441
- $ selectionModel ->save ();
442
- $ resource ->addProductRelation ($ product ->getData ($ linkField ), $ linkProductModel ->getEntityId ());
443
- } catch (\Exception $ e ) {
444
- throw new CouldNotSaveException (__ ('Could not save child: "%1" ' , $ e ->getMessage ()), $ e );
445
- }
428
+ $ this ->processLinkedProduct ($ linkedProduct , $ selections , $ optionId , $ product , $ linkField , $ resource );
446
429
}
447
430
}
448
431
0 commit comments