7
7
8
8
namespace Magento \Catalog \Model ;
9
9
10
+ use Magento \Catalog \Model \CategoryRepository \PopulateWithValues ;
11
+ use Magento \Catalog \Model \ResourceModel \Category as CategoryResource ;
12
+ use Magento \Framework \Api \ExtensibleDataObjectConverter ;
13
+ use Magento \Framework \App \ObjectManager ;
14
+ use Magento \Framework \EntityManager \MetadataPool ;
10
15
use Magento \Framework \Exception \CouldNotSaveException ;
11
16
use Magento \Framework \Exception \NoSuchEntityException ;
12
17
use Magento \Framework \Exception \StateException ;
13
18
use Magento \Catalog \Api \Data \CategoryInterface ;
19
+ use Magento \Store \Model \StoreManagerInterface ;
14
20
15
21
/**
16
22
* Repository for categories.
@@ -25,27 +31,27 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
25
31
protected $ instances = [];
26
32
27
33
/**
28
- * @var \Magento\Store\Model\ StoreManagerInterface
34
+ * @var StoreManagerInterface
29
35
*/
30
36
protected $ storeManager ;
31
37
32
38
/**
33
- * @var \Magento\Catalog\Model\ CategoryFactory
39
+ * @var CategoryFactory
34
40
*/
35
41
protected $ categoryFactory ;
36
42
37
43
/**
38
- * @var \Magento\Catalog\Model\ResourceModel\Category
44
+ * @var CategoryResource
39
45
*/
40
46
protected $ categoryResource ;
41
47
42
48
/**
43
- * @var \Magento\Framework\EntityManager\ MetadataPool
49
+ * @var MetadataPool
44
50
*/
45
51
protected $ metadataPool ;
46
52
47
53
/**
48
- * @var \Magento\Framework\Api\ ExtensibleDataObjectConverter
54
+ * @var ExtensibleDataObjectConverter
49
55
*/
50
56
private $ extensibleDataObjectConverter ;
51
57
@@ -57,28 +63,37 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
57
63
protected $ useConfigFields = ['available_sort_by ' , 'default_sort_by ' , 'filter_price_range ' ];
58
64
59
65
/**
60
- * @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
61
- * @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
62
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
66
+ * @var PopulateWithValues
67
+ */
68
+ private $ populateWithValues ;
69
+
70
+ /**
71
+ * @param CategoryFactory $categoryFactory
72
+ * @param CategoryResource $categoryResource
73
+ * @param StoreManagerInterface $storeManager
74
+ * @param PopulateWithValues|null $populateWithValues
63
75
*/
64
76
public function __construct (
65
- \Magento \Catalog \Model \CategoryFactory $ categoryFactory ,
66
- \Magento \Catalog \Model \ResourceModel \Category $ categoryResource ,
67
- \Magento \Store \Model \StoreManagerInterface $ storeManager
77
+ CategoryFactory $ categoryFactory ,
78
+ CategoryResource $ categoryResource ,
79
+ StoreManagerInterface $ storeManager ,
80
+ ?PopulateWithValues $ populateWithValues
68
81
) {
69
82
$ this ->categoryFactory = $ categoryFactory ;
70
83
$ this ->categoryResource = $ categoryResource ;
71
84
$ this ->storeManager = $ storeManager ;
85
+ $ objectManager = ObjectManager::getInstance ();
86
+ $ this ->populateWithValues = $ populateWithValues ?? $ objectManager ->get (PopulateWithValues::class);
72
87
}
73
88
74
89
/**
75
90
* @inheritdoc
76
91
*/
77
- public function save (\ Magento \ Catalog \ Api \ Data \ CategoryInterface $ category )
92
+ public function save (CategoryInterface $ category )
78
93
{
79
94
$ storeId = (int )$ this ->storeManager ->getStore ()->getId ();
80
95
$ existingData = $ this ->getExtensibleDataObjectConverter ()
81
- ->toNestedArray ($ category , [], \ Magento \ Catalog \ Api \ Data \ CategoryInterface::class);
96
+ ->toNestedArray ($ category , [], CategoryInterface::class);
82
97
$ existingData = array_diff_key ($ existingData , array_flip (['path ' , 'level ' , 'parent_id ' ]));
83
98
$ existingData ['store_id ' ] = $ storeId ;
84
99
@@ -110,7 +125,7 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
110
125
$ existingData ['parent_id ' ] = $ parentId ;
111
126
$ existingData ['level ' ] = null ;
112
127
}
113
- $ category -> addData ( $ existingData );
128
+ $ this -> populateWithValues -> execute ( $ category , $ existingData );
114
129
try {
115
130
$ this ->validateCategory ($ category );
116
131
$ this ->categoryResource ->save ($ category );
@@ -151,7 +166,7 @@ public function get($categoryId, $storeId = null)
151
166
/**
152
167
* @inheritdoc
153
168
*/
154
- public function delete (\ Magento \ Catalog \ Api \ Data \ CategoryInterface $ category )
169
+ public function delete (CategoryInterface $ category )
155
170
{
156
171
try {
157
172
$ categoryId = $ category ->getId ();
@@ -213,29 +228,29 @@ protected function validateCategory(Category $category)
213
228
/**
214
229
* Lazy loader for the converter.
215
230
*
216
- * @return \Magento\Framework\Api\ ExtensibleDataObjectConverter
231
+ * @return ExtensibleDataObjectConverter
217
232
*
218
233
* @deprecated 101.0.0
219
234
*/
220
235
private function getExtensibleDataObjectConverter ()
221
236
{
222
237
if ($ this ->extensibleDataObjectConverter === null ) {
223
- $ this ->extensibleDataObjectConverter = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
224
- ->get (\ Magento \ Framework \ Api \ ExtensibleDataObjectConverter::class);
238
+ $ this ->extensibleDataObjectConverter = ObjectManager::getInstance ()
239
+ ->get (ExtensibleDataObjectConverter::class);
225
240
}
226
241
return $ this ->extensibleDataObjectConverter ;
227
242
}
228
243
229
244
/**
230
245
* Lazy loader for the metadata pool.
231
246
*
232
- * @return \Magento\Framework\EntityManager\ MetadataPool
247
+ * @return MetadataPool
233
248
*/
234
249
private function getMetadataPool ()
235
250
{
236
251
if (null === $ this ->metadataPool ) {
237
- $ this ->metadataPool = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
238
- ->get (\ Magento \ Framework \ EntityManager \ MetadataPool::class);
252
+ $ this ->metadataPool = ObjectManager::getInstance ()
253
+ ->get (MetadataPool::class);
239
254
}
240
255
return $ this ->metadataPool ;
241
256
}
0 commit comments