Skip to content

Commit

Permalink
Merge pull request #16 from Progi1984/images
Browse files Browse the repository at this point in the history
Added option `images` for the command `prestashop:product-creator`
  • Loading branch information
Progi1984 authored Jan 10, 2025
2 parents 13025f2 + c50f23e commit 0959d54
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"php": ">=7.4",
"fakerphp/faker": "^1.21.0",
"symfony/console": "^4.4",
"mbezhanov/faker-provider-collection": "^2.0"
"mbezhanov/faker-provider-collection": "^2.0",
"bluemmb/faker-picsum-photos-provider": "^2.0"
},
"require-dev": {
"prestashop/php-dev-tools": "^4.3"
Expand Down
7 changes: 7 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ services:
arguments:
$faker: '@Faker\Generator'

PrestaShop\Module\PsFixturesCreator\Creator\ProductImageCreator:
arguments:
$faker: '@Faker\Generator'
$imageCopier : '@prestashop.adapter.import.image_copier'

PrestaShop\Module\PsFixturesCreator\Creator\ProductCreator:
arguments:
$langRepository: '@prestashop.core.admin.lang.repository'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
$productImageCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\ProductImageCreator'
$stockMovementCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator'
$connection: '@doctrine.dbal.default_connection'
$dbPrefix: '%database_prefix%'
Expand Down Expand Up @@ -48,6 +54,7 @@ services:
$attributeCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\AttributeCreator'
$langRepository: '@prestashop.core.admin.lang.repository'
$featureCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\FeatureCreator'
$productImageCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\ProductImageCreator'
$stockMovementCreator: '@PrestaShop\Module\PsFixturesCreator\Creator\StockMovementCreator'
$connection: '@doctrine.dbal.default_connection'
$dbPrefix: '%database_prefix%'
Expand Down
4 changes: 4 additions & 0 deletions src/Command/ProductCreatorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ protected function configure(): void
->addOption('features', null, InputOption::VALUE_OPTIONAL, 'Number of features per product', 2)
->addOption('featureValues', null, InputOption::VALUE_OPTIONAL, 'Number of values per feature', 5)
->addOption('stockMovements', null, InputOption::VALUE_OPTIONAL, 'Number of stock movements per product', 0)
->addOption('images', null, InputOption::VALUE_OPTIONAL, 'Number of images per product', 0)
;
}

Expand All @@ -82,6 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$numberOfFeatures = (int) $input->getOption('features');
$numberOfFeatureValues = (int) $input->getOption('featureValues');
$numberOfStockMovements = (int) $input->getOption('stockMovements');
$numberOfImages = (int) $input->getOption('images');
$productsWithCombinations = (int) $input->getOption('productsWithCombinations');

// create products
Expand All @@ -91,6 +93,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$numberOfFeatures,
$numberOfFeatureValues,
$numberOfStockMovements,
$numberOfImages,
$shopId
);
$output->writeln(sprintf('%s product(s) created', $numberOfProducts));
Expand All @@ -104,6 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$numberOfAttributes,
$numberOfFeatures,
$numberOfFeatureValues,
$numberOfImages,
$shopId
);
$output->writeln(sprintf('%s product(s) with combinations created', $productsWithCombinations));
Expand Down
16 changes: 16 additions & 0 deletions src/Creator/AbstractProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,31 @@
namespace PrestaShop\Module\PsFixturesCreator\Creator;

use Doctrine\DBAL\Connection;
use Faker\Generator as Faker;

abstract class AbstractProductCreator
{
protected FeatureCreator $featureCreator;
protected ProductImageCreator $productImageCreator;
protected StockMovementCreator $stockMovementCreator;
protected Connection $connection;
protected string $dbPrefix;
protected Faker $faker;

public function __construct(
FeatureCreator $featureCreator,
ProductImageCreator $productImageCreator,
StockMovementCreator $stockMovementCreator,
Connection $connection,
Faker $faker,
string $dbPrefix
) {
$this->featureCreator = $featureCreator;
$this->productImageCreator = $productImageCreator;
$this->stockMovementCreator = $stockMovementCreator;
$this->connection = $connection;
$this->dbPrefix = $dbPrefix;
$this->faker = $faker;
}

protected function associateFeatures(int $productId, int $numberOfFeatures, int $numberOfFeatureValues, int $shopId): void
Expand Down Expand Up @@ -87,6 +94,15 @@ protected function associateStockMovements(int $productId, int $numberOfStockMov
$this->stockMovementCreator->generate($numberOfStockMovements, $productId);
}

protected function associateImages(int $productId, array $combinationsId, int $numberOfImages): void
{
if ($numberOfImages <= 0) {
return;
}

$this->productImageCreator->generate($numberOfImages, $productId, $combinationsId);
}

protected function getRandomValues(int $featureId, int $numberOfFeatureValues): array
{
$featureValueIds = $this->connection->createQueryBuilder()
Expand Down
23 changes: 14 additions & 9 deletions src/Creator/ProductCombinationCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query;
use Faker\Generator;
use Faker\Generator as Faker;
use PrestaShop\PrestaShop\Core\CommandBus\CommandBusInterface;
use PrestaShop\PrestaShop\Core\Domain\Product\Combination\Command\GenerateProductCombinationsCommand;
use PrestaShop\PrestaShop\Core\Domain\Product\Combination\ValueObject\CombinationId;
use PrestaShop\PrestaShop\Core\Domain\Product\Command\AddProductCommand;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductId;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductType;
Expand All @@ -25,8 +26,6 @@ class ProductCombinationCreator extends AbstractProductCreator

private AttributeCreator $attributeCreator;

private Generator $faker;

private LangRepository $langRepository;

public function __construct(
Expand All @@ -37,15 +36,15 @@ public function __construct(
FeatureCreator $featureCreator,
Connection $connection,
string $dbPrefix,
Generator $faker,
StockMovementCreator $stockMovementCreator
Faker $faker,
StockMovementCreator $stockMovementCreator,
ProductImageCreator $productImageCreator
) {
parent::__construct($featureCreator, $stockMovementCreator, $connection, $dbPrefix);
parent::__construct($featureCreator, $productImageCreator, $stockMovementCreator, $connection, $faker, $dbPrefix);
$this->entityManager = $entityManager;
$this->commandBus = $commandBus;
$this->attributeCreator = $attributeCreator;
$this->langRepository = $langRepository;
$this->faker = $faker;
}

public function generate(
Expand All @@ -54,6 +53,7 @@ public function generate(
int $attributeValuePerGroupNumber,
int $numberOfFeatures,
int $numberOfFeatureValues,
int $numberOfImages,
int $shopId
): void {
$attributeGroups = $this->getAttributeGroupWithAtLeast($attributeValuePerGroupNumber);
Expand Down Expand Up @@ -82,7 +82,7 @@ public function generate(
$productNames
));

$this->commandBus->handle(new GenerateProductCombinationsCommand(
$combinationsIds = $this->commandBus->handle(new GenerateProductCombinationsCommand(
$newProductId->getValue(),
$combinationAttributes,
$shopId ? ShopConstraint::shop($shopId) : ShopConstraint::allShops()
Expand All @@ -94,14 +94,19 @@ public function generate(
$productNames
));

$this->commandBus->handle(new GenerateProductCombinationsCommand(
$combinationsIds = $this->commandBus->handle(new GenerateProductCombinationsCommand(
$newProductId->getValue(),
$combinationAttributes,
));
} else {
throw new \RuntimeException(sprintf('Version %s not handled to generate combinations', _PS_VERSION_));
}

$combinationsIds = array_map(static function (CombinationId $combination) {
return $combination->getValue();
}, $combinationsIds);

$this->associateImages($newProductId->getValue(), $combinationsIds, $numberOfImages);
$this->associateFeatures($newProductId->getValue(), $numberOfFeatures, $numberOfFeatureValues, $shopId);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/Creator/ProductCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,31 @@
class ProductCreator extends AbstractProductCreator
{
private LangRepository $langRepository;
private Faker $faker;

public function __construct(
LangRepository $langRepository,
FeatureCreator $featureCreator,
Connection $connection,
string $dbPrefix,
Faker $faker,
StockMovementCreator $stockMovementCreator
StockMovementCreator $stockMovementCreator,
ProductImageCreator $productImageCreator
) {
parent::__construct($featureCreator, $stockMovementCreator, $connection, $dbPrefix);
parent::__construct($featureCreator, $productImageCreator, $stockMovementCreator, $connection, $faker, $dbPrefix);
$this->langRepository = $langRepository;
$this->faker = $faker;
}

public function generate(
int $number,
int $numberOfFeatures,
int $numberOfFeatureValues,
int $numberOfStockMovements,
int $numberOfImages,
int $shopId
): void {
for ($i = 0; $i < $number; ++$i) {
$productId = $this->createProduct($shopId);
$this->associateImages($productId, [], $numberOfImages);
$this->associateStockMovements($productId, $numberOfStockMovements);
$this->associateFeatures($productId, $numberOfFeatures, $numberOfFeatureValues, $shopId);
}
Expand Down
81 changes: 81 additions & 0 deletions src/Creator/ProductImageCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace PrestaShop\Module\PsFixturesCreator\Creator;

use Bluemmb\Faker\PicsumPhotosProvider;
use Combination;
use Faker\Generator as Faker;
use Image;
use PrestaShop\PrestaShop\Adapter\Import\ImageCopier;

class ProductImageCreator
{
protected Faker $faker;
protected ImageCopier $imageCopier;

public function __construct(Faker $faker, ImageCopier $imageCopier)
{
$this->faker = $faker;
$this->faker->addProvider(new PicsumPhotosProvider($this->faker));
$this->imageCopier = $imageCopier;
}

public function generate(int $number, int $productId, array $combinationsId = []): void
{
if ($number <= 0) {
return;
}

$imageIds = [];
for ($inc = 0; $inc < $number; ++$inc) {
$imageId = $this->createImage($productId, $inc === 0);
if ($imageId) {
$imageIds[] = $imageId;
}
}

foreach ($combinationsId as $combinationId) {
$rndNumberOfImages = rand(1, $number);
$combinationImages = $rndNumberOfImages == 1
? $imageIds
: array_intersect_key($imageIds, array_flip(array_rand($imageIds, rand(2, $number))));

$this->associateCombinationImages($combinationId, $combinationImages);
}
}

public function createImage(int $productId, bool $isCover): ?int
{
// Download the file
$url = $this->faker->imageUrl();
$tmpFile = tempnam(sys_get_temp_dir(), 'data') . '.png';
file_put_contents($tmpFile, file_get_contents($url));

// Create the object Image
$image = new Image();
$image->id_product = $productId;
$image->position = Image::getHighestPosition($productId) + 1;
$image->cover = $isCover;
$image->add();

if (!$this->imageCopier->copyImg($productId, $image->id, $tmpFile, 'products', true)) {
$image->delete();

unlink($tmpFile);

return null;
}

unlink($tmpFile);

return (int) $image->id;
}

public function associateCombinationImages(int $combinationId, array $imageIds): void
{
$combination = new Combination($combinationId);
$combination->setImages($imageIds);
}
}

0 comments on commit 0959d54

Please sign in to comment.