3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Setup \Fixtures ;
8
9
9
10
use Magento \Catalog \Model \Product ;
10
11
use Magento \Catalog \Model \Product \Attribute \Source \Status ;
11
12
use Magento \Catalog \Model \ProductFactory ;
13
+ use Magento \Eav \Model \Entity \Attribute ;
14
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Collection ;
12
15
use Magento \Eav \Model \ResourceModel \Entity \Attribute \CollectionFactory ;
16
+ use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \Collection as AttributeSetCollection ;
13
17
use Magento \Eav \Model \ResourceModel \Entity \Attribute \Set \CollectionFactory as AttributeSetCollectionFactory ;
14
18
use Magento \Setup \Model \FixtureGenerator \ProductGenerator ;
15
19
use Magento \Setup \Model \SearchTermDescriptionGeneratorFactory ;
@@ -68,7 +72,7 @@ class SimpleProductsFixture extends Fixture
68
72
private $ defaultAttributeSetId ;
69
73
70
74
/**
71
- * @var \Magento\Eav\Model\ResourceModel\Entity\Attribute\ Collection
75
+ * @var Collection
72
76
*/
73
77
private $ attributeCollectionFactory ;
74
78
@@ -97,6 +101,11 @@ class SimpleProductsFixture extends Fixture
97
101
*/
98
102
private $ priceProvider ;
99
103
104
+ /**
105
+ * @var int[]
106
+ */
107
+ private $ additionalAttributeSetIds ;
108
+
100
109
/**
101
110
* @param FixtureModel $fixtureModel
102
111
* @param ProductFactory $productFactory
@@ -184,35 +193,38 @@ public function execute()
184
193
'Short simple product Description %s '
185
194
);
186
195
187
- $ additionalAttributeSets = $ this ->getAdditionalAttributeSets ();
188
- $ attributeSet = function ($ index ) use ($ defaultAttributeSets , $ additionalAttributeSets ) {
196
+ $ additionalAttributeSetIds = $ this ->getAdditionalAttributeSetIds ();
197
+ $ attributeSet = function ($ index ) use ($ defaultAttributeSets , $ additionalAttributeSetIds ) {
189
198
// phpcs:ignore
190
199
mt_srand ($ index );
191
200
$ attributeSetCount = count (array_keys ($ defaultAttributeSets ));
192
201
if ($ attributeSetCount > (($ index - 1 ) % (int )$ this ->fixtureModel ->getValue ('categories ' , 30 ))) {
193
- // phpcs:ignore Magento2.Functions.DiscouragedFunction
202
+ // phpcs:ignore Magento2.Security.InsecureFunction
194
203
return array_keys ($ defaultAttributeSets )[mt_rand (0 , count (array_keys ($ defaultAttributeSets )) - 1 )];
195
204
} else {
196
- $ customSetsAmount = count ($ additionalAttributeSets );
205
+ $ customSetsAmount = count ($ additionalAttributeSetIds );
197
206
return $ customSetsAmount
198
- ? $ additionalAttributeSets [$ index % count ( $ additionalAttributeSets )][ ' attribute_set_id ' ]
207
+ ? $ additionalAttributeSetIds [$ index % $ customSetsAmount ]
199
208
: $ this ->getDefaultAttributeSetId ();
200
209
}
201
210
};
202
211
212
+ $ additionalAttributeValues = $ this ->getAdditionalAttributeValues ();
203
213
$ additionalAttributes = function (
204
214
$ attributeSetId ,
205
215
$ index
206
216
) use (
207
217
$ defaultAttributeSets ,
208
- $ additionalAttributeSets
218
+ $ additionalAttributeValues
209
219
) {
210
220
$ attributeValues = [];
211
221
// phpcs:ignore
212
222
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
216
228
$ attributeValues [$ attributeCode ] = $ values [mt_rand (0 , count ($ values ) - 1 )];
217
229
}
218
230
}
@@ -279,10 +291,10 @@ private function getDefaultAttributeSetId()
279
291
}
280
292
281
293
/**
282
- * Get default attribute sets with attributes
294
+ * Get default attribute sets with attributes.
283
295
*
284
- * @see config/attributeSets.xml
285
296
* @return array
297
+ * @see config/attributeSets.xml
286
298
*/
287
299
private function getDefaultAttributeSets ()
288
300
{
@@ -301,17 +313,7 @@ private function getDefaultAttributeSets()
301
313
'attribute_code ' ,
302
314
array_column ($ attributesData , 'attribute_code ' )
303
315
);
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 );
315
317
}
316
318
}
317
319
$ attributes [$ this ->getDefaultAttributeSetId ()] = [];
@@ -381,16 +383,64 @@ private function readDescriptionConfig($configSrc)
381
383
}
382
384
383
385
/**
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.
385
407
*
386
- * @return \Magento\Eav\Model\ResourceModel\Entity\Attribute\Set\Collection[]
408
+ * @return array
387
409
*/
388
- private function getAdditionalAttributeSets ()
410
+ private function getAdditionalAttributeValues (): array
389
411
{
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
+ }
393
419
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 ;
395
445
}
396
446
}
0 commit comments