Skip to content

Commit

Permalink
Merge pull request #5 from jolelievre/generate-features
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-rolland committed Nov 21, 2023
2 parents 048d0c0 + e37d748 commit aff685e
Show file tree
Hide file tree
Showing 11 changed files with 468 additions and 67 deletions.
20 changes: 9 additions & 11 deletions config.xml
@@ -1,13 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>psfixturescreator</name>
<displayName><![CDATA[psfixturescreator]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Accept payments by bank transfer.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall><![CDATA[Are you sure about removing these details?]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
<limited_countries></limited_countries>
</module>
<name>psfixturescreator</name>
<displayName><![CDATA[PS Fixtures Creator]]></displayName>
<version><![CDATA[1.0.0]]></version>
<description><![CDATA[Module to create fixtures for PrestaShop]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[administration]]></tab>
<is_configurable>0</is_configurable>
<need_instance>0</need_instance>
</module>
23 changes: 20 additions & 3 deletions config/services.yml
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' }
106 changes: 106 additions & 0 deletions src/Command/ProductCreatorCommand.php
@@ -0,0 +1,106 @@
<?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('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');
$shopId = (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, $shopId);
$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,
$shopId
);
$output->writeln(sprintf('%s product(s) with combinations created', $productsWithCombinations));
}

return 0;
}
}
43 changes: 16 additions & 27 deletions src/Command/ShopCreatorCommand.php
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 @@ -90,13 +85,13 @@ protected function configure(): void
->addOption('customers', null, InputOption::VALUE_OPTIONAL, 'Number of customers without order to create', 0)
->addOption('carts', null, InputOption::VALUE_OPTIONAL, 'Number of carts to create', 0)
->addOption('cart-rules', null, InputOption::VALUE_OPTIONAL, 'Number of cart rules to create', 0)
->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', 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 @@ -108,13 +103,13 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$numberOfCustomerWithoutOrder = (int) $input->getOption('customers');
$numberOfCarts = (int) $input->getOption('carts');
$numberOfCartRules = (int) $input->getOption('cart-rules');
$numberOfProducts = (int) $input->getOption('products');
$idLang = (int) $input->getOption('languageId');
$idshop = (int) $input->getOption('shopId');
$idShopGroup = (int) $input->getOption('shopGroupId');
$numberOfAttributeGroups = (int) $input->getOption('attributeGroups');
$numberOfAttributes = (int) $input->getOption('attributes');
$productsWithCombinations = (int) $input->getOption('productsWithCombinations');
$numberOfFeatures = (int) $input->getOption('features');
$numberOfFeatureValues = (int) $input->getOption('featureValues');

$productIds = $this->getStandardProducts($idLang);

Expand All @@ -130,22 +125,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

0 comments on commit aff685e

Please sign in to comment.