Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BundleProductGenerator.php
Go to the documentation of this file.
1 <?php
7 
10 
25 {
29  private $sequenceValues = [
30  'sequence_product_bundle_option' => null,
31  'sequence_product_bundle_selection' => null
32  ];
33 
37  private $productGeneratorFactory;
38 
42  private $resource;
43 
48  public function __construct(
49  ProductGeneratorFactory $productGeneratorFactory,
50  ResourceConnection $resource = null
51  ) {
52  $this->productGeneratorFactory = $productGeneratorFactory;
53 
54  $this->resource = $resource ?: ObjectManager::getInstance()->get(
55  ResourceConnection::class
56  );
57  }
58 
69  public function generate($products, $fixtureMap)
70  {
71  $this->productGeneratorFactory->create([
72  'customTableMap' => [
73  'catalog_product_bundle_option' => [
74  'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
75  'handler' => function ($productId, $entityNumber, $fixture, $binds) {
76  foreach ($binds as &$bind) {
77  $bind['option_id'] = $this->generateOptionId(
78  $entityNumber,
79  $bind['option_id'],
80  $fixture
81  );
82 
83  $bind['parent_id'] = $productId;
84  }
85 
86  return $binds;
87  },
88  ],
89  'sequence_product_bundle_option' => [
90  'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
91  'handler' => function ($productId, $entityNumber, $fixture, $binds) {
92  foreach ($binds as &$bind) {
93  $bind['sequence_value'] = $this->generateSequenceId(
94  'sequence_product_bundle_option'
95  );
96  }
97 
98  return $binds;
99  },
100  ],
101  'catalog_product_bundle_option_value' => [
102  'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
103  'handler' => function ($productId, $entityNumber, $fixture, $binds) {
104  foreach ($binds as &$bind) {
105  $bind['option_id'] = $this->generateOptionId(
106  $entityNumber,
107  $bind['option_id'],
108  $fixture
109  );
110 
111  $bind['parent_product_id'] = $productId;
112  }
113 
114  return $binds;
115  },
116  ],
117  'catalog_product_bundle_selection' => [
118  'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
119  'handler' => function ($productId, $entityNumber, $fixture, $binds) {
120  foreach ($binds as &$bind) {
121  $bind['selection_id'] = $this->generateSelectionId(
122  $entityNumber,
123  $bind['selection_id'],
124  $fixture
125  );
126 
127  $bind['parent_product_id'] = $productId;
128 
129  $bind['option_id'] = $this->generateOptionId(
130  $entityNumber,
131  $bind['option_id'],
132  $fixture
133  );
134 
135  $bind['product_id'] = $this->generateSimpleProductId(
136  $bind['product_id'],
137  $entityNumber,
138  $fixture
139  );
140 
141  $bind['selection_price_type'] = $fixture['priceType']($bind['product_id']);
142  $bind['selection_price_value'] = $fixture['price']($bind['product_id']);
143  }
144 
145  return $binds;
146  },
147  ],
148  'sequence_product_bundle_selection' => [
149  'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
150  'handler' => function ($productId, $entityNumber, $fixture, $binds) {
151  foreach ($binds as &$bind) {
152  $bind['sequence_value'] = $this->generateSequenceId(
153  'sequence_product_bundle_selection'
154  );
155  }
156 
157  return $binds;
158  },
159  ],
160  'catalog_product_relation' => [
161  'entity_id_field' => EntityGenerator::SKIP_ENTITY_ID_BINDING,
162  'handler' => function ($productId, $entityNumber, $fixture, $binds) {
163  foreach ($binds as &$bind) {
164  $bind['parent_id'] = $productId;
165  $bind['child_id'] = $this->generateSimpleProductId(
166  $bind['child_id'],
167  $entityNumber,
168  $fixture
169  );
170  }
171  return $binds;
172  },
173  ],
174  ]
175  ])->generate($products, $fixtureMap);
176  }
177 
187  private function generateOptionId($entityNumber, $originalOptionId, array $fixture)
188  {
189  if ($originalOptionId) {
190  return $fixture['_bundle_options'] * ($entityNumber + 1) + $originalOptionId;
191  }
192 
193  return $originalOptionId;
194  }
195 
205  private function generateSelectionId($entityNumber, $originalSelectionId, array $fixture)
206  {
207  if ($originalSelectionId) {
208  $selectionsPerProduct = $fixture['_bundle_products_per_option'] * $fixture['_bundle_options'];
209 
210  return $selectionsPerProduct * ($entityNumber + 1) + $originalSelectionId;
211  }
212 
213  return $originalSelectionId;
214  }
215 
223  private function generateSequenceId($tableName)
224  {
225  if (!$this->sequenceValues[$tableName]) {
226  $connection = $this->resource->getConnection();
227 
228  $this->sequenceValues[$tableName] = $connection->fetchOne(
229  $connection->select()->from(
230  $this->resource->getTableName($tableName),
231  'MAX(`sequence_value`)'
232  )
233  );
234  }
235 
236  return ++$this->sequenceValues[$tableName];
237  }
238 
247  private function generateSimpleProductId($previousProductId, $entityNumber, array $fixture)
248  {
249  return $previousProductId +
250  $entityNumber * $fixture['_bundle_products_per_option'] * $fixture['_bundle_options'];
251  }
252 }
$tableName
Definition: trigger.php:13
__construct(ProductGeneratorFactory $productGeneratorFactory, ResourceConnection $resource=null)
$connection
Definition: bulk.php:13