1
1
<?php
2
2
/**
3
- *
4
3
* Copyright © Magento, Inc. All rights reserved.
5
4
* See COPYING.txt for license details.
6
5
*/
11
10
use Magento \Catalog \Api \Data \ProductExtension ;
12
11
use Magento \Catalog \Api \Data \ProductInterface ;
13
12
use Magento \Catalog \Model \Product \Gallery \MimeTypeExtensionMap ;
13
+ use Magento \Catalog \Model \ProductRepository \MediaGalleryProcessor ;
14
14
use Magento \Catalog \Model \ResourceModel \Product \Collection ;
15
15
use Magento \Eav \Model \Entity \Attribute \Exception as AttributeException ;
16
- use Magento \Framework \Api \Data \ImageContentInterface ;
17
16
use Magento \Framework \Api \Data \ImageContentInterfaceFactory ;
18
17
use Magento \Framework \Api \ImageContentValidatorInterface ;
19
18
use Magento \Framework \Api \ImageProcessorInterface ;
26
25
use Magento \Framework \Exception \InputException ;
27
26
use Magento \Framework \Exception \LocalizedException ;
28
27
use Magento \Framework \Exception \NoSuchEntityException ;
29
- use Magento \Framework \Exception \TemporaryState \CouldNotSaveException as TemporaryCouldNotSaveException ;
30
28
use Magento \Framework \Exception \StateException ;
29
+ use Magento \Framework \Exception \TemporaryState \CouldNotSaveException as TemporaryCouldNotSaveException ;
31
30
use Magento \Framework \Exception \ValidatorException ;
32
31
33
32
/**
@@ -123,11 +122,15 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
123
122
protected $ fileSystem ;
124
123
125
124
/**
125
+ * @deprecated
126
+ *
126
127
* @var ImageContentInterfaceFactory
127
128
*/
128
129
protected $ contentFactory ;
129
130
130
131
/**
132
+ * @deprecated
133
+ *
131
134
* @var ImageProcessorInterface
132
135
*/
133
136
protected $ imageProcessor ;
@@ -138,10 +141,17 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
138
141
protected $ extensionAttributesJoinProcessor ;
139
142
140
143
/**
144
+ * @deprecated
145
+ *
141
146
* @var \Magento\Catalog\Model\Product\Gallery\Processor
142
147
*/
143
148
protected $ mediaGalleryProcessor ;
144
149
150
+ /**
151
+ * @var MediaGalleryProcessor
152
+ */
153
+ private $ mediaProcessor ;
154
+
145
155
/**
146
156
* @var CollectionProcessorInterface
147
157
*/
@@ -192,8 +202,8 @@ class ProductRepository implements \Magento\Catalog\Api\ProductRepositoryInterfa
192
202
* @param CollectionProcessorInterface $collectionProcessor [optional]
193
203
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
194
204
* @param int $cacheLimit [optional]
195
- * @param ReadExtensions|null $readExtensions
196
- * @param Magento\Catalog\Api\ CategoryLinkManagementInterface|null $linkManagement
205
+ * @param ReadExtensions $readExtensions
206
+ * @param CategoryLinkManagementInterface $linkManagement
197
207
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
198
208
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
199
209
*/
@@ -391,6 +401,9 @@ private function assignProductToWebsites(\Magento\Catalog\Model\Product $product
391
401
/**
392
402
* Process new gallery media entry.
393
403
*
404
+ * @deprecated
405
+ * @see MediaGalleryProcessor::processNewMediaGalleryEntry()
406
+ *
394
407
* @param ProductInterface $product
395
408
* @param array $newEntry
396
409
* @return $this
@@ -402,40 +415,8 @@ protected function processNewMediaGalleryEntry(
402
415
ProductInterface $ product ,
403
416
array $ newEntry
404
417
) {
405
- /** @var ImageContentInterface $contentDataObject */
406
- $ contentDataObject = $ newEntry ['content ' ];
407
-
408
- /** @var \Magento\Catalog\Model\Product\Media\Config $mediaConfig */
409
- $ mediaConfig = $ product ->getMediaConfig ();
410
- $ mediaTmpPath = $ mediaConfig ->getBaseTmpMediaPath ();
411
-
412
- $ relativeFilePath = $ this ->imageProcessor ->processImageContent ($ mediaTmpPath , $ contentDataObject );
413
- $ tmpFilePath = $ mediaConfig ->getTmpMediaShortUrl ($ relativeFilePath );
418
+ $ this ->getMediaGalleryProcessor ()->processNewMediaGalleryEntry ($ product , $ newEntry );
414
419
415
- if (!$ product ->hasGalleryAttribute ()) {
416
- throw new StateException (
417
- __ ("The product that was requested doesn't exist. Verify the product and try again. " )
418
- );
419
- }
420
-
421
- $ imageFileUri = $ this ->getMediaGalleryProcessor ()->addImage (
422
- $ product ,
423
- $ tmpFilePath ,
424
- isset ($ newEntry ['types ' ]) ? $ newEntry ['types ' ] : [],
425
- true ,
426
- isset ($ newEntry ['disabled ' ]) ? $ newEntry ['disabled ' ] : true
427
- );
428
- // Update additional fields that are still empty after addImage call
429
- $ this ->getMediaGalleryProcessor ()->updateImage (
430
- $ product ,
431
- $ imageFileUri ,
432
- [
433
- 'label ' => $ newEntry ['label ' ],
434
- 'position ' => $ newEntry ['position ' ],
435
- 'disabled ' => $ newEntry ['disabled ' ],
436
- 'media_type ' => $ newEntry ['media_type ' ],
437
- ]
438
- );
439
420
return $ this ;
440
421
}
441
422
@@ -510,68 +491,13 @@ private function processLinks(ProductInterface $product, $newLinks)
510
491
* @return $this
511
492
* @throws InputException
512
493
* @throws StateException
494
+ * @throws LocalizedException
513
495
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
514
496
*/
515
497
protected function processMediaGallery (ProductInterface $ product , $ mediaGalleryEntries )
516
498
{
517
- $ existingMediaGallery = $ product ->getMediaGallery ('images ' );
518
- $ newEntries = [];
519
- $ entriesById = [];
520
- if (!empty ($ existingMediaGallery )) {
521
- foreach ($ mediaGalleryEntries as $ entry ) {
522
- if (isset ($ entry ['value_id ' ])) {
523
- $ entriesById [$ entry ['value_id ' ]] = $ entry ;
524
- } else {
525
- $ newEntries [] = $ entry ;
526
- }
527
- }
528
- foreach ($ existingMediaGallery as $ key => &$ existingEntry ) {
529
- if (isset ($ entriesById [$ existingEntry ['value_id ' ]])) {
530
- $ updatedEntry = $ entriesById [$ existingEntry ['value_id ' ]];
531
- if ($ updatedEntry ['file ' ] === null ) {
532
- unset($ updatedEntry ['file ' ]);
533
- }
534
- $ existingMediaGallery [$ key ] = array_merge ($ existingEntry , $ updatedEntry );
535
- } else {
536
- //set the removed flag
537
- $ existingEntry ['removed ' ] = true ;
538
- }
539
- }
540
- $ product ->setData ('media_gallery ' , ["images " => $ existingMediaGallery ]);
541
- } else {
542
- $ newEntries = $ mediaGalleryEntries ;
543
- }
544
-
545
- $ images = (array )$ product ->getMediaGallery ('images ' );
546
- $ images = $ this ->determineImageRoles ($ product , $ images );
547
-
548
- $ this ->getMediaGalleryProcessor ()->clearMediaAttribute ($ product , array_keys ($ product ->getMediaAttributes ()));
549
-
550
- foreach ($ images as $ image ) {
551
- if (!isset ($ image ['removed ' ]) && !empty ($ image ['types ' ])) {
552
- $ this ->getMediaGalleryProcessor ()->setMediaAttribute ($ product , $ image ['types ' ], $ image ['file ' ]);
553
- }
554
- }
499
+ $ this ->getMediaGalleryProcessor ()->processMediaGallery ($ product , $ mediaGalleryEntries );
555
500
556
- foreach ($ newEntries as $ newEntry ) {
557
- if (!isset ($ newEntry ['content ' ])) {
558
- throw new InputException (__ ('The image content is invalid. Verify the content and try again. ' ));
559
- }
560
- /** @var ImageContentInterface $contentDataObject */
561
- $ contentDataObject = $ this ->contentFactory ->create ()
562
- ->setName ($ newEntry ['content ' ]['data ' ][ImageContentInterface::NAME ])
563
- ->setBase64EncodedData ($ newEntry ['content ' ]['data ' ][ImageContentInterface::BASE64_ENCODED_DATA ])
564
- ->setType ($ newEntry ['content ' ]['data ' ][ImageContentInterface::TYPE ]);
565
- $ newEntry ['content ' ] = $ contentDataObject ;
566
- $ this ->processNewMediaGalleryEntry ($ product , $ newEntry );
567
-
568
- $ finalGallery = $ product ->getData ('media_gallery ' );
569
- $ newEntryId = key (array_diff_key ($ product ->getData ('media_gallery ' )['images ' ], $ entriesById ));
570
- $ newEntry = array_replace_recursive ($ newEntry , $ finalGallery ['images ' ][$ newEntryId ]);
571
- $ entriesById [$ newEntryId ] = $ newEntry ;
572
- $ finalGallery ['images ' ][$ newEntryId ] = $ newEntry ;
573
- $ product ->setData ('media_gallery ' , $ finalGallery );
574
- }
575
501
return $ this ;
576
502
}
577
503
@@ -781,44 +707,19 @@ public function cleanCache()
781
707
$ this ->instancesById = null ;
782
708
}
783
709
784
- /**
785
- * Ascertain image roles, if they are not set against the gallery entries
786
- *
787
- * @param ProductInterface $product
788
- * @param array $images
789
- * @return array
790
- */
791
- private function determineImageRoles (ProductInterface $ product , array $ images ) : array
792
- {
793
- $ imagesWithRoles = [];
794
- foreach ($ images as $ image ) {
795
- if (!isset ($ image ['types ' ])) {
796
- $ image ['types ' ] = [];
797
- if (isset ($ image ['file ' ])) {
798
- foreach (array_keys ($ product ->getMediaAttributes ()) as $ attribute ) {
799
- if ($ image ['file ' ] == $ product ->getData ($ attribute )) {
800
- $ image ['types ' ][] = $ attribute ;
801
- }
802
- }
803
- }
804
- }
805
- $ imagesWithRoles [] = $ image ;
806
- }
807
- return $ imagesWithRoles ;
808
- }
809
-
810
710
/**
811
711
* Retrieve media gallery processor.
812
712
*
813
- * @return Product\Gallery\Processor
713
+ * @return MediaGalleryProcessor
814
714
*/
815
715
private function getMediaGalleryProcessor ()
816
716
{
817
- if (null === $ this ->mediaGalleryProcessor ) {
818
- $ this ->mediaGalleryProcessor = \Magento \Framework \App \ObjectManager::getInstance ()
819
- ->get (\ Magento \ Catalog \ Model \ Product \ Gallery \Processor ::class);
717
+ if (null === $ this ->mediaProcessor ) {
718
+ $ this ->mediaProcessor = \Magento \Framework \App \ObjectManager::getInstance ()
719
+ ->get (MediaGalleryProcessor ::class);
820
720
}
821
- return $ this ->mediaGalleryProcessor ;
721
+
722
+ return $ this ->mediaProcessor ;
822
723
}
823
724
824
725
/**
@@ -930,6 +831,7 @@ private function saveProduct($product): void
930
831
throw new CouldNotSaveException (__ ($ e ->getMessage ()));
931
832
} catch (LocalizedException $ e ) {
932
833
throw $ e ;
834
+ // phpcs:disable Magento2.Exceptions.ThrowCatch
933
835
} catch (\Exception $ e ) {
934
836
throw new CouldNotSaveException (
935
837
__ ('The product was unable to be saved. Please try again. ' ),
0 commit comments