@@ -16,12 +16,27 @@ class AttributeSetsFixture extends Fixture
16
16
*/
17
17
protected $ priority = 25 ;
18
18
19
+ /**
20
+ * Cache for attribute IDs.
21
+ *
22
+ * @var array
23
+ */
24
+ private $ attributeIdsCache = [];
25
+
26
+ /**
27
+ * Quantity of unique attributes to generate. Zero means infinity.
28
+ *
29
+ * @var int
30
+ */
31
+ protected $ uniqueAttributesQuantity = 0 ;
32
+
19
33
/**
20
34
* {@inheritdoc}
21
35
*/
22
36
public function execute ()
23
37
{
24
- $ attributeSets = $ this ->fixtureModel ->getValue ('attribute_sets ' , null );
38
+ $ this ->populateUniqueAttributesQuantity ();
39
+ $ attributeSets = $ this ->getAttributeSetsFixtureValue ();
25
40
if ($ attributeSets === null ) {
26
41
return ;
27
42
}
@@ -71,37 +86,81 @@ public function execute()
71
86
$ attributesData = array_key_exists (0 , $ attributeSetData ['attributes ' ]['attribute ' ])
72
87
? $ attributeSetData ['attributes ' ]['attribute ' ] : [$ attributeSetData ['attributes ' ]['attribute ' ]];
73
88
foreach ($ attributesData as $ attributeData ) {
74
- //Create Attribute
75
- /** @var \Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory $attributeFactory */
76
- $ attributeFactory = $ this ->fixtureModel ->getObjectManager ()->create (
77
- \Magento \Catalog \Api \Data \ProductAttributeInterfaceFactory::class
78
- );
79
-
80
- $ optionsData = array_key_exists (0 , $ attributeData ['options ' ]['option ' ])
81
- ? $ attributeData ['options ' ]['option ' ] : [$ attributeData ['options ' ]['option ' ]];
82
- $ options = [];
83
- foreach ($ optionsData as $ optionData ) {
84
- /** @var \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory */
85
- $ optionFactory = $ this ->fixtureModel ->getObjectManager ()->create (
86
- \Magento \Eav \Api \Data \AttributeOptionInterfaceFactory::class
89
+ if ($ this ->uniqueAttributesQuantity === 0
90
+ || (count ($ this ->attributeIdsCache ) < $ this ->uniqueAttributesQuantity )) {
91
+ //Create Attribute
92
+ /** @var \Magento\Catalog\Api\Data\ProductAttributeInterfaceFactory $attributeFactory */
93
+ $ attributeFactory = $ this ->fixtureModel ->getObjectManager ()->create (
94
+ \Magento \Catalog \Api \Data \ProductAttributeInterfaceFactory::class
87
95
);
88
- $ option = $ optionFactory ->create (['data ' => $ optionData ]);
89
- $ options [] = $ option ;
90
- }
91
96
92
- $ attribute = $ attributeFactory ->create (['data ' => $ attributeData ]);
93
- $ attribute ->setOptions ($ options );
97
+ $ optionsData = array_key_exists (0 , $ attributeData ['options ' ]['option ' ])
98
+ ? $ attributeData ['options ' ]['option ' ] : [$ attributeData ['options ' ]['option ' ]];
99
+ $ options = [];
100
+ foreach ($ optionsData as $ optionData ) {
101
+ /** @var \Magento\Eav\Api\Data\AttributeOptionInterfaceFactory $optionFactory */
102
+ $ optionFactory = $ this ->fixtureModel ->getObjectManager ()->create (
103
+ \Magento \Eav \Api \Data \AttributeOptionInterfaceFactory::class
104
+ );
105
+ $ option = $ optionFactory ->create (['data ' => $ optionData ]);
106
+ $ options [] = $ option ;
107
+ }
94
108
95
- $ result = $ attributeRepository -> save ( $ attribute );
96
- $ attributeId = $ result -> getAttributeId ( );
109
+ $ attribute = $ attributeFactory -> create ([ ' data ' => $ attributeData ] );
110
+ $ attribute -> setOptions ( $ options );
97
111
112
+ $ result = $ attributeRepository ->save ($ attribute );
113
+ $ attributeId = $ result ->getAttributeId ();
114
+ $ this ->fillAttributeIdsCache ($ attributeId );
115
+ } else {
116
+ $ attributeId = $ this ->getAttributeIdFromCache ();
117
+ }
98
118
//Associate Attribute to Attribute Set
99
119
$ sortOrder = 3 ;
120
+
100
121
$ attributeManagement ->assign ($ attributeSetId , $ attributeGroupId , $ attributeId , $ sortOrder );
101
122
}
102
123
}
103
124
}
104
125
126
+ /**
127
+ * Get attribute ID from cache.
128
+ *
129
+ * @return int
130
+ */
131
+ private function getAttributeIdFromCache ()
132
+ {
133
+ $ attributeId = next ($ this ->attributeIdsCache );
134
+ if ($ attributeId === false ) {
135
+ $ attributeId = reset ($ this ->attributeIdsCache );
136
+ }
137
+
138
+ return $ attributeId ;
139
+ }
140
+
141
+ /**
142
+ * Fill attribute IDs cache.
143
+ *
144
+ * @param int $attributeId
145
+ * @return void
146
+ */
147
+ private function fillAttributeIdsCache ($ attributeId )
148
+ {
149
+ if ($ this ->uniqueAttributesQuantity !== 0 ) {
150
+ $ this ->attributeIdsCache [] = $ attributeId ;
151
+ }
152
+ }
153
+
154
+ /**
155
+ * Populate quantity of unique attributes to generate.
156
+ *
157
+ * @return void
158
+ */
159
+ protected function populateUniqueAttributesQuantity ()
160
+ {
161
+ $ this ->uniqueAttributesQuantity = $ this ->fixtureModel ->getValue ('unique_attributes_quantity ' , 0 );
162
+ }
163
+
105
164
/**
106
165
* {@inheritdoc}
107
166
*/
@@ -119,4 +178,14 @@ public function introduceParamLabels()
119
178
'attribute_sets ' => 'Attribute Sets '
120
179
];
121
180
}
181
+
182
+ /**
183
+ * Get attribute sets fixture value.
184
+ *
185
+ * @return array|null
186
+ */
187
+ protected function getAttributeSetsFixtureValue ()
188
+ {
189
+ return $ this ->fixtureModel ->getValue ('attribute_sets ' , null );
190
+ }
122
191
}
0 commit comments