Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate features, associate them on product generation #5

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ services:

PrestaShop\Module\PsFixturesCreator\Creator\ProductCreator:
arguments:
$langRepository: '@prestashop.core.admin.lang.repository'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
$connection: '@doctrine.dbal.default_connection'
$dbPrefix: '%database_prefix%'
$faker: '@Faker\Generator'

PrestaShop\Module\PsFixturesCreator\Creator\OrderCreator:
Expand All @@ -31,14 +35,20 @@ services:
$entityManager: '@doctrine.orm.entity_manager'
$langRepository: '@prestashop.core.admin.lang.repository'
$shopRepository: '@prestashop.core.admin.shop.repository'
$faker: '@Faker\Generator'

PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator:
arguments:
$langRepository: '@prestashop.core.admin.lang.repository'

PrestaShop\Module\PsFixturesCreator\Creator\ProductCombinationCreator:
arguments:
$entityManager: '@doctrine.orm.entity_manager'
$commandBus: '@prestashop.core.command_bus'
$attributeCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\AttributeCreator'
$langRepository: '@prestashop.core.admin.lang.repository'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
$connection: '@doctrine.dbal.default_connection'
$dbPrefix: '%database_prefix%'
$faker: '@Faker\Generator'

PrestaShop\Module\PsFixturesCreator\Command\ShopCreatorCommand:
Expand All @@ -48,8 +58,15 @@ services:
$cartCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\CartCreator'
$orderCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\OrderCreator'
$cartRuleCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\CartRuleCreator'
$productCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\ProductCreator'
$attributeCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\AttributeCreator'
$productCombinationCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\ProductCombinationCreator'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
tags:
- { name: 'console.command' }

PrestaShop\Module\PsFixturesCreator\Command\ProductCreatorCommand:
class: PrestaShop\Module\PsFixturesCreator\Command\ProductCreatorCommand
arguments:
$productCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\ProductCreator'
$productCombinationCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\ProductCombinationCreator'
tags:
- { name: 'console.command' }
108 changes: 108 additions & 0 deletions src/Command/ProductCreatorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

namespace PrestaShop\Module\PsFixturesCreator\Command;

use PrestaShop\Module\PsFixturesCreator\Creator\ProductCombinationCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\ProductCreator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* This command is used for appending the hook names in the configuration file.
*/
class ProductCreatorCommand extends Command
{
private ProductCreator $productCreator;

private ProductCombinationCreator $productCombinationCreator;

public function __construct(
ProductCreator $productCreator,
ProductCombinationCreator $productCombinationCreator
) {
parent::__construct(null);

$this->productCreator = $productCreator;
$this->productCombinationCreator = $productCombinationCreator;
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setName('prestashop:product-creator')
->addOption('products', null, InputOption::VALUE_OPTIONAL, 'Number of products to create', 0)
->addOption('productsWithCombinations', null, InputOption::VALUE_OPTIONAL, 'Number of products with combinations to create', 0)
->addOption('shopId', null, InputOption::VALUE_OPTIONAL, 'The shop identifier', 1)
->addOption('shopGroupId', null, InputOption::VALUE_OPTIONAL, 'The shop group identifier', 1)
->addOption('languageId', null, InputOption::VALUE_OPTIONAL, 'The languageId identifier', 1)
->addOption('attributeGroups', null, InputOption::VALUE_OPTIONAL, 'Number of attribute groups per product', 2)
->addOption('attributes', null, InputOption::VALUE_OPTIONAL, 'Number of attributes per attribute group', 5)
->addOption('features', null, InputOption::VALUE_OPTIONAL, 'Number of features per product', 2)
->addOption('featureValues', null, InputOption::VALUE_OPTIONAL, 'Number of values per feature', 5)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
\Context::getContext()->currency = \Currency::getDefaultCurrency();

$numberOfProducts = (int) $input->getOption('products');
$idLang = (int) $input->getOption('languageId');
$idshop = (int) $input->getOption('shopId');
$numberOfAttributeGroups = (int) $input->getOption('attributeGroups');
$numberOfAttributes = (int) $input->getOption('attributes');
$numberOfFeatures = (int) $input->getOption('features');
$numberOfFeatureValues = (int) $input->getOption('featureValues');
$productsWithCombinations = (int) $input->getOption('productsWithCombinations');

// create products
if (!empty($numberOfProducts)) {
$this->productCreator->generate($numberOfProducts, $numberOfFeatures, $numberOfFeatureValues, $idshop);
$output->writeln(sprintf('%s product(s) created', $numberOfProducts));
}

// create product with combinations, if attributes are needed they will be created dynamically
if (!empty($productsWithCombinations)) {
$this->productCombinationCreator->generate(
$productsWithCombinations,
$numberOfAttributeGroups,
$numberOfAttributes,
$numberOfFeatures,
$numberOfFeatureValues,
$idshop
);
$output->writeln(sprintf('%s product(s) with combinations created', $productsWithCombinations));
}

return 0;
}
}
39 changes: 16 additions & 23 deletions src/Command/ShopCreatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
use PrestaShop\Module\PsFixturesCreator\Creator\CartCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\CartRuleCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\CustomerCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\OrderCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\ProductCombinationCreator;
use PrestaShop\Module\PsFixturesCreator\Creator\ProductCreator;
use Shop;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -53,30 +52,26 @@ class ShopCreatorCommand extends Command

private CartRuleCreator $cartRuleCreator;

private ProductCreator $productCreator;

private AttributeCreator $attributeCreator;

private ProductCombinationCreator $productCombinationCreator;
private FeatureCreator $featureCreator;

public function __construct(
CustomerCreator $customerCreator,
CartCreator $cartCreator,
OrderCreator $orderCreator,
CartRuleCreator $cartRuleCreator,
ProductCreator $productCreator,
AttributeCreator $attributeCreator,
ProductCombinationCreator $productCombinationCreator
FeatureCreator $featureCreator
) {
parent::__construct(null);

$this->customerCreator = $customerCreator;
$this->cartCreator = $cartCreator;
$this->orderCreator = $orderCreator;
$this->cartRuleCreator = $cartRuleCreator;
$this->productCreator = $productCreator;
$this->attributeCreator = $attributeCreator;
$this->productCombinationCreator = $productCombinationCreator;
$this->featureCreator = $featureCreator;
}

/**
Expand All @@ -97,6 +92,8 @@ protected function configure(): void
->addOption('languageId', null, InputOption::VALUE_OPTIONAL, 'The languageId identifier', 1)
->addOption('attributeGroups', null, InputOption::VALUE_OPTIONAL, 'Number of attribute groups', 0)
->addOption('attributes', null, InputOption::VALUE_OPTIONAL, 'Number of attributes per attribute group', 10)
->addOption('features', null, InputOption::VALUE_OPTIONAL, 'Number of features', 0)
->addOption('featureValues', null, InputOption::VALUE_OPTIONAL, 'Number of values per feature', 10)
;
}

Expand All @@ -114,6 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$idShopGroup = (int) $input->getOption('shopGroupId');
$numberOfAttributeGroups = (int) $input->getOption('attributeGroups');
$numberOfAttributes = (int) $input->getOption('attributes');
$numberOfFeatures = (int) $input->getOption('features');
$numberOfFeatureValues = (int) $input->getOption('featureValues');
$productsWithCombinations = (int) $input->getOption('productsWithCombinations');

$productIds = $this->getStandardProducts($idLang);
Expand All @@ -130,22 +129,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->writeln(sprintf('%s cart rule(s) created.', $numberOfCartRules));
}

// create products
if (!empty($numberOfProducts)) {
$this->productCreator->generate($numberOfProducts, $idLang);
$output->writeln(sprintf('%s product(s) created', $numberOfProducts));
// create features
if ($numberOfFeatures > 0 && $numberOfFeatureValues > 0) {
$this->featureCreator->generate($numberOfFeatures, $numberOfFeatureValues, $idshop);
$output->writeln(sprintf('Created %s feature(s) with %s different values each.', $numberOfFeatures, $numberOfFeatureValues));
}

// create product with combinations, if attributes are needed they will be created dynamically
if (!empty($productsWithCombinations)) {
$this->productCombinationCreator->generate($productsWithCombinations, $numberOfAttributeGroups, $numberOfAttributes, $idshop);
$output->writeln(sprintf('%s product(s) with combinations created', $productsWithCombinations));
} else {
// If not product with combinations asked, simply create attributes
if ($numberOfAttributeGroups > 0 && $numberOfAttributes > 0) {
$this->attributeCreator->generate($numberOfAttributeGroups, $numberOfAttributes, $idshop);
$output->writeln(sprintf('Created %s attribute group(s) with %s different values each.', $numberOfAttributeGroups, $numberOfAttributes));
}
// Create attributes
if ($numberOfAttributeGroups > 0 && $numberOfAttributes > 0) {
$this->attributeCreator->generate($numberOfAttributeGroups, $numberOfAttributes, $idshop);
$output->writeln(sprintf('Created %s attribute group(s) with %s different values each.', $numberOfAttributeGroups, $numberOfAttributes));
}

// Carts and orders are created last, so they can use new products randomly
Expand Down
122 changes: 122 additions & 0 deletions src/Creator/AbstractProductCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\Module\PsFixturesCreator\Creator;

use Doctrine\DBAL\Connection;

abstract class AbstractProductCreator
{
protected FeatureCreator $featureCreator;
protected Connection $connection;
protected string $dbPrefix;

public function __construct(
FeatureCreator $featureCreator,
Connection $connection,
string $dbPrefix
) {
$this->featureCreator = $featureCreator;
$this->connection = $connection;
$this->dbPrefix = $dbPrefix;
}

protected function associateFeatures(int $productId, int $numberOfFeatures, int $numberOfFeatureValues, int $shopId): void
{
$featureIds = $this->getFeatureWithAtLeast($numberOfFeatureValues);
// Create the missing attribute groups if needed
if (count($featureIds) < $numberOfFeatures) {
$generatedFeatures = $this->featureCreator->generate($numberOfFeatures - count($featureIds), $numberOfFeatureValues, $shopId);
foreach ($generatedFeatures as $feature) {
$featureIds[] = $feature->id;
}
}

foreach ($featureIds as $featureId) {
$randomFeatureValueIds = $this->getRandomValues($featureId, $numberOfFeatureValues);
$this->associateFeatureValues($productId, $featureId, $randomFeatureValueIds);
}
}

protected function associateFeatureValues(int $productId, int $featureId, array $featureValueIds): void
{
foreach ($featureValueIds as $featureValueId) {
$insertedValues = [
'id_product' => $productId,
'id_feature' => $featureId,
'id_feature_value' => $featureValueId,
];
$this->connection->insert($this->dbPrefix . 'feature_product', $insertedValues);
}
}

protected function getRandomValues(int $featureId, int $numberOfFeatureValues): array
{
$featureValueIds = $this->connection->createQueryBuilder()
->select('fv.id_feature_value')
->from($this->dbPrefix . 'feature_value', 'fv')
->where('fv.id_feature = :featureId')
->setParameter('featureId', $featureId)
->execute()
->fetchAllAssociative()
;

$randomKeys = array_rand($featureValueIds, $numberOfFeatureValues);
$randomFeatureValueIds = [];
foreach ($randomKeys as $randomKey) {
$randomFeatureValueIds[] = (int) $featureValueIds[$randomKey]['id_feature_value'];
}

return $randomFeatureValueIds;
}

protected function getFeatureWithAtLeast(int $minimumValuesNumber): array
{
$features = $this->connection->createQueryBuilder()
->select('f.id_feature, COUNT(fv.id_feature_value) AS featureValuesNb')
->from($this->dbPrefix . 'feature', 'f')
->leftJoin(
'f',
$this->dbPrefix . 'feature_value',
'fv',
'f.id_feature = fv.id_feature'
)
->addGroupBy('f.id_feature')
->execute()
->fetchAllAssociative()
;

$featureIds = array_map(static function (array $feature) {
return (int) $feature['id_feature'];
}, array_filter($features, static function (array $feature) use ($minimumValuesNumber) {
return $feature['featureValuesNb'] >= $minimumValuesNumber;
}));

return array_values($featureIds);
}
}
4 changes: 2 additions & 2 deletions src/Creator/AttributeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AttributeCreator
public function __construct(
EntityManagerInterface $entityManager,
LangRepository $langRepository,
ShopRepository $shopRepository,
ShopRepository $shopRepository
) {
$this->entityManager = $entityManager;
$this->langRepository = $langRepository;
Expand Down Expand Up @@ -127,7 +127,7 @@ private function createAttribute(int $attributeNumber, AttributeGroup $attribute
foreach ($languages as $lang) {
$attributeLang = new AttributeLang();
$attributeLang->setLang($lang);
$attributeLang->setName($fakerCategory->getCategoryValue($lang->getLocale()));
$attributeLang->setName($fakerCategory->getCategoryValue($lang->getLocale()) . ' ' . $attributeNumber);
$attribute->addAttributeLang($attributeLang);
$attributeLang->setAttribute($attribute);
$this->entityManager->persist($attributeLang);
Expand Down