@@ -455,18 +455,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
455
455
$ newEntries [] = $ entry ;
456
456
}
457
457
}
458
- foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
459
- if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
460
- $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
461
- if ($ updatedEntry ['file ' ] === null ) {
462
- unset($ updatedEntry ['file ' ]);
463
- }
464
- $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
465
- } else {
466
- //set the removed flag
467
- $ existingEntry ['removed ' ] = true ;
468
- }
469
- }
458
+ $ existingMediaGallery = $ this ->processingExistingImages ($ existingMediaGallery , $ entriesById );
470
459
$ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
471
460
} else {
472
461
$ newEntries = $ mediaGalleryEntries ;
@@ -487,25 +476,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
487
476
}
488
477
}
489
478
490
- foreach ($ newEntries as $ newEntry ) {
491
- if (!isset ($ newEntry ['content ' ])) {
492
- throw new InputException (__ ('The image content is not valid. ' ));
493
- }
494
- /** @var ImageContentInterface $contentDataObject */
495
- $ contentDataObject = $ this ->contentFactory ->create ()
496
- ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
497
- ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
498
- ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
499
- $ newEntry ['content ' ] = $ contentDataObject ;
500
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
479
+ $ this ->processingNewEntries ($ newEntries , $ product , $ entriesById );
501
480
502
- $ finalGallery = $ product ->getData ('media_gallery ' );
503
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
504
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
505
- $ entriesById [$ newEntryId ] = $ newEntry ;
506
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
507
- $ product ->setData ('media_gallery ' , $ finalGallery );
508
- }
509
481
return $ this ;
510
482
}
511
483
@@ -760,4 +732,58 @@ private function getMediaGalleryProcessor()
760
732
}
761
733
return $ this ->mediaGalleryProcessor ;
762
734
}
735
+
736
+ /**
737
+ * @param $existingMediaGallery
738
+ * @param $entriesById
739
+ * @return mixed
740
+ */
741
+ private function processingExistingImages ($ existingMediaGallery , $ entriesById )
742
+ {
743
+ foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
744
+ if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
745
+ $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
746
+ if ($ updatedEntry ['file ' ] === null ) {
747
+ unset($ updatedEntry ['file ' ]);
748
+ }
749
+ $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
750
+ } else {
751
+ //set the removed flag
752
+ $ existingEntry ['removed ' ] = true ;
753
+ }
754
+ }
755
+
756
+ return $ existingMediaGallery ;
757
+ }
758
+
759
+ /**
760
+ * @param $newEntries
761
+ * @param $product
762
+ * @param $entriesById
763
+ * @throws InputException
764
+ * @throws LocalizedException
765
+ * @throws StateException
766
+ */
767
+ private function processingNewEntries ($ newEntries , $ product , $ entriesById )
768
+ {
769
+ foreach ($ newEntries as $ newEntry ) {
770
+ if (!isset ($ newEntry ['content ' ])) {
771
+ throw new InputException (__ ('The image content is not valid. ' ));
772
+ }
773
+ /** @var ImageContentInterface $contentDataObject */
774
+ $ contentDataObject = $ this ->contentFactory ->create ()
775
+ ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
776
+ ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
777
+ ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
778
+ $ newEntry ['content ' ] = $ contentDataObject ;
779
+ $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
780
+
781
+ $ finalGallery = $ product ->getData ('media_gallery ' );
782
+ $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
783
+ $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
784
+ $ entriesById [$ newEntryId ] = $ newEntry ;
785
+ $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
786
+ $ product ->setData ('media_gallery ' , $ finalGallery );
787
+ }
788
+ }
763
789
}
0 commit comments