1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2025 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
7
7
namespace Magento \ConfigurableProduct \Model ;
8
8
9
- use Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory ;
9
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
+ use Magento \Catalog \Api \Data \ProductInterfaceFactory ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
10
12
use Magento \Catalog \Model \ProductRepository ;
13
+ use Magento \Catalog \Model \ResourceModel \Eav \AttributeFactory ;
14
+ use Magento \ConfigurableProduct \Api \LinkManagementInterface ;
15
+ use Magento \ConfigurableProduct \Helper \Product \Options \Factory ;
16
+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable ;
17
+ use Magento \Framework \Api \DataObjectHelper ;
11
18
use Magento \Framework \Exception \InputException ;
12
19
use Magento \Framework \Exception \NoSuchEntityException ;
13
20
use Magento \Framework \Exception \StateException ;
14
21
15
22
/**
16
23
* Configurable product link management.
17
24
*
18
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
25
*/
20
- class LinkManagement implements \ Magento \ ConfigurableProduct \ Api \ LinkManagementInterface
26
+ class LinkManagement implements LinkManagementInterface
21
27
{
22
28
/**
23
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
29
+ * @var ProductRepositoryInterface
24
30
*/
25
- private $ productRepository ;
31
+ private ProductRepositoryInterface $ productRepository ;
26
32
27
33
/**
28
- * @var \Magento\Catalog\Api\Data\ ProductInterfaceFactory
34
+ * @var ProductInterfaceFactory
29
35
*/
30
- private $ productFactory ;
36
+ private ProductInterfaceFactory $ productFactory ;
31
37
32
38
/**
33
- * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\ Configurable
39
+ * @var Configurable
34
40
*/
35
- private $ configurableType ;
41
+ private Configurable $ configurableType ;
36
42
37
43
/**
38
- * @var \Magento\Framework\Api\ DataObjectHelper
44
+ * @var DataObjectHelper
39
45
*/
40
- private $ dataObjectHelper ;
46
+ private DataObjectHelper $ dataObjectHelper ;
41
47
42
48
/**
43
- * @var \Magento\ConfigurableProduct\Helper\Product\Options\ Factory;
49
+ * @var Factory;
44
50
*/
45
- private $ optionsFactory ;
51
+ private Factory $ optionsFactory ;
46
52
47
53
/**
48
- * @var \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory
54
+ * @var AttributeFactory
49
55
*/
50
- private $ attributeFactory ;
56
+ private AttributeFactory $ attributeFactory ;
51
57
52
58
/**
53
59
* @var ProductRepository|mixed
54
60
*/
55
- private \Magento \Catalog \Model \ProductRepository $ mediaGallery ;
56
-
57
- /**
58
- * @var ProductAttributeMediaGalleryEntryInterfaceFactory|mixed
59
- */
60
- private \Magento \Catalog \Api \Data \ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
61
+ private ProductRepository $ mediaGallery ;
61
62
62
63
/**
63
64
* Constructor
64
65
*
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
66
+ * @param ProductRepositoryInterface $productRepository
67
+ * @param ProductInterfaceFactory $productFactory
68
+ * @param Configurable $configurableType
69
+ * @param DataObjectHelper $dataObjectHelper
70
+ * @param AttributeFactory|null $attributeFactory
71
+ * @param ProductRepository|null $mediaGalleryProcessor
72
+ * @param Factory|null $optionsFactory
73
73
*/
74
74
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
75
+ ProductRepositoryInterface $ productRepository ,
76
+ ProductInterfaceFactory $ productFactory ,
77
+ Configurable $ configurableType ,
78
+ DataObjectHelper $ dataObjectHelper ,
79
+ ?AttributeFactory $ attributeFactory = null ,
80
+ ?ProductRepository $ mediaGalleryProcessor = null ,
81
+ ?Factory $ optionsFactory = null
83
82
) {
84
83
$ this ->productRepository = $ productRepository ;
85
84
$ this ->productFactory = $ productFactory ;
86
85
$ this ->configurableType = $ configurableType ;
87
86
$ this ->dataObjectHelper = $ dataObjectHelper ;
88
87
$ this ->attributeFactory = $ attributeFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
89
- ->get (\ Magento \ Catalog \ Model \ ResourceModel \ Eav \ AttributeFactory::class);
88
+ ->get (AttributeFactory::class);
90
89
$ 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);
90
+ ->get (ProductRepository::class);
94
91
$ this ->optionsFactory = $ optionsFactory ?: \Magento \Framework \App \ObjectManager::getInstance ()
95
- ->get (\ Magento \ ConfigurableProduct \ Helper \ Product \ Options \ Factory::class);
92
+ ->get (Factory::class);
96
93
}
97
94
98
95
/**
@@ -102,10 +99,10 @@ public function getChildren($sku)
102
99
{
103
100
/** @var \Magento\Catalog\Model\Product $product */
104
101
$ product = $ this ->productRepository ->get ($ sku );
105
- if ($ product ->getTypeId () != \ Magento \ ConfigurableProduct \ Model \ Product \Type \Configurable::TYPE_CODE ) {
102
+ if ($ product ->getTypeId () != Product \Type \Configurable::TYPE_CODE ) {
106
103
return [];
107
104
}
108
- /** @var \Magento\ConfigurableProduct\Model\ Product\Type\Configurable $productTypeInstance */
105
+ /** @var Product\Type\Configurable $productTypeInstance */
109
106
$ productTypeInstance = $ product ->getTypeInstance ();
110
107
$ productTypeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
111
108
$ childrenList = [];
@@ -119,43 +116,18 @@ public function getChildren($sku)
119
116
$ attributes [$ attrCode ] = $ value ;
120
117
}
121
118
}
122
- $ images = (array )$ child ->getMediaGallery ('images ' );
123
119
$ attributes ['store_id ' ] = $ child ->getStoreId ();
124
- $ attributes ['media_gallery_entries ' ] = $ this ->getMediaEntries ($ images );
125
- /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
126
120
$ productDataObject = $ this ->productFactory ->create ();
127
121
$ this ->dataObjectHelper ->populateWithArray (
128
- $ productDataObject ,
122
+ $ productDataObject-> setMediaGalleryEntries ( $ child -> getMediaGalleryEntries ()) ,
129
123
$ attributes ,
130
- \ Magento \ Catalog \ Api \ Data \ ProductInterface::class
124
+ ProductInterface::class
131
125
);
132
126
$ childrenList [] = $ productDataObject ;
133
127
}
134
128
return $ childrenList ;
135
129
}
136
130
137
- /**
138
- * Get media entries
139
- *
140
- * @param array $images
141
- * @return array
142
- */
143
- public function getMediaEntries (array $ images ): array
144
- {
145
- $ media = $ this ->myModelFactory ->create ();
146
- $ mediaGalleryEntries =[];
147
- foreach ($ images as $ image ) {
148
- $ media ->setId ($ image ["value_id " ]);
149
- $ media ->setMediaType ($ image ["media_type " ]);
150
- $ media ->setLabel ($ image ["label " ]);
151
- $ media ->setPosition ($ image ["position " ]);
152
- $ media ->setDisabled ($ image ["disabled " ]);
153
- $ media ->setFile ($ image ["file " ]);
154
- $ mediaGalleryEntries []=$ media ->getData ();
155
- }
156
- return $ mediaGalleryEntries ;
157
- }
158
-
159
131
/**
160
132
* @inheritdoc
161
133
* @throws InputException
@@ -195,7 +167,7 @@ public function addChild($sku, $childSku)
195
167
}
196
168
$ configurableOptionData = $ this ->getConfigurableAttributesData ($ attributeData );
197
169
198
- /** @var \Magento\ConfigurableProduct\Helper\Product\Options\ Factory $optionFactory */
170
+ /** @var Factory $optionFactory */
199
171
$ optionFactory = $ this ->optionsFactory ;
200
172
$ options = $ optionFactory ->create ($ configurableOptionData );
201
173
$ childrenIds [] = $ child ->getId ();
@@ -216,7 +188,7 @@ public function removeChild($sku, $childSku)
216
188
{
217
189
$ product = $ this ->productRepository ->get ($ sku );
218
190
219
- if ($ product ->getTypeId () != \ Magento \ ConfigurableProduct \ Model \ Product \Type \Configurable::TYPE_CODE ) {
191
+ if ($ product ->getTypeId () != Product \Type \Configurable::TYPE_CODE ) {
220
192
throw new InputException (
221
193
__ ('The product with the "%1" SKU isn \'t a configurable product. ' , $ sku )
222
194
);
0 commit comments