Skip to content

Commit

Permalink
[framework] added optional option to export data to Elasticsearch onl…
Browse files Browse the repository at this point in the history
…y for the specified domain (#2780)
  • Loading branch information
TomasLudvik authored Sep 13, 2023
1 parent 4c86a91 commit c59b390
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
30 changes: 26 additions & 4 deletions src/Command/Elasticsearch/AbstractElasticsearchIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

abstract class AbstractElasticsearchIndexCommand extends Command
{
private const ARGUMENT_INDEX_NAME = 'name';
protected const OPTION_DOMAIN_ID = 'domainId';

/**
* @param \Shopsys\FrameworkBundle\Component\Elasticsearch\IndexRegistry $indexRegistry
Expand All @@ -43,6 +45,12 @@ protected function configure(): void
InputArgument::OPTIONAL,
$this->getArgumentNameDescription(),
)
->addOption(
static::OPTION_DOMAIN_ID,
null,
InputOption::VALUE_REQUIRED,
'Limit command to only one domain.',
)
->setDescription($this->getCommandDescription());
}

Expand All @@ -55,10 +63,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$symfonyStyleIo = new SymfonyStyle($input, $output);
$indexName = $input->getArgument(self::ARGUMENT_INDEX_NAME);
$domainId = $input->getOption(static::OPTION_DOMAIN_ID) !== null ? (int)$input->getOption(static::OPTION_DOMAIN_ID) : null;
$output->writeln($this->getActionStartedMessage());

foreach ($this->getAffectedIndexes($indexName) as $index) {
$this->executeForIndex($output, $index);
$this->executeForIndex($output, $index, $domainId);
}

$symfonyStyleIo->success($this->getActionFinishedMessage());
Expand All @@ -82,14 +91,27 @@ private function getAffectedIndexes(?string $indexName): array
/**
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Shopsys\FrameworkBundle\Component\Elasticsearch\AbstractIndex $index
* @param int|null $domainId
*/
protected function executeForIndex(OutputInterface $output, AbstractIndex $index): void
{
foreach ($this->domain->getAll() as $domainConfig) {
protected function executeForIndex(
OutputInterface $output,
AbstractIndex $index,
?int $domainId = null,
): void {
if ($domainId !== null) {
$domainConfig = $this->domain->getDomainConfigById($domainId);

$this->executeCommand(
$this->indexDefinitionLoader->getIndexDefinition($index::getName(), $domainConfig->getId()),
$output,
);
} else {
foreach ($this->domain->getAll() as $domainConfig) {
$this->executeCommand(
$this->indexDefinitionLoader->getIndexDefinition($index::getName(), $domainConfig->getId()),
$output,
);
}
}
}

Expand Down
27 changes: 21 additions & 6 deletions src/Command/Elasticsearch/ElasticsearchDataExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Shopsys\FrameworkBundle\Component\Elasticsearch\IndexExportedEvent;
use Shopsys\FrameworkBundle\Component\Elasticsearch\IndexFacade;
use Shopsys\FrameworkBundle\Component\Elasticsearch\IndexRegistry;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -39,17 +40,31 @@ public function __construct(
parent::__construct($indexRegistry, $indexFacade, $indexDefinitionLoader, $domain);
}

protected function configure(): void
{
parent::configure();

$this->addOption(
self::OPTION_DOMAIN_ID,
null,
InputOption::VALUE_REQUIRED,
'Limit command to only one domain. Products will not be marked as exported.',
);
}

/**
* {@inheritdoc}
*/
protected function executeForIndex(OutputInterface $output, AbstractIndex $index): void
protected function executeForIndex(OutputInterface $output, AbstractIndex $index, ?int $domainId = null): void
{
parent::executeForIndex($output, $index);
parent::executeForIndex($output, $index, $domainId);

$this->eventDispatcher->dispatch(
new IndexExportedEvent($index),
IndexExportedEvent::INDEX_EXPORTED,
);
if ($domainId === null) {
$this->eventDispatcher->dispatch(
new IndexExportedEvent($index),
IndexExportedEvent::INDEX_EXPORTED,
);
}
}

/**
Expand Down

0 comments on commit c59b390

Please sign in to comment.