66 */
77namespace Magento \Catalog \Model ;
88
9- use Magento \Catalog \Api \Data \ProductInterface ;
109use Magento \Catalog \Model \Product \Gallery \MimeTypeExtensionMap ;
1110use Magento \Catalog \Model \ResourceModel \Product \Collection ;
12- use Magento \Framework \Api \Data \ImageContentInterface ;
1311use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
1412use Magento \Framework \Api \ImageContentValidatorInterface ;
1513use Magento \Framework \Api \ImageProcessorInterface ;
1816use Magento \Framework \DB \Adapter \DeadlockException ;
1917use Magento \Framework \DB \Adapter \LockWaitException ;
2018use Magento \Framework \Exception \CouldNotSaveException ;
21- use Magento \Framework \Exception \InputException ;
2219use Magento \Framework \Exception \LocalizedException ;
2320use Magento \Framework \Exception \NoSuchEntityException ;
24- use Magento \Framework \Exception \StateException ;
2521use Magento \Framework \Exception \ValidatorException ;
2622
2723/**
@@ -116,11 +112,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
116112 protected $ fileSystem ;
117113
118114 /**
115+ * @deprecated
116+ * @see \Magento\Catalog\Model\MediaGalleryProcessor
119117 * @var ImageContentInterfaceFactory
120118 */
121119 protected $ contentFactory ;
122120
123121 /**
122+ * @deprecated
123+ * @see \Magento\Catalog\Model\MediaGalleryProcessor
124124 * @var ImageProcessorInterface
125125 */
126126 protected $ imageProcessor ;
@@ -131,7 +131,7 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
131131 protected $ extensionAttributesJoinProcessor ;
132132
133133 /**
134- * @var \Magento\Catalog\Model\Product\Gallery\Processor
134+ * @var ProductRepository\MediaGalleryProcessor
135135 */
136136 protected $ mediaGalleryProcessor ;
137137
@@ -329,6 +329,9 @@ protected function initializeProductData(array $productData, $createNew)
329329 unset($ productData ['media_gallery ' ]);
330330 if ($ createNew ) {
331331 $ product = $ this ->productFactory ->create ();
332+ if (isset ($ productData ['price ' ]) && !isset ($ productData ['product_type ' ])) {
333+ $ product ->setTypeId (Product \Type::TYPE_SIMPLE );
334+ }
332335 if ($ this ->storeManager ->hasSingleStore ()) {
333336 $ product ->setWebsiteIds ([$ this ->storeManager ->getStore (true )->getWebsiteId ()]);
334337 }
@@ -375,53 +378,6 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
375378 $ product ->setWebsiteIds ($ websiteIds );
376379 }
377380
378- /**
379- * @param ProductInterface $product
380- * @param array $newEntry
381- * @return $this
382- * @throws InputException
383- * @throws StateException
384- * @throws \Magento\Framework\Exception\LocalizedException
385- */
386- protected function processNewMediaGalleryEntry (
387- ProductInterface $ product ,
388- array $ newEntry
389- ) {
390- /** @var ImageContentInterface $contentDataObject */
391- $ contentDataObject = $ newEntry ['content ' ];
392-
393- /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
394- $ mediaConfig = $ product ->getMediaConfig ();
395- $ mediaTmpPath = $ mediaConfig ->getBaseTmpMediaPath ();
396-
397- $ relativeFilePath = $ this ->imageProcessor ->processImageContent ($ mediaTmpPath , $ contentDataObject );
398- $ tmpFilePath = $ mediaConfig ->getTmpMediaShortUrl ($ relativeFilePath );
399-
400- if (!$ product ->hasGalleryAttribute ()) {
401- throw new StateException (__ ('Requested product does not support images. ' ));
402- }
403-
404- $ imageFileUri = $ this ->getMediaGalleryProcessor ()->addImage (
405- $ product ,
406- $ tmpFilePath ,
407- isset ($ newEntry ['types ' ]) ? $ newEntry ['types ' ] : [],
408- true ,
409- isset ($ newEntry ['disabled ' ]) ? $ newEntry ['disabled ' ] : true
410- );
411- // Update additional fields that are still empty after addImage call
412- $ this ->getMediaGalleryProcessor ()->updateImage (
413- $ product ,
414- $ imageFileUri ,
415- [
416- 'label ' => $ newEntry ['label ' ],
417- 'position ' => $ newEntry ['position ' ],
418- 'disabled ' => $ newEntry ['disabled ' ],
419- 'media_type ' => $ newEntry ['media_type ' ],
420- ]
421- );
422- return $ this ;
423- }
424-
425381 /**
426382 * Process product links, creating new links, updating and deleting existing links
427383 *
@@ -480,67 +436,6 @@ private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $produc
480436 return $ this ;
481437 }
482438
483- /**
484- * Process Media gallery data before save product.
485- *
486- * Compare Media Gallery Entries Data with existing Media Gallery
487- * * If Media entry has not value_id set it as new
488- * * If Existing entry 'value_id' absent in Media Gallery set 'removed' flag
489- * * Merge Existing and new media gallery
490- *
491- * @param ProductInterface $product contains only existing media gallery items
492- * @param array $mediaGalleryEntries array which contains all media gallery items
493- * @return $this
494- * @throws InputException
495- * @throws StateException
496- * @throws LocalizedException
497- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
498- */
499- protected function processMediaGallery (ProductInterface $ product , $ mediaGalleryEntries )
500- {
501- $ existingMediaGallery = $ product ->getMediaGallery ('images ' );
502- $ newEntries = [];
503- $ entriesById = [];
504- if (!empty ($ existingMediaGallery )) {
505- foreach ($ mediaGalleryEntries as $ entry ) {
506- if (isset ($ entry ['id ' ])) {
507- $ entriesById [$ entry ['id ' ]] = $ entry ;
508- } else {
509- $ newEntries [] = $ entry ;
510- }
511- }
512- foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
513- if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
514- $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
515- if (array_key_exists ('file ' , $ updatedEntry ) && $ updatedEntry ['file ' ] === null ) {
516- unset($ updatedEntry ['file ' ]);
517- }
518- $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
519- } else {
520- //set the removed flag
521- $ existingEntry ['removed ' ] = true ;
522- }
523- }
524- unset($ existingEntry );
525- $ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
526- } else {
527- $ newEntries = $ mediaGalleryEntries ;
528- }
529-
530- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
531- $ images = $ product ->getMediaGallery ('images ' );
532- if ($ images ) {
533- foreach ($ images as $ image ) {
534- if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
535- $ this ->getMediaGalleryProcessor ()->setMediaAttribute ($ product , $ image ['types ' ], $ image ['file ' ]);
536- }
537- }
538- }
539- $ this ->processEntries ($ product , $ newEntries , $ entriesById );
540-
541- return $ this ;
542- }
543-
544439 /**
545440 * {@inheritdoc}
546441 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
@@ -577,7 +472,10 @@ public function save(\Magento\Catalog\Api\Data\ProductInterface $product, $saveO
577472
578473 $ this ->processLinks ($ product , $ productLinks );
579474 if (isset ($ productDataArray ['media_gallery_entries ' ])) {
580- $ this ->processMediaGallery ($ product , $ productDataArray ['media_gallery_entries ' ]);
475+ $ this ->getMediaGalleryProcessor ()->processMediaGallery (
476+ $ product ,
477+ $ productDataArray ['media_gallery_entries ' ]
478+ );
581479 }
582480
583481 if (!$ product ->getOptionsReadonly ()) {
@@ -749,13 +647,13 @@ public function cleanCache()
749647 }
750648
751649 /**
752- * @return Product\Gallery\Processor
650+ * @return ProductRepository\MediaGalleryProcessor
753651 */
754652 private function getMediaGalleryProcessor ()
755653 {
756654 if (null === $ this ->mediaGalleryProcessor ) {
757655 $ this ->mediaGalleryProcessor = \Magento \Framework \App \ObjectManager::getInstance ()
758- ->get (\ Magento \ Catalog \ Model \ Product \ Gallery \Processor ::class);
656+ ->get (ProductRepository \MediaGalleryProcessor ::class);
759657 }
760658 return $ this ->mediaGalleryProcessor ;
761659 }
@@ -775,60 +673,4 @@ private function getCollectionProcessor()
775673 }
776674 return $ this ->collectionProcessor ;
777675 }
778-
779- /**
780- * Convert extension attribute for product media gallery.
781- *
782- * @param array $newEntry
783- * @param array $extensionAttributes
784- * @return void
785- */
786- private function processExtensionAttributes (array &$ newEntry , array $ extensionAttributes )
787- {
788- foreach ($ extensionAttributes as $ code => $ value ) {
789- if (is_array ($ value )) {
790- $ this ->processExtensionAttributes ($ newEntry , $ value );
791- } else {
792- $ newEntry [$ code ] = $ value ;
793- }
794- }
795- unset($ newEntry ['extension_attributes ' ]);
796- }
797-
798- /**
799- * Convert entries into product media gallery data and set to product.
800- *
801- * @param ProductInterface $product
802- * @param array $newEntries
803- * @param array $entriesById
804- * @throws InputException
805- * @throws LocalizedException
806- * @throws StateException
807- * @return void
808- */
809- private function processEntries (ProductInterface $ product , array $ newEntries , array $ entriesById )
810- {
811- foreach ($ newEntries as $ newEntry ) {
812- if (!isset ($ newEntry ['content ' ])) {
813- throw new InputException (__ ('The image content is not valid. ' ));
814- }
815- /** @var ImageContentInterface $contentDataObject */
816- $ contentDataObject = $ this ->contentFactory ->create ()
817- ->setName ($ newEntry ['content ' ][ImageContentInterface::NAME ])
818- ->setBase64EncodedData ($ newEntry ['content ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
819- ->setType ($ newEntry ['content ' ][ImageContentInterface::TYPE ]);
820- $ newEntry ['content ' ] = $ contentDataObject ;
821- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
822-
823- $ finalGallery = $ product ->getData ('media_gallery ' );
824- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
825- if (isset ($ newEntry ['extension_attributes ' ])) {
826- $ this ->processExtensionAttributes ($ newEntry , $ newEntry ['extension_attributes ' ]);
827- }
828- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
829- $ entriesById [$ newEntryId ] = $ newEntry ;
830- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
831- $ product ->setData ('media_gallery ' , $ finalGallery );
832- }
833- }
834676}
0 commit comments