11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2011 Adobe
4+ * All Rights Reserved .
55 */
66namespace Magento \CatalogImportExport \Model \Import \Product \Type ;
77
8- use Magento \Catalog \Model \ResourceModel \Eav \Attribute ;
8+ use Magento \Catalog \Model \ResourceModel \Product \Attribute \ CollectionFactory as ProductAttributeCollectionFactory ;
99use Magento \CatalogImportExport \Model \Import \Product ;
1010use Magento \CatalogImportExport \Model \Import \Product \RowValidatorInterface ;
1111use Magento \Eav \Model \Entity \Attribute \Source \Table ;
1212use Magento \Eav \Model \ResourceModel \Entity \Attribute \Option \CollectionFactory as AttributeOptionCollectionFactory ;
13+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \CollectionFactory as AttributeSetCollectionFactory ;
1314use Magento \Framework \App \ObjectManager ;
1415use Magento \Framework \App \ResourceConnection ;
16+ use Magento \Framework \DB \Adapter \AdapterInterface ;
1517use Magento \Framework \EntityManager \MetadataPool ;
18+ use Magento \ImportExport \Model \Import ;
19+ use Magento \Store \Model \Store ;
1620
1721/**
1822 * Import entity abstract product type model
@@ -124,29 +128,29 @@ abstract class AbstractType
124128 protected $ _type ;
125129
126130 /**
127- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\CollectionFactory
131+ * @var AttributeSetCollectionFactory
128132 */
129133 protected $ _attrSetColFac ;
130134
131135 /**
132- * @var \Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection
136+ * @var ProductAttributeCollectionFactory
133137 */
134138 protected $ _prodAttrColFac ;
135139
136140 /**
137- * @var \Magento\Framework\App\ ResourceConnection
141+ * @var ResourceConnection
138142 */
139143 protected $ _resource ;
140144
141145 /**
142- * @var \Magento\Framework\DB\Adapter\ AdapterInterface
146+ * @var AdapterInterface
143147 */
144148 protected $ connection ;
145149
146150 /**
147151 * Product metadata pool
148152 *
149- * @var \Magento\Framework\EntityManager\ MetadataPool
153+ * @var MetadataPool
150154 * @since 100.1.0
151155 */
152156 protected $ metadataPool ;
@@ -164,18 +168,18 @@ abstract class AbstractType
164168 /**
165169 * AbstractType constructor
166170 *
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
169173 * @param ResourceConnection $resource
170174 * @param array $params
171175 * @param MetadataPool|null $metadataPool
172176 * @param AttributeOptionCollectionFactory|null $attributeOptionCollectionFactory
173177 * @throws \Magento\Framework\Exception\LocalizedException
174178 */
175179 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 ,
179183 array $ params ,
180184 ?MetadataPool $ metadataPool = null ,
181185 ?AttributeOptionCollectionFactory $ attributeOptionCollectionFactory = null
@@ -269,10 +273,6 @@ protected function _getProductAttributes($attrSetData)
269273 /**
270274 * Initialize attributes parameters for all attributes' sets.
271275 *
272- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
273- * @SuppressWarnings(PHPMD.NPathComplexity)
274- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
275- *
276276 * @return $this
277277 */
278278 protected function _initAttributes ()
@@ -288,50 +288,18 @@ protected function _initAttributes()
288288 'set.attribute_set_id = attr.attribute_set_id ' ,
289289 ['set.attribute_set_name ' ]
290290 )->where (
291- $ this ->connection ->quoteInto ('attr.entity_type_id IN (?) ' , $ entityId )
291+ $ this ->connection ->quoteInto ('attr.entity_type_id = ? ' , $ entityId )
292292 )
293293 );
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+
335303 foreach ($ entityAttributes as $ attributeRow ) {
336304 if (isset (self ::$ commonAttributesCache [$ attributeRow ['attribute_id ' ]])) {
337305 $ attribute = self ::$ commonAttributesCache [$ attributeRow ['attribute_id ' ]];
@@ -354,6 +322,7 @@ protected function _initAttributes()
354322 $ this ->_addAttributeParams ($ setName , $ attribute , $ attribute );
355323 }
356324 }
325+
357326 return $ this ;
358327 }
359328
@@ -364,73 +333,36 @@ protected function _initAttributes()
364333 * @param array $attributeIds
365334 * @return void
366335 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
367- * @deprecated use attachAttributesByOnlyId and attachAttributesByForcedCodes
368- * @see attachAttributesByOnlyId() and attachAttributesByForcedCodes()
336+ * @deprecated
337+ * @see self::attachAttributes
369338 */
370339 protected function attachAttributesById ($ attributeSetName , $ attributeIds )
371340 {
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 );
399342 }
400343
401344 /**
402- * Attach Attributes By _forcedAttributesCodes
345+ * Attach attributes to self::$commonAttributesCache or self::$invAttributesCache
403346 *
404- * @return array
347+ * @param int[] $attributeIds
348+ * @return void
405349 */
406- private function attachAttributesByForcedCodes () : array
350+ private function attachAttributes ( array $ attributeIds ): void
407351 {
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 ();
417359 }
418- return $ addedAttributes ;
419- }
420360
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 )) {
434366 $ defaultValue = $ attribute ->getDefaultValue ();
435367 $ cachedAttribute = [
436368 'id ' => $ attributeId ,
@@ -441,27 +373,41 @@ private function attachAttribute(Attribute $attribute)
441373 'frontend_label ' => $ attribute ->getFrontendLabel (),
442374 'is_static ' => $ attribute ->isStatic (),
443375 '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 ,
447378 'options ' => [],
448379 ];
449- $ sourceModel = $ attribute ->getSourceModel ();
450- if (Table::class === $ sourceModel ) {
451- $ cachedAttribute ['options_use_table ' ] = true ;
380+ if (Table::class === $ attribute ->getSourceModel ()) {
381+ $ attributesToLoadFromTable [] = $ attributeId ;
452382 } else {
453383 $ cachedAttribute ['options ' ] = $ this ->_entityModel ->getAttributeOptions (
454384 $ attribute ,
455385 $ this ->_indexValueAttributes
456386 );
457387 }
458- self ::$ commonAttributesCache [$ attributeId ] = $ cachedAttribute ;
459- self ::$ attributeCodeToId [$ attributeCode ] = $ attributeId ;
388+
389+ $ commonAttributes [$ attributeId ] = $ cachedAttribute ;
390+ $ attributeCodeToId [$ attributeCode ] = $ attributeId ;
391+ } else {
392+ $ invAttributes [] = $ attributeId ;
460393 }
461- } else {
462- self ::$ invAttributesCache [] = $ attributeId ;
463394 }
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 );
465411 }
466412
467413 /**
@@ -684,15 +630,15 @@ public function saveData()
684630 /**
685631 * Get product metadata pool
686632 *
687- * @return \Magento\Framework\EntityManager\ MetadataPool
633+ * @return MetadataPool
688634 * @since 100.1.0
689635 */
690636 protected function getMetadataPool ()
691637 {
692638 if (!$ this ->metadataPool ) {
693639 // phpcs:ignore Magento2.PHP.AutogeneratedClassNotInConstructor
694640 $ this ->metadataPool = \Magento \Framework \App \ObjectManager::getInstance ()
695- ->get (\ Magento \ Framework \ EntityManager \ MetadataPool::class);
641+ ->get (MetadataPool::class);
696642 }
697643 return $ this ->metadataPool ;
698644 }
0 commit comments