3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
-
7
6
namespace Magento \Setup \Model \FixtureGenerator ;
8
7
8
+ use Magento \Framework \App \ObjectManager ;
9
+ use Magento \Framework \App \ResourceConnection ;
10
+
9
11
/**
10
12
* Generate specified amount of bundle products based on passed fixture
11
13
*
21
23
*/
22
24
class BundleProductGenerator
23
25
{
26
+ /**
27
+ * @var array
28
+ */
29
+ private $ sequenceValues = [
30
+ 'sequence_product_bundle_option ' => null ,
31
+ 'sequence_product_bundle_selection ' => null
32
+ ];
33
+
24
34
/**
25
35
* @var ProductGeneratorFactory
26
36
*/
27
37
private $ productGeneratorFactory ;
28
38
39
+ /**
40
+ * @var ResourceConnection
41
+ */
42
+ private $ resource ;
43
+
29
44
/**
30
45
* @param ProductGeneratorFactory $productGeneratorFactory
46
+ * @param ResourceConnection $resource|null
31
47
*/
32
- public function __construct (ProductGeneratorFactory $ productGeneratorFactory )
33
- {
48
+ public function __construct (
49
+ ProductGeneratorFactory $ productGeneratorFactory ,
50
+ ResourceConnection $ resource = null
51
+ ) {
34
52
$ this ->productGeneratorFactory = $ productGeneratorFactory ;
53
+
54
+ $ this ->resource = $ resource ?: ObjectManager::getInstance ()->get (
55
+ ResourceConnection::class
56
+ );
35
57
}
36
58
37
59
/**
@@ -45,30 +67,90 @@ public function generate($products, $fixtureMap)
45
67
{
46
68
$ this ->productGeneratorFactory ->create ([
47
69
'customTableMap ' => [
70
+ 'catalog_product_bundle_option ' => [
71
+ 'entity_id_field ' => EntityGenerator::SKIP_ENTITY_ID_BINDING ,
72
+ 'handler ' => function ($ productId , $ entityNumber , $ fixture , $ binds ) {
73
+ foreach ($ binds as &$ bind ) {
74
+ $ bind ['option_id ' ] = $ this ->generateOptionId (
75
+ $ entityNumber ,
76
+ $ bind ['option_id ' ],
77
+ $ fixture
78
+ );
79
+
80
+ $ bind ['parent_id ' ] = $ productId ;
81
+ }
82
+
83
+ return $ binds ;
84
+ },
85
+ ],
86
+ 'sequence_product_bundle_option ' => [
87
+ 'entity_id_field ' => EntityGenerator::SKIP_ENTITY_ID_BINDING ,
88
+ 'handler ' => function ($ productId , $ entityNumber , $ fixture , $ binds ) {
89
+ foreach ($ binds as &$ bind ) {
90
+ $ bind ['sequence_value ' ] = $ this ->generateSequenceId (
91
+ 'sequence_product_bundle_option '
92
+ );
93
+ }
94
+
95
+ return $ binds ;
96
+ },
97
+ ],
48
98
'catalog_product_bundle_option_value ' => [
49
99
'entity_id_field ' => EntityGenerator::SKIP_ENTITY_ID_BINDING ,
50
100
'handler ' => function ($ productId , $ entityNumber , $ fixture , $ binds ) {
51
101
foreach ($ binds as &$ bind ) {
52
- $ bind ['option_id ' ] = $ this ->generateOptionId ($ bind ['option_id ' ], $ entityNumber , $ fixture );
102
+ $ bind ['option_id ' ] = $ this ->generateOptionId (
103
+ $ entityNumber ,
104
+ $ bind ['option_id ' ],
105
+ $ fixture
106
+ );
107
+
108
+ $ bind ['parent_product_id ' ] = $ productId ;
53
109
}
110
+
54
111
return $ binds ;
55
112
},
56
113
],
57
114
'catalog_product_bundle_selection ' => [
58
115
'entity_id_field ' => EntityGenerator::SKIP_ENTITY_ID_BINDING ,
59
116
'handler ' => function ($ productId , $ entityNumber , $ fixture , $ binds ) {
60
117
foreach ($ binds as &$ bind ) {
61
- $ bind ['option_id ' ] = $ this ->generateOptionId ($ bind ['option_id ' ], $ entityNumber , $ fixture );
118
+ $ bind ['selection_id ' ] = $ this ->generateSelectionId (
119
+ $ entityNumber ,
120
+ $ bind ['selection_id ' ],
121
+ $ fixture
122
+ );
123
+
62
124
$ bind ['parent_product_id ' ] = $ productId ;
63
- $ simpleProductId = $ this ->generateSimpleProductId (
125
+
126
+ $ bind ['option_id ' ] = $ this ->generateOptionId (
127
+ $ entityNumber ,
128
+ $ bind ['option_id ' ],
129
+ $ fixture
130
+ );
131
+
132
+ $ bind ['product_id ' ] = $ this ->generateSimpleProductId (
64
133
$ bind ['product_id ' ],
65
134
$ entityNumber ,
66
135
$ fixture
67
136
);
68
- $ bind ['product_id ' ] = $ simpleProductId ;
69
- $ bind ['selection_price_value ' ] = $ fixture ['price ' ]($ simpleProductId );
70
- $ bind ['selection_price_type ' ] = $ fixture ['priceType ' ]($ simpleProductId );
137
+
138
+ $ bind ['selection_price_type ' ] = $ fixture ['priceType ' ]($ bind ['product_id ' ]);
139
+ $ bind ['selection_price_value ' ] = $ fixture ['price ' ]($ bind ['product_id ' ]);
140
+ }
141
+
142
+ return $ binds ;
143
+ },
144
+ ],
145
+ 'sequence_product_bundle_selection ' => [
146
+ 'entity_id_field ' => EntityGenerator::SKIP_ENTITY_ID_BINDING ,
147
+ 'handler ' => function ($ productId , $ entityNumber , $ fixture , $ binds ) {
148
+ foreach ($ binds as &$ bind ) {
149
+ $ bind ['sequence_value ' ] = $ this ->generateSequenceId (
150
+ 'sequence_product_bundle_selection '
151
+ );
71
152
}
153
+
72
154
return $ binds ;
73
155
},
74
156
],
@@ -91,16 +173,64 @@ public function generate($products, $fixtureMap)
91
173
}
92
174
93
175
/**
94
- * Generate value of option_id for $entityNumber bundle product based on previous option_id
176
+ * Generates an option Id.
177
+ *
178
+ * @param int $entityNumber
179
+ * @param int $originalOptionId
180
+ * @param array $fixture
181
+ *
182
+ * @return int|null
183
+ */
184
+ private function generateOptionId ($ entityNumber , $ originalOptionId , array $ fixture )
185
+ {
186
+ if ($ originalOptionId ) {
187
+ return $ fixture ['_bundle_options ' ] * ($ entityNumber + 1 ) + $ originalOptionId ;
188
+ }
189
+
190
+ return $ originalOptionId ;
191
+ }
192
+
193
+ /**
194
+ * Generates a selection Id.
95
195
*
96
- * @param int $previousOptionId
97
196
* @param int $entityNumber
197
+ * @param int $originalSelectionId
98
198
* @param array $fixture
199
+ *
200
+ * @return int|null
201
+ */
202
+ private function generateSelectionId ($ entityNumber , $ originalSelectionId , array $ fixture )
203
+ {
204
+ if ($ originalSelectionId ) {
205
+ $ selectionsPerProduct = $ fixture ['_bundle_products_per_option ' ] * $ fixture ['_bundle_options ' ];
206
+
207
+ return $ selectionsPerProduct * ($ entityNumber + 1 ) + $ originalSelectionId ;
208
+ }
209
+
210
+ return $ originalSelectionId ;
211
+ }
212
+
213
+ /**
214
+ * Generates an Id for the given sequence table.
215
+ *
216
+ * @param string $tableName
217
+ *
99
218
* @return int
100
219
*/
101
- private function generateOptionId ( $ previousOptionId , $ entityNumber , array $ fixture )
220
+ private function generateSequenceId ( $ tableName )
102
221
{
103
- return $ previousOptionId + $ entityNumber * $ fixture ['_bundle_options ' ] + $ fixture ['_bundle_options ' ];
222
+ if (!$ this ->sequenceValues [$ tableName ]) {
223
+ $ connection = $ this ->resource ->getConnection ();
224
+
225
+ $ this ->sequenceValues [$ tableName ] = $ connection ->fetchOne (
226
+ $ connection ->select ()->from (
227
+ $ connection ->getTableName ($ tableName ),
228
+ 'MAX(`sequence_value`) '
229
+ )
230
+ );
231
+ }
232
+
233
+ return ++$ this ->sequenceValues [$ tableName ];
104
234
}
105
235
106
236
/**
0 commit comments