1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2011 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
namespace Magento \CatalogImportExport \Model \Import \Product \Type ;
7
7
8
- use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
8
+ use Magento \Catalog \Model \ResourceModel \Product \Attribute \ CollectionFactory as ProductAttributeCollectionFactory ;
9
9
use Magento \CatalogImportExport \Model \Import \Product ;
10
10
use Magento \CatalogImportExport \Model \Import \Product \RowValidatorInterface ;
11
11
use Magento \Eav \Model \Entity \Attribute \Source \Table ;
12
12
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Option \CollectionFactory as AttributeOptionCollectionFactory ;
13
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \CollectionFactory as AttributeSetCollectionFactory ;
13
14
use Magento \Framework \App \ObjectManager ;
14
15
use Magento \Framework \App \ResourceConnection ;
16
+ use Magento \Framework \DB \Adapter \AdapterInterface ;
15
17
use Magento \Framework \EntityManager \MetadataPool ;
18
+ use Magento \ImportExport \Model \Import ;
19
+ use Magento \Store \Model \Store ;
16
20
17
21
/**
18
22
* Import entity abstract product type model
@@ -124,29 +128,29 @@ abstract class AbstractType
124
128
protected $ _type ;
125
129
126
130
/**
127
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory
131
+ * @var AttributeSetCollectionFactory
128
132
*/
129
133
protected $ _attrSetColFac ;
130
134
131
135
/**
132
- * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
136
+ * @var ProductAttributeCollectionFactory
133
137
*/
134
138
protected $ _prodAttrColFac ;
135
139
136
140
/**
137
- * @var \Magento\Framework\App\ ResourceConnection
141
+ * @var ResourceConnection
138
142
*/
139
143
protected $ _resource ;
140
144
141
145
/**
142
- * @var \Magento\Framework\DB\Adapter\ AdapterInterface
146
+ * @var AdapterInterface
143
147
*/
144
148
protected $ connection ;
145
149
146
150
/**
147
151
* Product metadata pool
148
152
*
149
- * @var \Magento\Framework\EntityManager\ MetadataPool
153
+ * @var MetadataPool
150
154
* @since 100.1.0
151
155
*/
152
156
protected $ metadataPool ;
@@ -164,18 +168,18 @@ abstract class AbstractType
164
168
/**
165
169
* AbstractType constructor
166
170
*
167
- * @param \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory $attrSetColFac
168
- * @param \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $prodAttrColFac
171
+ * @param AttributeSetCollectionFactory $attrSetColFac
172
+ * @param ProductAttributeCollectionFactory $prodAttrColFac
169
173
* @param ResourceConnection $resource
170
174
* @param array $params
171
175
* @param MetadataPool|null $metadataPool
172
176
* @param AttributeOptionCollectionFactory|null $attributeOptionCollectionFactory
173
177
* @throws \Magento\Framework\Exception\LocalizedException
174
178
*/
175
179
public function __construct (
176
- \ Magento \ Eav \ Model \ ResourceModel \ Entity \ Attribute \ Set \ CollectionFactory $ attrSetColFac ,
177
- \ Magento \ Catalog \ Model \ ResourceModel \ Product \ Attribute \ CollectionFactory $ prodAttrColFac ,
178
- \ Magento \ Framework \ App \ ResourceConnection $ resource ,
180
+ AttributeSetCollectionFactory $ attrSetColFac ,
181
+ ProductAttributeCollectionFactory $ prodAttrColFac ,
182
+ ResourceConnection $ resource ,
179
183
array $ params ,
180
184
?MetadataPool $ metadataPool = null ,
181
185
?AttributeOptionCollectionFactory $ attributeOptionCollectionFactory = null
@@ -269,10 +273,6 @@ protected function _getProductAttributes($attrSetData)
269
273
/**
270
274
* Initialize attributes parameters for all attributes' sets.
271
275
*
272
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
273
- * @SuppressWarnings(PHPMD.NPathComplexity)
274
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
275
- *
276
276
* @return $this
277
277
*/
278
278
protected function _initAttributes ()
@@ -288,50 +288,18 @@ protected function _initAttributes()
288
288
'set.attribute_set_id = attr.attribute_set_id ' ,
289
289
['set.attribute_set_name ' ]
290
290
)->where (
291
- $ this ->connection ->quoteInto ('attr.entity_type_id IN (?) ' , $ entityId )
291
+ $ this ->connection ->quoteInto ('attr.entity_type_id = ? ' , $ entityId )
292
292
)
293
293
);
294
- $ absentKeys = [];
295
- foreach ($ entityAttributes as $ attributeRow ) {
296
- if (!isset (self ::$ commonAttributesCache [$ attributeRow ['attribute_id ' ]])) {
297
- if (!isset ($ absentKeys [$ attributeRow ['attribute_set_name ' ]])) {
298
- $ absentKeys [$ attributeRow ['attribute_set_name ' ]] = [];
299
- }
300
- $ absentKeys [$ attributeRow ['attribute_set_name ' ]][] = $ attributeRow ['attribute_id ' ];
301
- }
302
- }
303
- $ addedAttributes = [];
304
- foreach ($ absentKeys as $ attributeIds ) {
305
- $ unknownAttributeIds = array_diff (
306
- $ attributeIds ,
307
- array_keys (self ::$ commonAttributesCache ),
308
- self ::$ invAttributesCache
309
- );
310
- if ($ unknownAttributeIds ) {
311
- $ addedAttributes [] = $ this ->attachAttributesByOnlyId ($ unknownAttributeIds );
312
- }
313
- }
314
- if ($ this ->_forcedAttributesCodes ) {
315
- $ addedAttributes [] = $ this ->attachAttributesByForcedCodes ();
316
- }
317
- $ addedAttributes = array_merge (...$ addedAttributes );
318
- $ attributesToLoadFromTable = [];
319
- foreach ($ addedAttributes as $ addedAttribute ) {
320
- if (isset ($ addedAttribute ['options_use_table ' ])) {
321
- $ attributesToLoadFromTable [] = $ addedAttribute ['id ' ];
322
- unset(self ::$ commonAttributesCache [$ addedAttribute ['id ' ]]['options_use_table ' ]);
323
- }
324
- }
325
- foreach (array_chunk ($ attributesToLoadFromTable , 1000 ) as $ attributesToLoadFromTableChunk ) {
326
- $ collection = $ this ->attributeOptionCollectionFactory ->create ();
327
- $ collection ->setAttributeFilter (['in ' => $ attributesToLoadFromTableChunk ]);
328
- $ collection ->setStoreFilter (\Magento \Store \Model \Store::DEFAULT_STORE_ID );
329
- foreach ($ collection as $ option ) {
330
- $ attributeId = $ option ->getAttributeId ();
331
- $ value = strtolower ($ option ->getValue ());
332
- self ::$ commonAttributesCache [$ attributeId ]['options ' ][$ value ] = $ option ->getOptionId ();
333
- }
334
- }
294
+
295
+ $ attributeIds = array_unique (array_column ($ entityAttributes , 'attribute_id ' ));
296
+ $ unknownAttributeIds = array_diff (
297
+ $ attributeIds ,
298
+ array_keys (self ::$ commonAttributesCache ),
299
+ self ::$ invAttributesCache
300
+ );
301
+ $ this ->attachAttributes ($ unknownAttributeIds );
302
+
335
303
foreach ($ entityAttributes as $ attributeRow ) {
336
304
if (isset (self ::$ commonAttributesCache [$ attributeRow ['attribute_id ' ]])) {
337
305
$ attribute = self ::$ commonAttributesCache [$ attributeRow ['attribute_id ' ]];
@@ -354,6 +322,7 @@ protected function _initAttributes()
354
322
$ this ->_addAttributeParams ($ setName , $ attribute , $ attribute );
355
323
}
356
324
}
325
+
357
326
return $ this ;
358
327
}
359
328
@@ -364,73 +333,36 @@ protected function _initAttributes()
364
333
* @param array $attributeIds
365
334
* @return void
366
335
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
367
- * @deprecated use attachAttributesByOnlyId and attachAttributesByForcedCodes
368
- * @see attachAttributesByOnlyId() and attachAttributesByForcedCodes()
336
+ * @deprecated
337
+ * @see self::attachAttributes
369
338
*/
370
339
protected function attachAttributesById ($ attributeSetName , $ attributeIds )
371
340
{
372
- foreach ($ this ->_prodAttrColFac ->create ()->addFieldToFilter (
373
- ['main_table.attribute_id ' , 'main_table.attribute_code ' ],
374
- [['in ' => $ attributeIds ], ['in ' => $ this ->_forcedAttributesCodes ]]
375
- ) as $ attribute ) {
376
- $ this ->attachAttribute ($ attribute );
377
- }
378
- }
379
-
380
- /**
381
- * Attach Attributes By Id
382
- *
383
- * @param array $attributeIds
384
- * @return array
385
- */
386
- private function attachAttributesByOnlyId (array $ attributeIds ) : array
387
- {
388
- $ addedAttributes = [];
389
- foreach ($ this ->_prodAttrColFac ->create ()->addFieldToFilter (
390
- ['main_table.attribute_id ' ],
391
- [['in ' => $ attributeIds ]]
392
- ) as $ attribute ) {
393
- $ cachedAttribute = $ this ->attachAttribute ($ attribute );
394
- if (null !== $ cachedAttribute ) {
395
- $ addedAttributes [] = $ cachedAttribute ;
396
- }
397
- }
398
- return $ addedAttributes ;
341
+ $ this ->attachAttributes ($ attributeIds );
399
342
}
400
343
401
344
/**
402
- * Attach Attributes By _forcedAttributesCodes
345
+ * Attach attributes to self::$commonAttributesCache or self::$invAttributesCache
403
346
*
404
- * @return array
347
+ * @param int[] $attributeIds
348
+ * @return void
405
349
*/
406
- private function attachAttributesByForcedCodes () : array
350
+ private function attachAttributes ( array $ attributeIds ): void
407
351
{
408
- $ addedAttributes = [];
409
- foreach ($ this ->_prodAttrColFac ->create ()->addFieldToFilter (
410
- ['main_table.attribute_code ' ],
411
- [['in ' => $ this ->_forcedAttributesCodes ]]
412
- ) as $ attribute ) {
413
- $ cachedAttribute = $ this ->attachAttribute ($ attribute );
414
- if (null !== $ cachedAttribute ) {
415
- $ addedAttributes [] = $ cachedAttribute ;
416
- }
352
+ $ attributes = $ attributeIds ? $ this ->_prodAttrColFac ->create ()
353
+ ->addFieldToFilter (['main_table.attribute_id ' ], [['in ' => $ attributeIds ]])
354
+ ->getItems () : [];
355
+ if ($ this ->_forcedAttributesCodes ) {
356
+ $ attributes += $ this ->_prodAttrColFac ->create ()
357
+ ->addFieldToFilter (['main_table.attribute_code ' ], [['in ' => $ this ->_forcedAttributesCodes ]])
358
+ ->getItems ();
417
359
}
418
- return $ addedAttributes ;
419
- }
420
360
421
- /**
422
- * Attach Attribute to self::$commonAttributesCache or self::$invAttributesCache
423
- *
424
- * @param Attribute $attribute
425
- * @return array|null
426
- */
427
- private function attachAttribute (Attribute $ attribute )
428
- {
429
- $ cachedAttribute = null ;
430
- $ attributeCode = $ attribute ->getAttributeCode ();
431
- $ attributeId = $ attribute ->getId ();
432
- if ($ attribute ->getIsVisible () || in_array ($ attributeCode , $ this ->_forcedAttributesCodes )) {
433
- if (!isset (self ::$ commonAttributesCache [$ attributeId ])) {
361
+ $ commonAttributes = $ attributeCodeToId = $ attributesToLoadFromTable = $ invAttributes = [];
362
+ foreach ($ attributes as $ attribute ) {
363
+ $ attributeCode = $ attribute ->getAttributeCode ();
364
+ $ attributeId = $ attribute ->getId ();
365
+ if ($ attribute ->getIsVisible () || in_array ($ attributeCode , $ this ->_forcedAttributesCodes )) {
434
366
$ defaultValue = $ attribute ->getDefaultValue ();
435
367
$ cachedAttribute = [
436
368
'id ' => $ attributeId ,
@@ -441,27 +373,41 @@ private function attachAttribute(Attribute $attribute)
441
373
'frontend_label ' => $ attribute ->getFrontendLabel (),
442
374
'is_static ' => $ attribute ->isStatic (),
443
375
'apply_to ' => $ attribute ->getApplyTo (),
444
- 'type ' => \Magento \ImportExport \Model \Import::getAttributeType ($ attribute ),
445
- 'default_value ' => (is_string ($ defaultValue ) && strlen ($ defaultValue )) ?
446
- $ attribute ->getDefaultValue () : null ,
376
+ 'type ' => Import::getAttributeType ($ attribute ),
377
+ 'default_value ' => is_string ($ defaultValue ) && strlen ($ defaultValue ) ? $ defaultValue : null ,
447
378
'options ' => [],
448
379
];
449
- $ sourceModel = $ attribute ->getSourceModel ();
450
- if (Table::class === $ sourceModel ) {
451
- $ cachedAttribute ['options_use_table ' ] = true ;
380
+ if (Table::class === $ attribute ->getSourceModel ()) {
381
+ $ attributesToLoadFromTable [] = $ attributeId ;
452
382
} else {
453
383
$ cachedAttribute ['options ' ] = $ this ->_entityModel ->getAttributeOptions (
454
384
$ attribute ,
455
385
$ this ->_indexValueAttributes
456
386
);
457
387
}
458
- self ::$ commonAttributesCache [$ attributeId ] = $ cachedAttribute ;
459
- self ::$ attributeCodeToId [$ attributeCode ] = $ attributeId ;
388
+
389
+ $ commonAttributes [$ attributeId ] = $ cachedAttribute ;
390
+ $ attributeCodeToId [$ attributeCode ] = $ attributeId ;
391
+ } else {
392
+ $ invAttributes [] = $ attributeId ;
460
393
}
461
- } else {
462
- self ::$ invAttributesCache [] = $ attributeId ;
463
394
}
464
- return $ cachedAttribute ;
395
+
396
+ foreach (array_chunk ($ attributesToLoadFromTable , 1000 ) as $ ids ) {
397
+ $ collection = $ this ->attributeOptionCollectionFactory ->create ();
398
+ $ collection ->setAttributeFilter (['in ' => $ ids ]);
399
+ $ collection ->setStoreFilter (Store::DEFAULT_STORE_ID );
400
+ $ options = $ collection ->getItems ();
401
+ foreach ($ options as $ option ) {
402
+ $ attributeId = $ option ->getAttributeId ();
403
+ $ value = strtolower ($ option ->getValue ());
404
+ $ commonAttributes [$ attributeId ]['options ' ][$ value ] = $ option ->getOptionId ();
405
+ }
406
+ }
407
+
408
+ self ::$ commonAttributesCache += $ commonAttributes ;
409
+ self ::$ attributeCodeToId += $ attributeCodeToId ;
410
+ self ::$ invAttributesCache = array_merge (self ::$ invAttributesCache , $ invAttributes );
465
411
}
466
412
467
413
/**
@@ -684,15 +630,15 @@ public function saveData()
684
630
/**
685
631
* Get product metadata pool
686
632
*
687
- * @return \Magento\Framework\EntityManager\ MetadataPool
633
+ * @return MetadataPool
688
634
* @since 100.1.0
689
635
*/
690
636
protected function getMetadataPool ()
691
637
{
692
638
if (!$ this ->metadataPool ) {
693
639
// phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
694
640
$ this ->metadataPool = \Magento \Framework \App \ObjectManager::getInstance ()
695
- ->get (\ Magento \ Framework \ EntityManager \ MetadataPool::class);
641
+ ->get (MetadataPool::class);
696
642
}
697
643
return $ this ->metadataPool ;
698
644
}
0 commit comments