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

MAGE-1153 Synonym deduplicator #1660

Draft
wants to merge 4 commits into
base: release/3.14.4-dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 3 additions & 115 deletions Console/Command/AbstractReplicaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,123 +2,11 @@

namespace Algolia\AlgoliaSearch\Console\Command;

use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

abstract class AbstractReplicaCommand extends Command
abstract class AbstractReplicaCommand extends AbstractStoreCommand
{
protected const STORE_ARGUMENT = 'store';

protected ?OutputInterface $output = null;
protected ?InputInterface $input = null;

public function __construct(
protected State $state,
protected StoreNameFetcher $storeNameFetcher,
?string $name = null
)
{
parent::__construct($name);
}

abstract protected function getReplicaCommandName(): string;

abstract protected function getCommandDescription(): string;

abstract protected function getStoreArgumentDescription(): string;

abstract protected function getAdditionalDefinition(): array;

/**
* @inheritDoc
*/
protected function configure(): void
{
$definition = [$this->getStoreArgumentDefinition()];
$definition = array_merge($definition, $this->getAdditionalDefinition());

$this->setName($this->getCommandName())
->setDescription($this->getCommandDescription())
->setDefinition($definition);

parent::configure();
}

protected function getStoreArgumentDefinition(): InputArgument {
return new InputArgument(
self::STORE_ARGUMENT,
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
$this->getStoreArgumentDescription()
);
}

public function getCommandName(): string
{
return 'algolia:replicas:' . $this->getReplicaCommandName();
}

protected function setAreaCode(): void
{
try {
$this->state->setAreaCode(Area::AREA_CRONTAB);
} catch (LocalizedException) {
// Area code is already set - nothing to do
}
}

/**
* @param InputInterface $input
* @return int[]
*/
protected function getStoreIds(InputInterface $input): array
{
return (array) $input->getArgument(self::STORE_ARGUMENT);
}

/**
* @param int[] $storeIds
* @return string
*/
protected function getOperationTargetLabel(array $storeIds): string
{
return ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
}

/**
* Generate a CLI operation announcement based on passed store arguments
* @param string $msg Use {{target} in message as a placeholder for inserting the generated target label
* @param int[] $storeIds
* @return string
*/
protected function decorateOperationAnnouncementMessage(string $msg, array $storeIds): string
protected function getCommandPrefix(): string
{
$msg = str_replace('{{target}}', $this->getOperationTargetLabel($storeIds), $msg);
return ($storeIds)
? "<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>'
: "<info>$msg</info>";
return parent::getCommandPrefix() . 'replicas:';
}

protected function confirmOperation(string $okMessage = '', string $cancelMessage = 'Operation cancelled'): bool
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<question>Are you sure wish to proceed? (y/n)</question> ', false);
if (!$helper->ask($this->input, $this->output, $question)) {
if ($cancelMessage) {
$this->output->writeln("<comment>$cancelMessage</comment>");
}
return false;
}

if ($okMessage) {
$this->output->writeln("<comment>$okMessage</comment>");
}
return true;
}
}
128 changes: 128 additions & 0 deletions Console/Command/AbstractStoreCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

namespace Algolia\AlgoliaSearch\Console\Command;

use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
use Magento\Framework\App\Area;
use Magento\Framework\App\State;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

abstract class AbstractStoreCommand extends Command
{
protected const STORE_ARGUMENT = 'store';

protected ?OutputInterface $output = null;
protected ?InputInterface $input = null;

abstract protected function getCommandName(): string;
abstract protected function getCommandDescription(): string;
abstract protected function getAdditionalDefinition(): array;
abstract protected function getStoreArgumentDescription(): string;

public function __construct(
protected State $state,
protected StoreNameFetcher $storeNameFetcher,
?string $name = null
)
{
parent::__construct($name);
}

protected function getCommandPrefix(): string
{
return 'algolia:';
}

protected function getFullCommandName(): string
{
return $this->getCommandPrefix() . $this->getCommandName();
}

/**
* @inheritDoc
*/
protected function configure(): void
{
$definition = [$this->getStoreArgumentDefinition()];
$definition = array_merge($definition, $this->getAdditionalDefinition());

$this->setName($this->getFullCommandName())
->setDescription($this->getCommandDescription())
->setDefinition($definition);

parent::configure();
}

protected function setAreaCode(): void
{
try {
$this->state->setAreaCode(Area::AREA_CRONTAB);
} catch (LocalizedException) {
// Area code is already set - nothing to do
}
}

/**
* @param InputInterface $input
* @return int[]
*/
protected function getStoreIds(InputInterface $input): array
{
return (array) $input->getArgument(self::STORE_ARGUMENT);
}

protected function getStoreArgumentDefinition(): InputArgument {
return new InputArgument(
self::STORE_ARGUMENT,
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
$this->getStoreArgumentDescription()
);
}

/**
* @param int[] $storeIds
* @return string
*/
protected function getOperationTargetLabel(array $storeIds): string
{
return ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
}

/**
* Generate a CLI operation announcement based on passed store arguments
* @param string $msg Use {{target} in message as a placeholder for inserting the generated target label
* @param int[] $storeIds
* @return string
* @throws NoSuchEntityException
*/
protected function decorateOperationAnnouncementMessage(string $msg, array $storeIds): string
{
$msg = str_replace('{{target}}', $this->getOperationTargetLabel($storeIds), $msg);
return ($storeIds)
? "<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>'
: "<info>$msg</info>";
}

protected function confirmOperation(string $okMessage = '', string $cancelMessage = 'Operation cancelled'): bool
{
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<question>Are you sure wish to proceed? (y/n)</question> ', false);
if (!$helper->ask($this->input, $this->output, $question)) {
if ($cancelMessage) {
$this->output->writeln("<comment>$cancelMessage</comment>");
}
return false;
}

if ($okMessage) {
$this->output->writeln("<comment>$okMessage</comment>");
}
return true;
}
}
2 changes: 1 addition & 1 deletion Console/Command/ReplicaDeleteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
parent::__construct($state, $storeNameFetcher, $name);
}

protected function getReplicaCommandName(): string
protected function getCommandName(): string
{
return 'delete';
}
Expand Down
2 changes: 1 addition & 1 deletion Console/Command/ReplicaDisableVirtualCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(
parent::__construct($state, $storeNameFetcher, $name);
}

protected function getReplicaCommandName(): string
protected function getCommandName(): string
{
return 'disable-virtual-replicas';
}
Expand Down
2 changes: 1 addition & 1 deletion Console/Command/ReplicaRebuildCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
parent::__construct($appState, $storeNameFetcher, $name);
}

protected function getReplicaCommandName(): string
protected function getCommandName(): string
{
return 'rebuild';
}
Expand Down
2 changes: 1 addition & 1 deletion Console/Command/ReplicaSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(
parent::__construct($appState, $storeNameFetcher, $name);
}

protected function getReplicaCommandName(): string
protected function getCommandName(): string
{
return 'sync';
}
Expand Down
Loading