Skip to content

Commit 33261e7

Browse files
authored
Merge pull request #1042 from magento-performance/MAGETWO-66727
[Performance] MAGETWO-66727: Update performance profiles with swatches
2 parents 404a1a0 + c6acc08 commit 33261e7

File tree

8 files changed

+187
-48
lines changed

8 files changed

+187
-48
lines changed

setup/performance-toolkit/profiles/ce/extra_large.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
<simple_products>600000</simple_products> <!-- Simple products count -->
1515
<configurable_products> <!-- Configurable product -->
1616
<config>
17-
<attributes>1</attributes>
18-
<options>24</options>
17+
<attributes>
18+
<attribute>
19+
<options>3</options>
20+
<swatches>image</swatches>
21+
</attribute>
22+
<attribute>
23+
<options>8</options>
24+
</attribute>
25+
</attributes>
1926
<sku>Configurable Product %s</sku>
2027
<products>16000</products>
2128
</config>

setup/performance-toolkit/profiles/ce/large.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
<simple_products>300000</simple_products> <!-- Simple products count -->
1515
<configurable_products> <!-- Configurable product -->
1616
<config>
17-
<attributes>1</attributes>
18-
<options>24</options>
17+
<attributes>
18+
<attribute>
19+
<options>3</options>
20+
<swatches>image</swatches>
21+
</attribute>
22+
<attribute>
23+
<options>8</options>
24+
</attribute>
25+
</attributes>
1926
<sku>Configurable Product %s</sku>
2027
<products>8000</products>
2128
</config>

setup/performance-toolkit/profiles/ce/medium.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
<simple_products>24000</simple_products> <!-- Simple products count -->
1515
<configurable_products> <!-- Configurable product -->
1616
<config>
17-
<attributes>1</attributes>
18-
<options>24</options>
17+
<attributes>
18+
<attribute>
19+
<options>3</options>
20+
<swatches>image</swatches>
21+
</attribute>
22+
<attribute>
23+
<options>8</options>
24+
</attribute>
25+
</attributes>
1926
<sku>Configurable Product %s</sku>
2027
<products>640</products>
2128
</config>

setup/performance-toolkit/profiles/ce/medium_msite.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
<simple_products>4000</simple_products> <!-- Simple products count -->
1515
<configurable_products> <!-- Configurable product -->
1616
<config>
17-
<attributes>1</attributes>
18-
<options>24</options>
17+
<attributes>
18+
<attribute>
19+
<options>3</options>
20+
<swatches>image</swatches>
21+
</attribute>
22+
<attribute>
23+
<options>8</options>
24+
</attribute>
25+
</attributes>
1926
<sku>Configurable Product %s</sku>
2027
<products>800</products>
2128
</config>

setup/performance-toolkit/profiles/ce/small.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@
1414
<simple_products>800</simple_products> <!-- Simple products count -->
1515
<configurable_products> <!-- Configurable product -->
1616
<config>
17-
<attributes>1</attributes>
18-
<options>24</options>
17+
<attributes>
18+
<attribute>
19+
<options>3</options>
20+
<swatches>image</swatches>
21+
</attribute>
22+
<attribute>
23+
<options>8</options>
24+
</attribute>
25+
</attributes>
1926
<sku>Configurable Product %s</sku>
2027
<products>16</products>
2128
</config>

setup/src/Magento/Setup/Fixtures/AttributeSet/Pattern.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function generateAttributeSet(
4646
for ($index = 1; $index <= $attributesPerSet; $index++) {
4747
$attributeData = $this->generateAttribute(
4848
$index,
49-
$optionsPerAttribute
49+
is_array($optionsPerAttribute) ? $optionsPerAttribute[$index-1] : $optionsPerAttribute
5050
);
5151
if (is_callable($attributePattern)) {
5252
$attributeData = $attributePattern($index, $attributeData);

setup/src/Magento/Setup/Fixtures/ConfigurableProductsFixture.php

Lines changed: 112 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,25 @@
4747
* </config>
4848
* </configurable_products>
4949
*
50+
* 2.3 Generate products based on dynamically created attribute set with specified configuration per each attribute
51+
* <configurable_products> <!-- Configurable product -->
52+
* <config>
53+
* <attributes>
54+
* <!-- Configuration for a first attribute -->
55+
* <attribute>
56+
* <options>{Amount of options per attribute}</options>
57+
* <swatches>color|image</swatches> Type of Swatch attribute
58+
* </attribute>
59+
* <!-- Configuration for a second attribute -->
60+
* <attribute>
61+
* <options>{Amount of options per attribute}</options>
62+
* </attribute>
63+
* </attributes>
64+
* <sku>{Configurable sku pattern with %s}</sku>
65+
* <products>{Amount of configurable products}</products>
66+
* </config>
67+
* </configurable_products>
68+
*
5069
* Products will be uniformly distributed per categories and websites
5170
* If node "assign_entities_to_all_websites" from profile is set to "1" then products will be assigned to all websites
5271
*
@@ -392,31 +411,16 @@ public function introduceParamLabels()
392411
*/
393412
public function printInfo(OutputInterface $output)
394413
{
395-
$config = $this->fixtureModel->getValue('configurable_products', []);
396-
if (!$config) {
414+
if (!$this->fixtureModel->getValue('configurable_products', [])) {
397415
return;
398416
}
399417

400-
$generalAmount = is_numeric($config) ? $config : array_sum(array_column($config, 'products'));
401-
$output->writeln(sprintf('<info> |- Configurable products: %s</info>', $generalAmount));
402-
403418
$configurableProductConfig = $this->prepareConfigurableConfig(
404419
$this->getDefaultAttributeSetsWithAttributes()
405420
);
421+
$generalAmount = array_sum(array_column($configurableProductConfig, 'products'));
406422

407-
foreach ($configurableProductConfig as $config) {
408-
$attributeSetName = isset($config['attributeSet'])
409-
? $config['attributeSet']
410-
: 'Dynamic Attribute Set ' . $config['attributes'] . '-' . $config['options'];
411-
412-
$output->writeln(
413-
sprintf(
414-
'<info> |--- %s products for attribute set "%s"</info>',
415-
$config['products'],
416-
$attributeSetName
417-
)
418-
);
419-
}
423+
$output->writeln(sprintf('<info> |- Configurable products: %s</info>', $generalAmount));
420424
}
421425

422426
/**
@@ -477,6 +481,7 @@ function (&$item, $key) {
477481
*
478482
* @return array
479483
* @throws ValidatorException
484+
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
480485
*/
481486
private function getConfigurableProductConfig()
482487
{
@@ -500,14 +505,19 @@ private function getConfigurableProductConfig()
500505
$skuPull = [];
501506
foreach ($configurableProductConfig as $i => &$config) {
502507
$attributeSet = $config['attributeSet'];
503-
$attributes = (int)$config['attributes'];
508+
$attributes = $config['attributes'];
504509
$options = (int)$config['options'];
505510
if ($attributeSet && isset($defaultAttributeSets[$attributeSet])) {
506511
// process default attribute sets
507512
$attributeSet = $defaultAttributeSets[$attributeSet];
508513
$attributes = count($attributeSet['attributes']);
509514
$options = count($attributeSet['attributes'][0]['values']);
515+
} elseif (is_array($attributes)) {
516+
$attributeSet = $this->getCustomAttributeSet($attributes);
517+
$options = array_column($attributes, 'options');
518+
$attributes = count($attributes);
510519
} elseif ($attributes && $options) {
520+
$attributes = (int)$attributes;
511521
// process dynamic attribute sets
512522
$attributeSet = $this->getAttributeSet($attributes, $options, $config['swatches']);
513523
}
@@ -522,7 +532,7 @@ private function getConfigurableProductConfig()
522532
$config['attributeSet'] = $this->convertAttributesToDBFormat($attributeSet);
523533
$config['attributes'] = $attributes;
524534
$config['options'] = $options;
525-
$config['variationCount'] = pow($options, $attributes);
535+
$config['variationCount'] = is_array($options) ? array_product($options) : pow($options, $attributes);
526536
$skuPull[] = $config['sku'];
527537
}
528538

@@ -556,6 +566,16 @@ private function prepareConfigurableConfig($defaultAttributeSets)
556566
} else {
557567
$configurableConfigs = $configurableConfigs['config'];
558568
}
569+
570+
foreach ($configurableConfigs as &$config) {
571+
if (isset($config['attributes']) && is_array($config['attributes'])) {
572+
if (!isset($config['attributes']['attribute'][0])) {
573+
$config['attributes'] = [$config['attributes']['attribute']];
574+
} else {
575+
$config['attributes'] = $config['attributes']['attribute'];
576+
}
577+
}
578+
}
559579
} else {
560580
throw new ValidatorException(__('Configurable product config is invalid'));
561581
}
@@ -613,11 +633,10 @@ private function getConfigurableSkuPattern($config, $attributeSetName)
613633
private function getAttributeSet($attributes, $options, $swatches)
614634
{
615635
$attributeCode = 'configurable_attribute' . $attributes . '_' . $options . '_';
616-
$setName = 'Dynamic Attribute Set ' . $attributes . '-' . $options;
617636

618637
return $this->attributeSetsFixture->createAttributeSet(
619638
$this->attributePattern->generateAttributeSet(
620-
$setName,
639+
$this->getAttributeSetName($attributes, $options),
621640
$attributes,
622641
$options,
623642
function ($index, $attribute) use ($attributeCode, $options, $swatches) {
@@ -635,6 +654,68 @@ function ($index, $attribute) use ($attributeCode, $options, $swatches) {
635654
);
636655
}
637656

657+
/**
658+
* Provide attribute set based on attributes configuration
659+
*
660+
* @param array $attributes
661+
* @return array
662+
*/
663+
private function getCustomAttributeSet(array $attributes)
664+
{
665+
$attributeSetName = $this->getAttributeSetName(
666+
count($attributes),
667+
implode(',', array_column($attributes, 'options'))
668+
);
669+
670+
$pattern = $this->attributePattern->generateAttributeSet(
671+
$attributeSetName,
672+
count($attributes),
673+
array_column($attributes, 'options'),
674+
function ($index, $attribute) use ($attributeSetName, $attributes) {
675+
$swatch = [];
676+
$timeStamp = time();
677+
$data = [
678+
'attribute_code' => sprintf('custom_attribute_%s_%s', $index, $timeStamp),
679+
'frontend_label' => 'Dynamic Attribute ' . sprintf('custom_attribute_%s_%s', $index, $timeStamp),
680+
];
681+
682+
if (isset($attributes[$index-1]['swatches'])) {
683+
$data['is_visible_in_advanced_search'] = 1;
684+
$data['is_searchable'] = 1;
685+
$data['is_filterable'] = 1;
686+
$data['is_filterable_in_search'] = 1;
687+
$data['used_in_product_listing'] = 1;
688+
689+
$swatch = $this->swatchesGenerator->generateSwatchData(
690+
(int) $attributes[$index-1]['options'],
691+
$attributeSetName . $index,
692+
$attributes[$index-1]['swatches']
693+
);
694+
}
695+
696+
return array_replace_recursive(
697+
$attribute,
698+
$data,
699+
$swatch
700+
);
701+
}
702+
);
703+
704+
return $this->attributeSetsFixture->createAttributeSet($pattern);
705+
}
706+
707+
/**
708+
* Provide attribute set name based on amount of attributes and options per attribute set
709+
*
710+
* @param int $attributesCount
711+
* @param int $optionsCount
712+
* @return string
713+
*/
714+
private function getAttributeSetName($attributesCount, $optionsCount)
715+
{
716+
return sprintf('Dynamic Attribute Set %s-%s', $attributesCount, $optionsCount);
717+
}
718+
638719
/**
639720
* @return array
640721
*/
@@ -689,13 +770,13 @@ private function getConfigurableProductsVariationsValue()
689770
*/
690771
private function getAdditionalAttributesClosure(array $attributes, $variationCount)
691772
{
692-
return function ($attributeSetId, $index, $entityNumber) use ($attributes, $variationCount) {
693-
$variationIndex = $this->getConfigurableVariationIndex($entityNumber, $variationCount) - 1;
694-
$attributeValues = [];
695-
$optionsPerAttribute = count($attributes[0]['values']);
696-
$variationIndex = $variationIndex % $variationCount;
773+
$optionsPerAttribute = array_map(function ($attr) {
774+
return count($attr['values']);
775+
}, $attributes);
776+
$variationsMatrix = $this->generateVariationsMatrix(count($attributes), $optionsPerAttribute);
697777

698-
$variationsMatrix = $this->generateVariationsMatrix(count($attributes), $optionsPerAttribute);
778+
return function ($attributeSetId, $index, $entityNumber) use ($attributes, $variationCount, $variationsMatrix) {
779+
$variationIndex = $this->getConfigurableVariationIndex($entityNumber, $variationCount) - 1;
699780
if (isset($variationsMatrix[$variationIndex])) {
700781
$tempProductData = [];
701782
foreach ($variationsMatrix[$variationIndex] as $attributeIndex => $optionIndex) {
@@ -706,7 +787,7 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
706787
return $tempProductData;
707788
}
708789

709-
return $attributeValues;
790+
return [];
710791
};
711792
}
712793

@@ -719,8 +800,8 @@ private function getAdditionalAttributesClosure(array $attributes, $variationCou
719800
private function generateVariationsMatrix($attributesPerSet, $optionsPerAttribute)
720801
{
721802
$variationsMatrix = null;
722-
for ($i = 1; $i <= $attributesPerSet; $i++) {
723-
$variationsMatrix[] = range(0, $optionsPerAttribute - 1);
803+
for ($i = 0; $i < $attributesPerSet; $i++) {
804+
$variationsMatrix[] = range(0, $optionsPerAttribute[$i] - 1);
724805
}
725806
return $this->generateVariations($variationsMatrix);
726807
}

setup/src/Magento/Setup/Model/FixtureGenerator/EntityGenerator.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\Setup\Model\FixtureGenerator;
88

9+
use Magento\Framework\Exception\LocalizedException;
910
use Magento\Framework\Exception\ValidatorException;
1011

1112
/**
@@ -138,14 +139,25 @@ public function __construct(
138139
* @param TemplateEntityGeneratorInterface $entityGenerator
139140
* @param int $entitiesAmount
140141
* @param callable $fixture
142+
* @throws LocalizedException
141143
* @return void
142144
*/
143145
public function generate(TemplateEntityGeneratorInterface $entityGenerator, $entitiesAmount, callable $fixture)
144146
{
145-
$this->sqlCollector->enable();
146-
$entity = $entityGenerator->generateEntity();
147-
$this->sqlCollector->disable();
148-
$entity->delete();
147+
$this->getConnection()->beginTransaction();
148+
try {
149+
$this->sqlCollector->enable();
150+
$entity = $entityGenerator->generateEntity();
151+
$this->sqlCollector->disable();
152+
$entity->delete();
153+
$this->getConnection()->commit();
154+
} catch (\Exception $e) {
155+
$this->getConnection()->rollBack();
156+
throw new LocalizedException(
157+
__('Cannot generate entities - error occurred during template creation: %1', $e->getMessage()),
158+
$e
159+
);
160+
}
149161

150162
$map = [];
151163
$processed = 0;
@@ -239,11 +251,22 @@ private function bindWithCustomHandler($table, $entityId, $entityNumber, $fixtur
239251
*
240252
* @param array $map
241253
* @return void
254+
* @throws LocalizedException
242255
*/
243256
private function saveEntities(array &$map)
244257
{
245-
foreach ($map as $table => $data) {
246-
$this->getConnection()->insertMultiple($table, $data);
258+
$this->getConnection()->beginTransaction();
259+
try {
260+
foreach ($map as $table => $data) {
261+
$this->getConnection()->insertMultiple($table, $data);
262+
}
263+
$this->getConnection()->commit();
264+
} catch (\Exception $e) {
265+
$this->getConnection()->rollBack();
266+
throw new LocalizedException(
267+
__('Cannot save entity. Error occurred: %1', $e->getMessage()),
268+
$e
269+
);
247270
}
248271

249272
$map = [];

0 commit comments

Comments
 (0)