@@ -64,7 +64,7 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ
64
64
protected $ attributeGroupFactory ;
65
65
66
66
/**
67
- * @var AttributeManagementInterfaces
67
+ * @var AttributeManagementInterface
68
68
*/
69
69
protected $ attributeManagement ;
70
70
@@ -82,42 +82,15 @@ class AddAttributeToTemplate extends \Magento\Catalog\Controller\Adminhtml\Produ
82
82
* @param \Magento\Backend\App\Action\Context $context
83
83
* @param Builder $productBuilder
84
84
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
85
- * @param AttributeRepositoryInterface $attributeRepository
86
- * @param AttributeSetRepositoryInterface $attributeSetRepository
87
- * @param AttributeGroupRepositoryInterface $attributeGroupRepository
88
- * @param AttributeGroupInterfaceFactory $attributeGroupFactory
89
- * @param SearchCriteriaBuilder $searchCriteriaBuilder
90
- * @param SortOrderBuilder $sortOrderBuilder
91
- * @param AttributeManagementInterface $attributeManagement
92
- * @param LoggerInterface $logger
93
- * @param ExtensionAttributesFactory $extensionAttributesFactory
94
85
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
95
86
*/
96
87
public function __construct (
97
88
\Magento \Backend \App \Action \Context $ context ,
98
89
\Magento \Catalog \Controller \Adminhtml \Product \Builder $ productBuilder ,
99
- \Magento \Framework \Controller \Result \JsonFactory $ resultJsonFactory ,
100
- AttributeRepositoryInterface $ attributeRepository ,
101
- AttributeSetRepositoryInterface $ attributeSetRepository ,
102
- AttributeGroupRepositoryInterface $ attributeGroupRepository ,
103
- AttributeGroupInterfaceFactory $ attributeGroupFactory ,
104
- SearchCriteriaBuilder $ searchCriteriaBuilder ,
105
- SortOrderBuilder $ sortOrderBuilder ,
106
- AttributeManagementInterface $ attributeManagement ,
107
- LoggerInterface $ logger ,
108
- ExtensionAttributesFactory $ extensionAttributesFactory
90
+ \Magento \Framework \Controller \Result \JsonFactory $ resultJsonFactory
109
91
) {
110
92
parent ::__construct ($ context , $ productBuilder );
111
93
$ this ->resultJsonFactory = $ resultJsonFactory ;
112
- $ this ->attributeRepository = $ attributeRepository ;
113
- $ this ->attributeSetRepository = $ attributeSetRepository ;
114
- $ this ->attributeGroupRepository = $ attributeGroupRepository ;
115
- $ this ->attributeGroupFactory = $ attributeGroupFactory ;
116
- $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
117
- $ this ->sortOrderBuilder = $ sortOrderBuilder ;
118
- $ this ->attributeManagement = $ attributeManagement ;
119
- $ this ->logger = $ logger ;
120
- $ this ->extensionAttributesFactory = $ extensionAttributesFactory ;
121
94
}
122
95
123
96
/**
@@ -133,22 +106,22 @@ public function execute()
133
106
134
107
try {
135
108
/** @var AttributeSetInterface $attributeSet */
136
- $ attributeSet = $ this ->attributeSetRepository ->get ($ request ->getParam ('templateId ' ));
109
+ $ attributeSet = $ this ->getAttributeSetRepository () ->get ($ request ->getParam ('templateId ' ));
137
110
$ groupCode = $ request ->getParam ('groupCode ' );
138
111
$ groupName = $ request ->getParam ('groupName ' );
139
112
$ groupSortOrder = $ request ->getParam ('groupSortOrder ' );
140
113
141
114
$ attributeSearchCriteria = $ this ->getBasicAttributeSearchCriteriaBuilder ()->create ();
142
- $ attributeGroupSearchCriteria = $ this ->searchCriteriaBuilder
115
+ $ attributeGroupSearchCriteria = $ this ->getSearchCriteriaBuilder ()
143
116
->addFilter ('attribute_set_id ' , $ attributeSet ->getAttributeSetId ())
144
117
->addFilter ('attribute_group_code ' , $ groupCode )
145
- ->addSortOrder ($ this ->sortOrderBuilder ->setAscendingDirection ()->create ())
118
+ ->addSortOrder ($ this ->getSortOrderBuilder () ->setAscendingDirection ()->create ())
146
119
->setPageSize (1 )
147
120
->create ();
148
121
149
122
try {
150
123
/** @var AttributeGroupInterface[] $attributeGroupItems */
151
- $ attributeGroupItems = $ this ->attributeGroupRepository ->getList ($ attributeGroupSearchCriteria )
124
+ $ attributeGroupItems = $ this ->getAttributeGroupRepository () ->getList ($ attributeGroupSearchCriteria )
152
125
->getItems ();
153
126
154
127
if (!$ attributeGroupItems ) {
@@ -159,28 +132,28 @@ public function execute()
159
132
$ attributeGroup = reset ($ attributeGroupItems );
160
133
} catch (\Magento \Framework \Exception \NoSuchEntityException $ e ) {
161
134
/** @var AttributeGroupInterface $attributeGroup */
162
- $ attributeGroup = $ this ->attributeGroupFactory ->create ();
135
+ $ attributeGroup = $ this ->getAttributeGroupFactory () ->create ();
163
136
}
164
137
165
138
$ extensionAttributes = $ attributeGroup ->getExtensionAttributes ()
166
- ?: $ this ->extensionAttributesFactory ->create (AttributeGroupInterface::class);
139
+ ?: $ this ->getExtensionAttributesFactory () ->create (AttributeGroupInterface::class);
167
140
168
141
$ extensionAttributes ->setAttributeGroupCode ($ groupCode );
169
142
$ extensionAttributes ->setSortOrder ($ groupSortOrder );
170
143
$ attributeGroup ->setAttributeGroupName ($ groupName );
171
144
$ attributeGroup ->setAttributeSetId ($ attributeSet ->getAttributeSetId ());
172
145
$ attributeGroup ->setExtensionAttributes ($ extensionAttributes );
173
146
174
- $ this ->attributeGroupRepository ->save ($ attributeGroup );
147
+ $ this ->getAttributeGroupRepository () ->save ($ attributeGroup );
175
148
176
149
/** @var AttributeInterface[] $attributesItems */
177
- $ attributesItems = $ this ->attributeRepository ->getList (
150
+ $ attributesItems = $ this ->getAttributeRepository () ->getList (
178
151
ProductAttributeInterface::ENTITY_TYPE_CODE ,
179
152
$ attributeSearchCriteria
180
153
)->getItems ();
181
154
182
155
array_walk ($ attributesItems , function (AttributeInterface $ attribute ) use ($ attributeSet , $ attributeGroup ) {
183
- $ this ->attributeManagement ->assign (
156
+ $ this ->getAttributeManagement () ->assign (
184
157
ProductAttributeInterface::ENTITY_TYPE_CODE ,
185
158
$ attributeSet ->getAttributeSetId (),
186
159
$ attributeGroup ->getAttributeGroupId (),
@@ -192,7 +165,7 @@ public function execute()
192
165
$ response ->setError (true );
193
166
$ response ->setMessage ($ e ->getMessage ());
194
167
} catch (\Exception $ e ) {
195
- $ this ->logger ->critical ($ e );
168
+ $ this ->getLogger () ->critical ($ e );
196
169
$ response ->setError (true );
197
170
$ response ->setMessage (__ ('Unable to add attribute ' ));
198
171
}
@@ -214,8 +187,116 @@ private function getBasicAttributeSearchCriteriaBuilder()
214
187
throw new LocalizedException (__ ('Please, specify attributes ' ));
215
188
}
216
189
217
- return $ this ->searchCriteriaBuilder
190
+ return $ this ->getSearchCriteriaBuilder ()
218
191
->addFilter ('attribute_set_id ' , new \Zend_Db_Expr ('null ' ), 'is ' )
219
192
->addFilter ('attribute_id ' , [$ attributeIds ['selected ' ]], 'in ' );
220
193
}
194
+
195
+ /**
196
+ * @return AttributeRepositoryInterface
197
+ */
198
+ private function getAttributeRepository ()
199
+ {
200
+ if (null === $ this ->attributeRepository ) {
201
+ $ this ->attributeRepository = \Magento \Framework \App \ObjectManager::getInstance ()
202
+ ->get ('Magento\Eav\Api\AttributeRepositoryInterface ' );
203
+ }
204
+ return $ this ->attributeRepository ;
205
+ }
206
+
207
+ /**
208
+ * @return AttributeSetRepositoryInterface
209
+ */
210
+ private function getAttributeSetRepository ()
211
+ {
212
+ if (null === $ this ->attributeSetRepository ) {
213
+ $ this ->attributeSetRepository = \Magento \Framework \App \ObjectManager::getInstance ()
214
+ ->get ('Magento\Catalog\Api\AttributeSetRepositoryInterface ' );
215
+ }
216
+ return $ this ->attributeSetRepository ;
217
+ }
218
+
219
+ /**
220
+ * @return AttributeGroupRepositoryInterface
221
+ */
222
+ private function getAttributeGroupRepository ()
223
+ {
224
+ if (null === $ this ->attributeGroupRepository ) {
225
+ $ this ->attributeGroupRepository = \Magento \Framework \App \ObjectManager::getInstance ()
226
+ ->get ('Magento\Eav\Api\AttributeGroupRepositoryInterface ' );
227
+ }
228
+ return $ this ->attributeGroupRepository ;
229
+ }
230
+
231
+ /**
232
+ * @return AttributeGroupInterfaceFactory
233
+ */
234
+ private function getAttributeGroupFactory ()
235
+ {
236
+ if (null === $ this ->attributeGroupFactory ) {
237
+ $ this ->attributeGroupFactory = \Magento \Framework \App \ObjectManager::getInstance ()
238
+ ->get ('Magento\Eav\Api\Data\AttributeGroupInterfaceFactory ' );
239
+ }
240
+ return $ this ->attributeGroupFactory ;
241
+ }
242
+
243
+ /**
244
+ * @return SearchCriteriaBuilder
245
+ */
246
+ private function getSearchCriteriaBuilder ()
247
+ {
248
+ if (null === $ this ->searchCriteriaBuilder ) {
249
+ $ this ->searchCriteriaBuilder = \Magento \Framework \App \ObjectManager::getInstance ()
250
+ ->get ('Magento\Framework\Api\SearchCriteriaBuilder ' );
251
+ }
252
+ return $ this ->searchCriteriaBuilder ;
253
+ }
254
+
255
+ /**
256
+ * @return SortOrderBuilder
257
+ */
258
+ private function getSortOrderBuilder ()
259
+ {
260
+ if (null === $ this ->sortOrderBuilder ) {
261
+ $ this ->sortOrderBuilder = \Magento \Framework \App \ObjectManager::getInstance ()
262
+ ->get ('Magento\Framework\Api\SortOrderBuilder ' );
263
+ }
264
+ return $ this ->sortOrderBuilder ;
265
+ }
266
+
267
+ /**
268
+ * @return AttributeManagementInterface
269
+ */
270
+ private function getAttributeManagement ()
271
+ {
272
+ if (null === $ this ->attributeManagement ) {
273
+ $ this ->attributeManagement = \Magento \Framework \App \ObjectManager::getInstance ()
274
+ ->get ('Magento\Eav\Api\AttributeManagementInterface ' );
275
+ }
276
+ return $ this ->attributeManagement ;
277
+ }
278
+
279
+ /**
280
+ * @return LoggerInterface
281
+ */
282
+ private function getLogger ()
283
+ {
284
+ if (null === $ this ->logger ) {
285
+ $ this ->logger = \Magento \Framework \App \ObjectManager::getInstance ()
286
+ ->get ('Psr\Log\LoggerInterface ' );
287
+ }
288
+ return $ this ->logger ;
289
+ }
290
+
291
+ /**
292
+ * @return ExtensionAttributesFactory
293
+ */
294
+ private function getExtensionAttributesFactory ()
295
+ {
296
+ if (null === $ this ->extensionAttributesFactory ) {
297
+ $ this ->extensionAttributesFactory = \Magento \Framework \App \ObjectManager::getInstance ()
298
+ ->get ('Magento\Framework\Api\ExtensionAttributesFactory ' );
299
+ }
300
+ return $ this ->extensionAttributesFactory ;
301
+ }
221
302
}
0 commit comments