@@ -488,8 +488,8 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
488
488
* @return $this
489
489
* @throws InputException
490
490
* @throws StateException
491
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
492
491
* @throws LocalizedException
492
+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
493
493
*/
494
494
protected function processMediaGallery (ProductInterface $ product , array $ mediaGalleryEntries )
495
495
{
@@ -531,29 +531,8 @@ protected function processMediaGallery(ProductInterface $product, array $mediaGa
531
531
}
532
532
}
533
533
}
534
+ $ this ->processEntries ($ product , $ newEntries , $ entriesById );
534
535
535
- foreach ($ newEntries as $ newEntry ) {
536
- if (!isset ($ newEntry ['content ' ])) {
537
- throw new InputException (__ ('The image content is not valid. ' ));
538
- }
539
- /** @var ImageContentInterface $contentDataObject */
540
- $ contentDataObject = $ this ->contentFactory ->create ()
541
- ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
542
- ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
543
- ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
544
- $ newEntry ['content ' ] = $ contentDataObject ;
545
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
546
-
547
- $ finalGallery = $ product ->getData ('media_gallery ' );
548
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
549
- if (isset ($ newEntry ['extension_attributes ' ])) {
550
- $ this ->processExtensionAttributes ($ newEntry , $ newEntry ['extension_attributes ' ]);
551
- }
552
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
553
- $ entriesById [$ newEntryId ] = $ newEntry ;
554
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
555
- $ product ->setData ('media_gallery ' , $ finalGallery );
556
- }
557
536
return $ this ;
558
537
}
559
538
@@ -810,4 +789,41 @@ private function processExtensionAttributes(array &$newEntry, array $extensionAt
810
789
}
811
790
unset($ newEntry ['extension_attributes ' ]);
812
791
}
792
+
793
+ /**
794
+ * Convert entries into product media gallery data and set to product.
795
+ *
796
+ * @param ProductInterface $product
797
+ * @param array $newEntries
798
+ * @param array $entriesById
799
+ * @throws InputException
800
+ * @throws LocalizedException
801
+ * @throws StateException
802
+ * @return void
803
+ */
804
+ private function processEntries (ProductInterface $ product , array $ newEntries , array $ entriesById )
805
+ {
806
+ foreach ($ newEntries as $ newEntry ) {
807
+ if (!isset ($ newEntry ['content ' ])) {
808
+ throw new InputException (__ ('The image content is not valid. ' ));
809
+ }
810
+ /** @var ImageContentInterface $contentDataObject */
811
+ $ contentDataObject = $ this ->contentFactory ->create ()
812
+ ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
813
+ ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
814
+ ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
815
+ $ newEntry ['content ' ] = $ contentDataObject ;
816
+ $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
817
+
818
+ $ finalGallery = $ product ->getData ('media_gallery ' );
819
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
820
+ if (isset ($ newEntry ['extension_attributes ' ])) {
821
+ $ this ->processExtensionAttributes ($ newEntry , $ newEntry ['extension_attributes ' ]);
822
+ }
823
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
824
+ $ entriesById [$ newEntryId ] = $ newEntry ;
825
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
826
+ $ product ->setData ('media_gallery ' , $ finalGallery );
827
+ }
828
+ }
813
829
}
0 commit comments