11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2014 Adobe
4+ * All Rights Reserved .
55 */
66
77namespace Magento \ConfigurableProduct \Model ;
88
99use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory ;
10+ use Magento \Catalog \Api \Data \ProductInterface ;
11+ use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
12+ use Magento \Catalog \Api \ProductRepositoryInterface ;
1013use Magento \Catalog \Model \ProductRepository ;
14+ use Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory ;
15+ use Magento \ConfigurableProduct \Api \LinkManagementInterface ;
16+ use Magento \ConfigurableProduct \Helper \Product \Options \Factory ;
17+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable ;
18+ use Magento \Framework \Api \DataObjectHelper ;
19+ use Magento \Framework \App \ObjectManager ;
1120use Magento \Framework \Exception \InputException ;
1221use Magento \Framework \Exception \NoSuchEntityException ;
1322use Magento \Framework \Exception \StateException ;
1726 *
1827 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1928 */
20- class LinkManagement implements \ Magento \ ConfigurableProduct \ Api \ LinkManagementInterface
29+ class LinkManagement implements LinkManagementInterface
2130{
2231 /**
23- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
32+ * @var ProductRepositoryInterface
2433 */
25- private $ productRepository ;
34+ private ProductRepositoryInterface $ productRepository ;
2635
2736 /**
28- * @var \Magento\Catalog\Api\Data\ ProductInterfaceFactory
37+ * @var ProductInterfaceFactory
2938 */
30- private $ productFactory ;
39+ private ProductInterfaceFactory $ productFactory ;
3140
3241 /**
33- * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\ Configurable
42+ * @var Configurable
3443 */
35- private $ configurableType ;
44+ private Configurable $ configurableType ;
3645
3746 /**
38- * @var \Magento\Framework\Api\ DataObjectHelper
47+ * @var DataObjectHelper
3948 */
40- private $ dataObjectHelper ;
49+ private DataObjectHelper $ dataObjectHelper ;
4150
4251 /**
43- * @var \Magento\ConfigurableProduct\Helper\Product\Options\ Factory;
52+ * @var Factory;
4453 */
45- private $ optionsFactory ;
54+ private Factory $ optionsFactory ;
4655
4756 /**
48- * @var \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory
57+ * @var AttributeFactory
4958 */
50- private $ attributeFactory ;
59+ private AttributeFactory $ attributeFactory ;
5160
5261 /**
5362 * @var ProductRepository|mixed
5463 */
55- private \ Magento \ Catalog \ Model \ ProductRepository $ mediaGallery ;
64+ private ProductRepository $ mediaGallery ;
5665
5766 /**
5867 * @var ProductAttributeMediaGalleryEntryInterfaceFactory|mixed
5968 */
60- private \ Magento \ Catalog \ Api \ Data \ ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
69+ private ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
6170
6271 /**
6372 * Constructor
6473 *
65- * @param \Magento\Catalog\Api\ ProductRepositoryInterface $productRepository
66- * @param \Magento\Catalog\Api\Data\ ProductInterfaceFactory $productFactory
67- * @param \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\ Configurable $configurableType
68- * @param \Magento\Framework\Api\ DataObjectHelper $dataObjectHelper
69- * @param \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory $attributeFactory
70- * @param \Magento\Catalog\Model\ ProductRepository $mediaGalleryProcessor
71- * @param \Magento\Catalog\Api\Data\ ProductAttributeMediaGalleryEntryInterfaceFactory $myModelFactory
72- * @param \Magento\ConfigurableProduct\Helper\Product\Options\ Factory $optionsFactory
74+ * @param ProductRepositoryInterface $productRepository
75+ * @param ProductInterfaceFactory $productFactory
76+ * @param Configurable $configurableType
77+ * @param DataObjectHelper $dataObjectHelper
78+ * @param AttributeFactory|null $attributeFactory
79+ * @param ProductRepository|null $mediaGalleryProcessor
80+ * @param ProductAttributeMediaGalleryEntryInterfaceFactory|null $myModelFactory
81+ * @param Factory|null $optionsFactory
7382 */
7483 public function __construct (
75- \ Magento \ Catalog \ Api \ ProductRepositoryInterface $ productRepository ,
76- \ Magento \ Catalog \ Api \ Data \ ProductInterfaceFactory $ productFactory ,
77- \ Magento \ ConfigurableProduct \ Model \ ResourceModel \ Product \ Type \ Configurable $ configurableType ,
78- \ Magento \ Framework \ Api \ DataObjectHelper $ dataObjectHelper ,
79- ?\ Magento \ Catalog \ Model \ ResourceModel \ Eav \ AttributeFactory $ attributeFactory = null ,
80- ?\ Magento \ Catalog \ Model \ ProductRepository $ mediaGalleryProcessor = null ,
81- ?\ Magento \ Catalog \ Api \ Data \ ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory = null ,
82- ?\ Magento \ ConfigurableProduct \ Helper \ Product \ Options \ Factory $ optionsFactory = null
84+ ProductRepositoryInterface $ productRepository ,
85+ ProductInterfaceFactory $ productFactory ,
86+ Configurable $ configurableType ,
87+ DataObjectHelper $ dataObjectHelper ,
88+ ?AttributeFactory $ attributeFactory = null ,
89+ ?ProductRepository $ mediaGalleryProcessor = null ,
90+ ?ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory = null ,
91+ ?Factory $ optionsFactory = null
8392 ) {
8493 $ this ->productRepository = $ productRepository ;
8594 $ this ->productFactory = $ productFactory ;
8695 $ this ->configurableType = $ configurableType ;
8796 $ this ->dataObjectHelper = $ dataObjectHelper ;
88- $ this ->attributeFactory = $ attributeFactory ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
89- ->get (\ Magento \ Catalog \ Model \ ResourceModel \ Eav \ AttributeFactory::class);
90- $ this ->mediaGallery = $ mediaGalleryProcessor ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
91- ->get (\ Magento \ Catalog \ Model \ ProductRepository::class);
92- $ this ->myModelFactory = $ myModelFactory ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
93- ->get (\ Magento \ Catalog \ Api \ Data \ ProductAttributeMediaGalleryEntryInterfaceFactory::class);
94- $ this ->optionsFactory = $ optionsFactory ?: \ Magento \ Framework \ App \ ObjectManager::getInstance ()
95- ->get (\ Magento \ ConfigurableProduct \ Helper \ Product \ Options \ Factory::class);
97+ $ this ->attributeFactory = $ attributeFactory ?: ObjectManager::getInstance ()
98+ ->get (AttributeFactory::class);
99+ $ this ->mediaGallery = $ mediaGalleryProcessor ?: ObjectManager::getInstance ()
100+ ->get (ProductRepository::class);
101+ $ this ->myModelFactory = $ myModelFactory ?: ObjectManager::getInstance ()
102+ ->get (ProductAttributeMediaGalleryEntryInterfaceFactory::class);
103+ $ this ->optionsFactory = $ optionsFactory ?: ObjectManager::getInstance ()
104+ ->get (Factory::class);
96105 }
97106
98107 /**
@@ -102,10 +111,10 @@ public function getChildren($sku)
102111 {
103112 /** @var \Magento\Catalog\Model\Product $product */
104113 $ product = $ this ->productRepository ->get ($ sku );
105- if ($ product ->getTypeId () != \ Magento \ ConfigurableProduct \ Model \ Product \Type \Configurable::TYPE_CODE ) {
114+ if ($ product ->getTypeId () != Product \Type \Configurable::TYPE_CODE ) {
106115 return [];
107116 }
108- /** @var \Magento\ConfigurableProduct\Model\ Product\Type\Configurable $productTypeInstance */
117+ /** @var Product\Type\Configurable $productTypeInstance */
109118 $ productTypeInstance = $ product ->getTypeInstance ();
110119 $ productTypeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
111120 $ childrenList = [];
@@ -119,15 +128,12 @@ public function getChildren($sku)
119128 $ attributes [$ attrCode ] = $ value ;
120129 }
121130 }
122- $ images = (array )$ child ->getMediaGallery ('images ' );
123131 $ attributes ['store_id ' ] = $ child ->getStoreId ();
124- $ attributes ['media_gallery_entries ' ] = $ this ->getMediaEntries ($ images );
125- /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
126132 $ productDataObject = $ this ->productFactory ->create ();
127133 $ this ->dataObjectHelper ->populateWithArray (
128- $ productDataObject ,
134+ $ productDataObject-> setMediaGalleryEntries ( $ child -> getMediaGalleryEntries ()) ,
129135 $ attributes ,
130- \ Magento \ Catalog \ Api \ Data \ ProductInterface::class
136+ ProductInterface::class
131137 );
132138 $ childrenList [] = $ productDataObject ;
133139 }
@@ -139,6 +145,8 @@ public function getChildren($sku)
139145 *
140146 * @param array $images
141147 * @return array
148+ * @deprecated This approach is designed only for images
149+ * @see ProductInterface::getMediaGalleryEntries
142150 */
143151 public function getMediaEntries (array $ images ): array
144152 {
@@ -195,7 +203,7 @@ public function addChild($sku, $childSku)
195203 }
196204 $ configurableOptionData = $ this ->getConfigurableAttributesData ($ attributeData );
197205
198- /** @var \Magento\ConfigurableProduct\Helper\Product\Options\ Factory $optionFactory */
206+ /** @var Factory $optionFactory */
199207 $ optionFactory = $ this ->optionsFactory ;
200208 $ options = $ optionFactory ->create ($ configurableOptionData );
201209 $ childrenIds [] = $ child ->getId ();
@@ -216,7 +224,7 @@ public function removeChild($sku, $childSku)
216224 {
217225 $ product = $ this ->productRepository ->get ($ sku );
218226
219- if ($ product ->getTypeId () != \ Magento \ ConfigurableProduct \ Model \ Product \Type \Configurable::TYPE_CODE ) {
227+ if ($ product ->getTypeId () != Product \Type \Configurable::TYPE_CODE ) {
220228 throw new InputException (
221229 __ ('The product with the "%1" SKU isn \'t a configurable product. ' , $ sku )
222230 );
0 commit comments