33 * Copyright © Magento, Inc. All rights reserved.
44 * See COPYING.txt for license details.
55 */
6+ declare (strict_types=1 );
67
78namespace Magento \Setup \Fixtures ;
89
910use Magento \Catalog \Model \Product ;
1011use Magento \Catalog \Model \Product \Attribute \Source \Status ;
1112use Magento \Catalog \Model \ProductFactory ;
13+ use Magento \Eav \Model \Entity \Attribute ;
14+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Collection ;
1215use Magento \Eav \Model \ResourceModel \Entity \Attribute \CollectionFactory ;
16+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \Collection as AttributeSetCollection ;
1317use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \CollectionFactory as AttributeSetCollectionFactory ;
1418use Magento \Setup \Model \FixtureGenerator \ProductGenerator ;
1519use Magento \Setup \Model \SearchTermDescriptionGeneratorFactory ;
@@ -68,7 +72,7 @@ class SimpleProductsFixture extends Fixture
6872 private $ defaultAttributeSetId ;
6973
7074 /**
71- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\ Collection
75+ * @var Collection
7276 */
7377 private $ attributeCollectionFactory ;
7478
@@ -97,6 +101,11 @@ class SimpleProductsFixture extends Fixture
97101 */
98102 private $ priceProvider ;
99103
104+ /**
105+ * @var int[]
106+ */
107+ private $ additionalAttributeSetIds ;
108+
100109 /**
101110 * @param FixtureModel $fixtureModel
102111 * @param ProductFactory $productFactory
@@ -184,35 +193,38 @@ public function execute()
184193 'Short simple product Description %s '
185194 );
186195
187- $ additionalAttributeSets = $ this ->getAdditionalAttributeSets ();
188- $ attributeSet = function ($ index ) use ($ defaultAttributeSets , $ additionalAttributeSets ) {
196+ $ additionalAttributeSetIds = $ this ->getAdditionalAttributeSetIds ();
197+ $ attributeSet = function ($ index ) use ($ defaultAttributeSets , $ additionalAttributeSetIds ) {
189198 // phpcs:ignore
190199 mt_srand ($ index );
191200 $ attributeSetCount = count (array_keys ($ defaultAttributeSets ));
192201 if ($ attributeSetCount > (($ index - 1 ) % (int )$ this ->fixtureModel ->getValue ('categories ' , 30 ))) {
193- // phpcs:ignore Magento2.Functions.DiscouragedFunction
202+ // phpcs:ignore Magento2.Security.InsecureFunction
194203 return array_keys ($ defaultAttributeSets )[mt_rand (0 , count (array_keys ($ defaultAttributeSets )) - 1 )];
195204 } else {
196- $ customSetsAmount = count ($ additionalAttributeSets );
205+ $ customSetsAmount = count ($ additionalAttributeSetIds );
197206 return $ customSetsAmount
198- ? $ additionalAttributeSets [$ index % count ( $ additionalAttributeSets )][ ' attribute_set_id ' ]
207+ ? $ additionalAttributeSetIds [$ index % $ customSetsAmount ]
199208 : $ this ->getDefaultAttributeSetId ();
200209 }
201210 };
202211
212+ $ additionalAttributeValues = $ this ->getAdditionalAttributeValues ();
203213 $ additionalAttributes = function (
204214 $ attributeSetId ,
205215 $ index
206216 ) use (
207217 $ defaultAttributeSets ,
208- $ additionalAttributeSets
218+ $ additionalAttributeValues
209219 ) {
210220 $ attributeValues = [];
211221 // phpcs:ignore
212222 mt_srand ($ index );
213- if (isset ($ defaultAttributeSets [$ attributeSetId ])) {
214- foreach ($ defaultAttributeSets [$ attributeSetId ] as $ attributeCode => $ values ) {
215- // phpcs:ignore Magento2.Functions.DiscouragedFunction
223+ $ attributeValuesByAttributeSet = $ defaultAttributeSets [$ attributeSetId ]
224+ ?? $ additionalAttributeValues [$ attributeSetId ];
225+ if (!empty ($ attributeValuesByAttributeSet )) {
226+ foreach ($ attributeValuesByAttributeSet as $ attributeCode => $ values ) {
227+ // phpcs:ignore Magento2.Security.InsecureFunction
216228 $ attributeValues [$ attributeCode ] = $ values [mt_rand (0 , count ($ values ) - 1 )];
217229 }
218230 }
@@ -279,10 +291,10 @@ private function getDefaultAttributeSetId()
279291 }
280292
281293 /**
282- * Get default attribute sets with attributes
294+ * Get default attribute sets with attributes.
283295 *
284- * @see config/attributeSets.xml
285296 * @return array
297+ * @see config/attributeSets.xml
286298 */
287299 private function getDefaultAttributeSets ()
288300 {
@@ -301,17 +313,7 @@ private function getDefaultAttributeSets()
301313 'attribute_code ' ,
302314 array_column ($ attributesData , 'attribute_code ' )
303315 );
304- /** @var \Magento\Eav\Model\Entity\Attribute $attribute */
305- foreach ($ attributeCollection as $ attribute ) {
306- $ values = [];
307- $ options = $ attribute ->getOptions ();
308- foreach (($ options ?: []) as $ option ) {
309- if ($ option ->getValue ()) {
310- $ values [] = $ option ->getValue ();
311- }
312- }
313- $ attributes [$ attribute ->getAttributeSetId ()][$ attribute ->getAttributeCode ()] = $ values ;
314- }
316+ $ attributes = $ this ->processAttributeValues ($ attributeCollection , $ attributes );
315317 }
316318 }
317319 $ attributes [$ this ->getDefaultAttributeSetId ()] = [];
@@ -381,16 +383,64 @@ private function readDescriptionConfig($configSrc)
381383 }
382384
383385 /**
384- * Get additional attribute sets
386+ * Get additional attribute set ids.
387+ *
388+ * @return int[]
389+ */
390+ private function getAdditionalAttributeSetIds ()
391+ {
392+ if (null === $ this ->additionalAttributeSetIds ) {
393+ /** @var AttributeSetCollection $sets */
394+ $ sets = $ this ->attributeSetCollectionFactory ->create ();
395+ $ sets ->addFieldToFilter (
396+ 'attribute_set_name ' ,
397+ ['like ' => AttributeSetsFixture::PRODUCT_SET_NAME . '% ' ]
398+ );
399+ $ this ->additionalAttributeSetIds = $ sets ->getAllIds ();
400+ }
401+
402+ return $ this ->additionalAttributeSetIds ;
403+ }
404+
405+ /**
406+ * Get values of additional attributes.
385407 *
386- * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection[]
408+ * @return array
387409 */
388- private function getAdditionalAttributeSets ()
410+ private function getAdditionalAttributeValues (): array
389411 {
390- /** @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection $sets */
391- $ sets = $ this ->attributeSetCollectionFactory ->create ();
392- $ sets ->addFieldToFilter ('attribute_set_name ' , ['like ' => AttributeSetsFixture::PRODUCT_SET_NAME . '% ' ]);
412+ $ attributeCollection = $ this ->attributeCollectionFactory ->create ();
413+ $ attributeCollection ->setAttributeSetsFilter ($ this ->getAdditionalAttributeSetIds ())
414+ ->addFieldToFilter ('attribute_code ' , ['like ' => 'attribute_set% ' ]);
415+ $ attributeCollection ->getSelect ()->columns (['entity_attribute.attribute_set_id ' ]);
416+
417+ return $ this ->processAttributeValues ($ attributeCollection );
418+ }
393419
394- return $ sets ->getData ();
420+ /**
421+ * Maps attribute values by attribute set and attribute code.
422+ *
423+ * @param Collection $attributeCollection
424+ * @param array $attributes
425+ * @return array
426+ */
427+ private function processAttributeValues (
428+ Collection $ attributeCollection ,
429+ array $ attributes = []
430+ ): array {
431+ /** @var Attribute $attribute */
432+ foreach ($ attributeCollection as $ attribute ) {
433+ $ values = [];
434+ $ options = $ attribute ->getOptions () ?? [];
435+ $ attributeSetId = $ attribute ->getAttributeSetId () ?? $ this ->getDefaultAttributeSetId ();
436+ foreach ($ options as $ option ) {
437+ if ($ option ->getValue ()) {
438+ $ values [] = $ option ->getValue ();
439+ }
440+ }
441+ $ attributes [$ attributeSetId ][$ attribute ->getAttributeCode ()] = $ values ;
442+ }
443+
444+ return $ attributes ;
395445 }
396446}
0 commit comments