Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigurableAttributesData.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Fixture\DataSource;
11 use Magento\Mtf\Fixture\FixtureFactory;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 use Magento\Mtf\Fixture\InjectableFixture;
14 use Magento\Mtf\Repository\RepositoryFactory;
16 
20 class ConfigurableAttributesData extends DataSource
21 {
27  protected $fixtureFactory;
28 
34  protected $attributesData = [];
35 
41  protected $mediaPathTmp = '/pub/media/tmp/catalog/product/';
42 
48  protected $variationsMatrix = [];
49 
55  private $addMediaGallery = false;
56 
62  protected $attributes = [];
63 
69  protected $attributeSet;
70 
76  protected $products = [];
77 
83  private $bulkImagesPriceQuantity = [];
84 
92  public function __construct(
93  RepositoryFactory $repositoryFactory,
94  FixtureFactory $fixtureFactory,
95  array $data,
96  array $params = []
97  ) {
98  $this->fixtureFactory = $fixtureFactory;
99  $this->params = $params;
100  $dataset = [];
101  if (isset($data['dataset']) && isset($this->params['repository'])) {
102  $dataset = $repositoryFactory->get($this->params['repository'])->get($data['dataset']);
103  unset($data['dataset']);
104  }
105 
106  if (isset($data['media_gallery'])) {
107  $this->addMediaGallery = true;
108  unset($data['media_gallery']);
109  }
110 
111  $data = array_replace_recursive($data, $dataset);
112 
113  if (!empty($data)) {
114  $this->prepareAttributes($data);
115  $this->prepareAttributesData($data);
116  $this->prepareProducts($data);
117  $this->prepareVariationsMatrix($data);
118  $this->prepareData();
119  $this->prepareBulkImagesPriceQuantity($data);
120  }
121  }
122 
129  protected function prepareAttributes(array $data)
130  {
131  if (!isset($data['attributes'])) {
132  return;
133  }
134 
135  foreach ($data['attributes'] as $key => $attribute) {
136  if (is_string($attribute)) {
137  list($fixture, $dataset) = explode('::', $attribute);
139  $attribute = $this->fixtureFactory->createByCode($fixture, ['dataset' => $dataset]);
140  }
141  if (!$attribute->hasData('attribute_id')) {
142  $attribute->persist();
143  }
144  $this->attributes[$key] = $attribute;
145  }
146  }
147 
154  protected function prepareAttributesData(array $data)
155  {
156  foreach ($this->attributes as $attributeKey => $attribute) {
157  $attributeData = $attribute->getData();
158  $options = [];
159  foreach ($attributeData['options'] as $key => $option) {
160  $options['option_key_' . $key] = $option;
161  }
162  $attributeData['options'] = $options;
163 
164  $this->attributesData[$attributeKey] = $attributeData;
165  }
166 
167  $this->attributesData = array_replace_recursive(
168  isset($data['attributes_data']) ? $data['attributes_data'] : [],
169  $this->attributesData
170  );
171  }
172 
178  public function generateProducts()
179  {
180  $assignedProducts = ['products' => []];
181  foreach (array_keys($this->variationsMatrix) as $variation) {
182  $assignedProducts['products'][$variation] = 'catalogProductSimple::default';
183  }
184 
185  $this->prepareProducts($assignedProducts);
186  }
187 
194  protected function prepareProducts(array $data)
195  {
196  if (!isset($data['products'])) {
197  return;
198  }
199 
200  $attributeSetData = [];
201  if (!empty($this->attributes)) {
202  $attributeSetData['attribute_set_id'] = ['attribute_set' => $this->createAttributeSet()];
203  }
204 
205  foreach ($data['products'] as $key => $product) {
206  if (is_string($product)) {
207  list($fixture, $dataset) = explode('::', $product);
208  $attributeData = ['attributes' => $this->getProductAttributeData($key)];
209  $productData = isset($this->variationsMatrix[$key]) ? $this->variationsMatrix[$key] : [];
210 
211  $product = $this->fixtureFactory->createByCode(
212  $fixture,
213  [
214  'dataset' => $dataset,
215  'data' => array_merge($attributeSetData, $attributeData, $productData)
216  ]
217  );
218  }
219  if (!$product->hasData('id')) {
220  $product->persist();
221  }
222 
223  $this->products[$key] = $product;
224  }
225  }
226 
232  protected function createAttributeSet()
233  {
234  if (!$this->attributeSet) {
235  $this->attributeSet = $this->fixtureFactory->createByCode(
236  'catalogAttributeSet',
237  [
238  'dataset' => 'custom_attribute_set',
239  'data' => [
240  'assigned_attributes' => [
241  'attributes' => array_values($this->attributes),
242  ],
243  ]
244  ]
245  );
246  $this->attributeSet->persist();
247  }
248 
249  return $this->attributeSet;
250  }
251 
258  protected function getProductAttributeData($key)
259  {
260  $compositeKeys = explode(' ', $key);
261  $data = [];
262 
263  foreach ($compositeKeys as $compositeKey) {
264  $attributeId = $this->getAttributeOptionId($compositeKey);
265  if ($attributeId) {
266  $compositeKey = explode(':', $compositeKey);
267  $attributeKey = $compositeKey[0];
268  $data[$this->attributesData[$attributeKey]['attribute_code']] = $attributeId;
269  }
270  }
271 
272  return $data;
273  }
274 
281  protected function getAttributeOptionId($compositeKey)
282  {
283  list($attributeKey, $optionKey) = explode(':', $compositeKey);
284  return isset($this->attributesData[$attributeKey]['options'][$optionKey]['id'])
285  ? $this->attributesData[$attributeKey]['options'][$optionKey]['id']
286  : null;
287  }
288 
297  protected function prepareVariationsMatrix(array $data)
298  {
299  $variationsMatrix = [];
300 
301  // generate matrix
302  foreach ($this->attributesData as $attributeKey => $attribute) {
304  }
305 
306  if (isset($data['matrix'])) {
307  foreach ($data['matrix'] as $key => $value) {
308  if (isset($value['sku']) && $value['sku'] === '') {
309  unset($variationsMatrix[$key]['sku']);
310  unset($data['matrix'][$key]['sku']);
311  }
312  }
313  }
314 
315  $this->variationsMatrix = isset($data['matrix'])
316  ? array_replace_recursive($variationsMatrix, $data['matrix'])
318 
319  // assigned products
320  foreach ($this->variationsMatrix as $key => $row) {
321  if (isset($this->products[$key])) {
323  $product = $this->products[$key];
324  $quantityAndStockStatus = $product->getQuantityAndStockStatus();
325  $productData = [
326  'configurable_attribute' => $product->getId(),
327  'name' => $product->getName(),
328  'sku' => $product->getSku(),
329  'qty' => $quantityAndStockStatus['qty'],
330  'weight' => $product->getWeight(),
331  'price' => $product->getPrice()
332  ];
333  $this->variationsMatrix[$key] = array_replace_recursive($this->variationsMatrix[$key], $productData);
334  } else {
335  $this->variationsMatrix[$key] = array_replace_recursive(
336  $this->variationsMatrix[$key],
337  [
338  'weight' => 1,
339  'qty' => 10,
340  ],
341  $row
342  );
343  }
344  }
345  }
346 
357  protected function addVariationMatrix(array $variationsMatrix, array $attribute, $attributeKey)
358  {
359  $result = [];
360 
361  /* If empty matrix add one empty row */
362  if (empty($variationsMatrix)) {
363  $variationIsolation = mt_rand(10000, 70000);
365  [
366  'name' => "In configurable product {$variationIsolation}",
367  'sku' => "in_configurable_product_{$variationIsolation}",
368  ],
369  ];
370  }
371 
372  foreach ($variationsMatrix as $rowKey => $row) {
373  $randIsolation = mt_rand(1, 100);
374  $rowName = $row['name'];
375  $rowSku = $row['sku'];
376  $index = 1;
377 
378  if (isset($attribute['options'])) {
379  foreach ($attribute['options'] as $optionKey => $option) {
380  $compositeKey = "{$attributeKey}:{$optionKey}";
381  $row['name'] = $rowName . ' ' . $randIsolation . ' ' . $index;
382  $row['sku'] = $rowSku . '_' . $randIsolation . '_' . $index;
383  $row['price'] = $option['pricing_value'];
384  if ($this->addMediaGallery) {
385  $row['media_gallery'] = $this->prepareMediaGallery();
386  }
387  $newRowKey = $rowKey ? "{$rowKey} {$compositeKey}" : $compositeKey;
388  $result[$newRowKey] = $row;
389  $index++;
390  }
391  }
392  }
393 
394  return $result;
395  }
396 
403  protected function prepareMediaGallery($filename = 'option_image.jpg')
404  {
405  $filePath = $this->getFullPath($filename);
406  if (!file_exists($filePath)) {
407  $optionImage = imagecreate(300, 200);
408  $colorYellow = imagecolorallocate($optionImage, 255, 255, 0);
409  imagefilledrectangle($optionImage, 50, 50, 250, 150, $colorYellow);
410  $directory = dirname($filePath);
411  if (!file_exists($directory)) {
412  mkdir($directory, 0777, true);
413  }
414  imagejpeg($optionImage, $filePath);
415  imagedestroy($optionImage);
416  }
417 
418  return [
419  'images' => [
420  0 => [
421  'position' => 1,
422  'file' => $filename,
423  'disabled' => 0,
424  'label' => '1231414',
425  ],
426  ],
427  ];
428  }
429 
436  private function getFullPath($filename)
437  {
438  return BP . $this->mediaPathTmp . $filename;
439  }
440 
446  protected function prepareData()
447  {
448  $attributeFields = [
449  'frontend_label',
450  'label',
451  'frontend_input',
452  'attribute_code',
453  'attribute_id',
454  'is_required',
455  'options',
456  ];
457  $optionFields = [
458  'admin',
459  'label',
460  'pricing_value',
461  'include',
462  ];
463  $variationMatrixFields = [
464  'configurable_attribute',
465  'name',
466  'sku',
467  'price',
468  'qty',
469  'weight',
470  'media_gallery'
471  ];
472 
473  $this->data = [
474  'matrix' => [],
475  'attributes_data' => [],
476  ];
477 
478  foreach ($this->attributesData as $attributeKey => $attribute) {
479  foreach ($attribute['options'] as $optionKey => $option) {
480  $option['label'] = isset($option['view']) ? $option['view'] : $option['label'];
481  $attribute['options'][$optionKey] = array_intersect_key($option, array_flip($optionFields));
482  }
483  $attribute['label'] = isset($attribute['label'])
484  ? $attribute['label']
485  : (isset($attribute['frontend_label']) ? $attribute['frontend_label'] : null);
486  $attribute = array_intersect_key($attribute, array_flip($attributeFields));
487 
488  $this->data['attributes_data'][$attributeKey] = $attribute;
489  }
490  foreach ($this->variationsMatrix as $key => $variationMatrix) {
491  $this->data['matrix'][$key] = array_intersect_key($variationMatrix, array_flip($variationMatrixFields));
492  }
493  }
494 
501  private function prepareBulkImagesPriceQuantity(array $data)
502  {
503  if (isset($data['bulk_images_price_quantity'])) {
504  $this->bulkImagesPriceQuantity = $data['bulk_images_price_quantity'];
505  }
506  }
507 
513  public function getAttributesData()
514  {
515  return $this->attributesData;
516  }
517 
523  public function getVariationsMatrix()
524  {
526  }
527 
533  public function getBulkImagesPriceQuantity()
534  {
535  return $this->bulkImagesPriceQuantity;
536  }
537 
543  public function getAttributes()
544  {
545  return $this->attributes;
546  }
547 
553  public function getAttributeSet()
554  {
555  return $this->attributeSet;
556  }
557 
563  public function getProducts()
564  {
565  return $this->products;
566  }
567 }
__construct(RepositoryFactory $repositoryFactory, FixtureFactory $fixtureFactory, array $data, array $params=[])
$value
Definition: gender.phtml:16
$productData
const BP
Definition: autoload.php:14
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$index
Definition: list.phtml:44