1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2014 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
7
7
namespace Magento \ConfigurableProduct \Model ;
8
8
9
9
use 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 ;
10
13
use 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 ;
11
20
use Magento \Framework \Exception \InputException ;
12
21
use Magento \Framework \Exception \NoSuchEntityException ;
13
22
use Magento \Framework \Exception \StateException ;
17
26
*
18
27
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
28
*/
20
- class LinkManagement implements \ Magento \ ConfigurableProduct \ Api \ LinkManagementInterface
29
+ class LinkManagement implements LinkManagementInterface
21
30
{
22
31
/**
23
- * @var \Magento\Catalog\Api\ ProductRepositoryInterface
32
+ * @var ProductRepositoryInterface
24
33
*/
25
- private $ productRepository ;
34
+ private ProductRepositoryInterface $ productRepository ;
26
35
27
36
/**
28
- * @var \Magento\Catalog\Api\Data\ ProductInterfaceFactory
37
+ * @var ProductInterfaceFactory
29
38
*/
30
- private $ productFactory ;
39
+ private ProductInterfaceFactory $ productFactory ;
31
40
32
41
/**
33
- * @var \Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\ Configurable
42
+ * @var Configurable
34
43
*/
35
- private $ configurableType ;
44
+ private Configurable $ configurableType ;
36
45
37
46
/**
38
- * @var \Magento\Framework\Api\ DataObjectHelper
47
+ * @var DataObjectHelper
39
48
*/
40
- private $ dataObjectHelper ;
49
+ private DataObjectHelper $ dataObjectHelper ;
41
50
42
51
/**
43
- * @var \Magento\ConfigurableProduct\Helper\Product\Options\ Factory;
52
+ * @var Factory;
44
53
*/
45
- private $ optionsFactory ;
54
+ private Factory $ optionsFactory ;
46
55
47
56
/**
48
- * @var \Magento\Catalog\Model\ResourceModel\Eav\ AttributeFactory
57
+ * @var AttributeFactory
49
58
*/
50
- private $ attributeFactory ;
59
+ private AttributeFactory $ attributeFactory ;
51
60
52
61
/**
53
62
* @var ProductRepository|mixed
54
63
*/
55
- private \ Magento \ Catalog \ Model \ ProductRepository $ mediaGallery ;
64
+ private ProductRepository $ mediaGallery ;
56
65
57
66
/**
58
67
* @var ProductAttributeMediaGalleryEntryInterfaceFactory|mixed
59
68
*/
60
- private \ Magento \ Catalog \ Api \ Data \ ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
69
+ private ProductAttributeMediaGalleryEntryInterfaceFactory $ myModelFactory ;
61
70
62
71
/**
63
72
* Constructor
64
73
*
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
73
82
*/
74
83
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
83
92
) {
84
93
$ this ->productRepository = $ productRepository ;
85
94
$ this ->productFactory = $ productFactory ;
86
95
$ this ->configurableType = $ configurableType ;
87
96
$ 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);
96
105
}
97
106
98
107
/**
@@ -102,10 +111,10 @@ public function getChildren($sku)
102
111
{
103
112
/** @var \Magento\Catalog\Model\Product $product */
104
113
$ 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 ) {
106
115
return [];
107
116
}
108
- /** @var \Magento\ConfigurableProduct\Model\ Product\Type\Configurable $productTypeInstance */
117
+ /** @var Product\Type\Configurable $productTypeInstance */
109
118
$ productTypeInstance = $ product ->getTypeInstance ();
110
119
$ productTypeInstance ->setStoreFilter ($ product ->getStoreId (), $ product );
111
120
$ childrenList = [];
@@ -119,15 +128,12 @@ public function getChildren($sku)
119
128
$ attributes [$ attrCode ] = $ value ;
120
129
}
121
130
}
122
- $ images = (array )$ child ->getMediaGallery ('images ' );
123
131
$ attributes ['store_id ' ] = $ child ->getStoreId ();
124
- $ attributes ['media_gallery_entries ' ] = $ this ->getMediaEntries ($ images );
125
- /** @var \Magento\Catalog\Api\Data\ProductInterface $productDataObject */
126
132
$ productDataObject = $ this ->productFactory ->create ();
127
133
$ this ->dataObjectHelper ->populateWithArray (
128
- $ productDataObject ,
134
+ $ productDataObject-> setMediaGalleryEntries ( $ child -> getMediaGalleryEntries ()) ,
129
135
$ attributes ,
130
- \ Magento \ Catalog \ Api \ Data \ ProductInterface::class
136
+ ProductInterface::class
131
137
);
132
138
$ childrenList [] = $ productDataObject ;
133
139
}
@@ -139,6 +145,8 @@ public function getChildren($sku)
139
145
*
140
146
* @param array $images
141
147
* @return array
148
+ * @deprecated This approach is designed only for images
149
+ * @see ProductInterface::getMediaGalleryEntries
142
150
*/
143
151
public function getMediaEntries (array $ images ): array
144
152
{
@@ -195,7 +203,7 @@ public function addChild($sku, $childSku)
195
203
}
196
204
$ configurableOptionData = $ this ->getConfigurableAttributesData ($ attributeData );
197
205
198
- /** @var \Magento\ConfigurableProduct\Helper\Product\Options\ Factory $optionFactory */
206
+ /** @var Factory $optionFactory */
199
207
$ optionFactory = $ this ->optionsFactory ;
200
208
$ options = $ optionFactory ->create ($ configurableOptionData );
201
209
$ childrenIds [] = $ child ->getId ();
@@ -216,7 +224,7 @@ public function removeChild($sku, $childSku)
216
224
{
217
225
$ product = $ this ->productRepository ->get ($ sku );
218
226
219
- if ($ product ->getTypeId () != \ Magento \ ConfigurableProduct \ Model \ Product \Type \Configurable::TYPE_CODE ) {
227
+ if ($ product ->getTypeId () != Product \Type \Configurable::TYPE_CODE ) {
220
228
throw new InputException (
221
229
__ ('The product with the "%1" SKU isn \'t a configurable product. ' , $ sku )
222
230
);
0 commit comments