7
7
8
8
namespace Magento \Bundle \Model ;
9
9
10
+ use Magento \Bundle \Model \Option \SaveAction ;
10
11
use Magento \Catalog \Api \Data \ProductInterface ;
11
- use Magento \Framework \App \ObjectManager ;
12
- use Magento \Framework \EntityManager \MetadataPool ;
13
- use Magento \Framework \Exception \CouldNotSaveException ;
14
12
use Magento \Framework \Exception \InputException ;
15
13
use Magento \Framework \Exception \NoSuchEntityException ;
16
14
17
15
/**
16
+ * Repository for performing CRUD operations for a bundle product's options.
17
+ *
18
18
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
19
19
*/
20
20
class OptionRepository implements \Magento \Bundle \Api \ProductOptionRepositoryInterface
@@ -39,11 +39,6 @@ class OptionRepository implements \Magento\Bundle\Api\ProductOptionRepositoryInt
39
39
*/
40
40
protected $ optionResource ;
41
41
42
- /**
43
- * @var \Magento\Store\Model\StoreManager
44
- */
45
- protected $ storeManager ;
46
-
47
42
/**
48
43
* @var \Magento\Bundle\Api\ProductLinkManagementInterface
49
44
*/
@@ -54,53 +49,44 @@ class OptionRepository implements \Magento\Bundle\Api\ProductOptionRepositoryInt
54
49
*/
55
50
protected $ productOptionList ;
56
51
57
- /**
58
- * @var Product\LinksList
59
- */
60
- protected $ linkList ;
61
-
62
52
/**
63
53
* @var \Magento\Framework\Api\DataObjectHelper
64
54
*/
65
55
protected $ dataObjectHelper ;
66
56
67
57
/**
68
- * @var \Magento\Framework\EntityManager\MetadataPool
58
+ * @var SaveAction
69
59
*/
70
- private $ metadataPool ;
60
+ private $ optionSave ;
71
61
72
62
/**
73
63
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
74
64
* @param Product\Type $type
75
65
* @param \Magento\Bundle\Api\Data\OptionInterfaceFactory $optionFactory
76
66
* @param \Magento\Bundle\Model\ResourceModel\Option $optionResource
77
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
78
67
* @param \Magento\Bundle\Api\ProductLinkManagementInterface $linkManagement
79
68
* @param Product\OptionList $productOptionList
80
- * @param Product\LinksList $linkList
81
69
* @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
82
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
70
+ * @param SaveAction $optionSave
83
71
*/
84
72
public function __construct (
85
73
\Magento \Catalog \Api \ProductRepositoryInterface $ productRepository ,
86
74
\Magento \Bundle \Model \Product \Type $ type ,
87
75
\Magento \Bundle \Api \Data \OptionInterfaceFactory $ optionFactory ,
88
76
\Magento \Bundle \Model \ResourceModel \Option $ optionResource ,
89
- \Magento \Store \Model \StoreManagerInterface $ storeManager ,
90
77
\Magento \Bundle \Api \ProductLinkManagementInterface $ linkManagement ,
91
78
\Magento \Bundle \Model \Product \OptionList $ productOptionList ,
92
- \Magento \Bundle \ Model \ Product \ LinksList $ linkList ,
93
- \ Magento \ Framework \ Api \ DataObjectHelper $ dataObjectHelper
79
+ \Magento \Framework \ Api \ DataObjectHelper $ dataObjectHelper ,
80
+ SaveAction $ optionSave
94
81
) {
95
82
$ this ->productRepository = $ productRepository ;
96
83
$ this ->type = $ type ;
97
84
$ this ->optionFactory = $ optionFactory ;
98
85
$ this ->optionResource = $ optionResource ;
99
- $ this ->storeManager = $ storeManager ;
100
86
$ this ->linkManagement = $ linkManagement ;
101
87
$ this ->productOptionList = $ productOptionList ;
102
- $ this ->linkList = $ linkList ;
103
88
$ this ->dataObjectHelper = $ dataObjectHelper ;
89
+ $ this ->optionSave = $ optionSave ;
104
90
}
105
91
106
92
/**
@@ -118,7 +104,7 @@ public function get($sku, $optionId)
118
104
);
119
105
}
120
106
121
- $ productLinks = $ this ->linkList -> getItems ($ product , $ optionId );
107
+ $ productLinks = $ this ->linkManagement -> getChildren ($ product-> getSku () , $ optionId );
122
108
123
109
/** @var \Magento\Bundle\Api\Data\OptionInterface $option */
124
110
$ optionDataObject = $ this ->optionFactory ->create ();
@@ -177,7 +163,9 @@ public function deleteById($sku, $optionId)
177
163
$ product = $ this ->getProduct ($ sku );
178
164
$ optionCollection = $ this ->type ->getOptionsCollection ($ product );
179
165
$ optionCollection ->setIdFilter ($ optionId );
180
- return $ this ->delete ($ optionCollection ->getFirstItem ());
166
+ $ hasBeenDeleted = $ this ->delete ($ optionCollection ->getFirstItem ());
167
+
168
+ return $ hasBeenDeleted ;
181
169
}
182
170
183
171
/**
@@ -187,51 +175,12 @@ public function save(
187
175
\Magento \Catalog \Api \Data \ProductInterface $ product ,
188
176
\Magento \Bundle \Api \Data \OptionInterface $ option
189
177
) {
190
- $ metadata = $ this ->getMetadataPool ()->getMetadata (\Magento \Catalog \Api \Data \ProductInterface::class);
191
-
192
- $ option ->setStoreId ($ product ->getStoreId ());
193
- $ parentId = $ product ->getData ($ metadata ->getLinkField ());
194
- $ option ->setParentId ($ parentId );
195
-
196
- $ optionId = $ option ->getOptionId ();
197
- $ linksToAdd = [];
198
- $ optionCollection = $ this ->type ->getOptionsCollection ($ product );
199
- $ optionCollection ->setIdFilter ($ option ->getOptionId ());
200
- $ optionCollection ->setProductLinkFilter ($ parentId );
201
-
202
- /** @var \Magento\Bundle\Model\Option $existingOption */
203
- $ existingOption = $ optionCollection ->getFirstItem ();
204
- if (!$ optionId || $ existingOption ->getParentId () != $ parentId ) {
205
- //If option ID is empty or existing option's parent ID is different
206
- //we'd need a new ID for the option.
207
- $ option ->setOptionId (null );
208
- $ option ->setDefaultTitle ($ option ->getTitle ());
209
- if (is_array ($ option ->getProductLinks ())) {
210
- $ linksToAdd = $ option ->getProductLinks ();
211
- }
212
- } else {
213
- if (!$ existingOption ->getOptionId ()) {
214
- throw new NoSuchEntityException (
215
- __ ("The option that was requested doesn't exist. Verify the entity and try again. " )
216
- );
217
- }
178
+ $ savedOption = $ this ->optionSave ->save ($ product , $ option );
218
179
219
- $ option ->setData (array_merge ($ existingOption ->getData (), $ option ->getData ()));
220
- $ this ->updateOptionSelection ($ product , $ option );
221
- }
180
+ $ productToSave = $ this ->productRepository ->get ($ product ->getSku ());
181
+ $ this ->productRepository ->save ($ productToSave );
222
182
223
- try {
224
- $ this ->optionResource ->save ($ option );
225
- } catch (\Exception $ e ) {
226
- throw new CouldNotSaveException (__ ("The option couldn't be saved. " ), $ e );
227
- }
228
-
229
- /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
230
- foreach ($ linksToAdd as $ linkedProduct ) {
231
- $ this ->linkManagement ->addChild ($ product , $ option ->getOptionId (), $ linkedProduct );
232
- }
233
- $ product ->setIsRelationsChanged (true );
234
- return $ option ->getOptionId ();
183
+ return $ savedOption ->getOptionId ();
235
184
}
236
185
237
186
/**
@@ -325,16 +274,4 @@ private function compareLinks(array $firstArray, array $secondArray)
325
274
326
275
return $ result ;
327
276
}
328
-
329
- /**
330
- * Get MetadataPool instance
331
- * @return MetadataPool
332
- */
333
- private function getMetadataPool ()
334
- {
335
- if (!$ this ->metadataPool ) {
336
- $ this ->metadataPool = ObjectManager::getInstance ()->get (MetadataPool::class);
337
- }
338
- return $ this ->metadataPool ;
339
- }
340
277
}
0 commit comments