|
2 | 2 |
|
3 | 3 | namespace Algolia\AlgoliaSearch\Console\Command;
|
4 | 4 |
|
5 |
| -class ReplicaRebuildCommand extends AbstractReplicaCommand |
| 5 | +use Algolia\AlgoliaSearch\Api\Console\ReplicaDeleteCommandInterface; |
| 6 | +use Algolia\AlgoliaSearch\Api\Console\ReplicaSyncCommandInterface; |
| 7 | +use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface; |
| 8 | +use Algolia\AlgoliaSearch\Console\Traits\ReplicaDeleteCommandTrait; |
| 9 | +use Algolia\AlgoliaSearch\Console\Traits\ReplicaSyncCommandTrait; |
| 10 | +use Algolia\AlgoliaSearch\Exception\ReplicaLimitExceededException; |
| 11 | +use Algolia\AlgoliaSearch\Exceptions\BadRequestException; |
| 12 | +use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper; |
| 13 | +use Algolia\AlgoliaSearch\Service\StoreNameFetcher; |
| 14 | +use Magento\Framework\App\State; |
| 15 | +use Magento\Framework\Console\Cli; |
| 16 | +use Symfony\Component\Console\Input\InputInterface; |
| 17 | +use Symfony\Component\Console\Output\OutputInterface; |
| 18 | + |
| 19 | +class ReplicaRebuildCommand |
| 20 | + extends AbstractReplicaCommand |
| 21 | + implements ReplicaSyncCommandInterface, ReplicaDeleteCommandInterface |
6 | 22 | {
|
| 23 | + use ReplicaSyncCommandTrait; |
| 24 | + use ReplicaDeleteCommandTrait; |
| 25 | + |
| 26 | + public function __construct( |
| 27 | + protected ReplicaManagerInterface $replicaManager, |
| 28 | + protected StoreNameFetcher $storeNameFetcher, |
| 29 | + protected ProductHelper $productHelper, |
| 30 | + State $state, |
| 31 | + ?string $name = null |
| 32 | + ) |
| 33 | + { |
| 34 | + parent::__construct($state, $name); |
| 35 | + } |
| 36 | + |
7 | 37 | protected function getReplicaCommandName(): string
|
8 | 38 | {
|
9 | 39 | return 'rebuild';
|
10 | 40 | }
|
11 | 41 |
|
12 |
| - protected function getCommandDescription(): string { |
| 42 | + protected function getCommandDescription(): string |
| 43 | + { |
13 | 44 | return 'Rebuild replica configuration for Magento sorting attributes';
|
14 | 45 | }
|
15 | 46 |
|
16 | 47 | protected function getStoreArgumentDescription(): string
|
17 | 48 | {
|
18 |
| - return 'ID(s) for store(s) to rebuild replicas'; |
| 49 | + return 'ID(s) for store(s) to rebuild replicas (optional), if not specified all store replicas will be rebuilt'; |
19 | 50 | }
|
20 | 51 |
|
21 | 52 | protected function getAdditionalDefinition(): array
|
22 | 53 | {
|
23 |
| - return []; |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 58 | + { |
| 59 | + $this->output = $output; |
| 60 | + $this->setAreaCode(); |
| 61 | + |
| 62 | + $storeIds = $this->getStoreIds($input); |
| 63 | + |
| 64 | + $msg = 'Rebuilding replicas for ' . ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : ''); |
| 65 | + if ($storeIds) { |
| 66 | + $output->writeln("<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>'); |
| 67 | + } else { |
| 68 | + $output->writeln("<info>$msg</info>"); |
| 69 | + } |
| 70 | + |
| 71 | + $this->deleteReplicas($storeIds); |
| 72 | + try { |
| 73 | + $this->syncReplicas($storeIds); |
| 74 | + } catch (ReplicaLimitExceededException $e) { |
| 75 | + $this->output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 76 | + $this->output->writeln('<comment>Reduce the number of sorting attributes that have enabled virtual replicas and try again.</comment>'); |
| 77 | + return CLI::RETURN_FAILURE; |
| 78 | + } catch (BadRequestException $e) { |
| 79 | + $this->output->writeln('<error>' . $e->getMessage() . '</error>'); |
| 80 | + if ($storeIds) { |
| 81 | + $this->output->writeln('<comment>Your Algolia application may contain cris-crossed replicas. Try running "algolia:replicas:rebuild" for all stores to correct this.'); |
| 82 | + } |
| 83 | + return CLI::RETURN_FAILURE; |
| 84 | + } |
| 85 | + |
| 86 | + return Cli::RETURN_SUCCESS; |
24 | 87 | }
|
25 | 88 |
|
26 | 89 | }
|
0 commit comments