Skip to content

Commit 2be962d

Browse files
committed
Splitting some logic into separated methods
1 parent abb3c4a commit 2be962d

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -455,18 +455,7 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
455455
$newEntries[] = $entry;
456456
}
457457
}
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);
470459
$product->setData('media_gallery', ["images" => $existingMediaGallery]);
471460
} else {
472461
$newEntries = $mediaGalleryEntries;
@@ -487,25 +476,8 @@ protected function processMediaGallery(ProductInterface $product, $mediaGalleryE
487476
}
488477
}
489478

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);
501480

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-
}
509481
return $this;
510482
}
511483

@@ -760,4 +732,58 @@ private function getMediaGalleryProcessor()
760732
}
761733
return $this->mediaGalleryProcessor;
762734
}
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+
}
763789
}

0 commit comments

Comments
 (0)