Skip to content

Commit 1996233

Browse files
committed
MAGE-938 Refactor replica command classes for code reuse
1 parent 6029971 commit 1996233

File tree

3 files changed

+123
-61
lines changed

3 files changed

+123
-61
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Console\Command;
4+
5+
use Magento\Framework\App\Area;
6+
use Magento\Framework\App\State;
7+
use Magento\Framework\Exception\LocalizedException;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
11+
abstract class AbstractReplicaCommand extends Command
12+
{
13+
protected const STORE_ARGUMENT = 'store';
14+
15+
public function __construct(
16+
protected State $state,
17+
?string $name = null
18+
)
19+
{
20+
parent::__construct($name);
21+
}
22+
23+
abstract protected function getReplicaCommandName(): string;
24+
25+
abstract protected function getCommandDescription(): string;
26+
27+
abstract protected function getStoreArgumentDescription(): string;
28+
29+
abstract protected function getAdditionalDefinition(): array;
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
protected function configure(): void
35+
{
36+
$definition = [$this->getStoreArgumentDefinition()];
37+
$definition = array_merge($definition, $this->getAdditionalDefinition());
38+
39+
$this->setName($this->getCommandName())
40+
->setDescription($this->getCommandDescription())
41+
->setDefinition($definition);
42+
43+
parent::configure();
44+
}
45+
46+
protected function getStoreArgumentDefinition(): InputArgument {
47+
return new InputArgument(
48+
self::STORE_ARGUMENT,
49+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
50+
$this->getStoreArgumentDescription()
51+
);
52+
}
53+
54+
public function getCommandName(): string
55+
{
56+
return 'algolia:replicas:' . $this->getReplicaCommandName();
57+
}
58+
59+
protected function setAreaCode(): void
60+
{
61+
try {
62+
$this->state->setAreaCode(Area::AREA_CRONTAB);
63+
} catch (LocalizedException) {
64+
// Area code is already set - nothing to do
65+
}
66+
}
67+
68+
}

Console/Command/ReplicaDeleteCommand.php

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
77
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
88
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
9+
use Magento\Framework\App\State;
910
use Magento\Framework\Console\Cli;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Store\Model\StoreManagerInterface;
13-
use Symfony\Component\Console\Command\Command;
14-
use Symfony\Component\Console\Input\InputArgument;
1514
use Symfony\Component\Console\Input\InputInterface;
1615
use Symfony\Component\Console\Input\InputOption;
1716
use Symfony\Component\Console\Output\OutputInterface;
1817
use Symfony\Component\Console\Question\ConfirmationQuestion;
1918

20-
class ReplicaDeleteCommand extends Command
19+
class ReplicaDeleteCommand extends AbstractReplicaCommand
2120
{
2221
protected const STORE_ARGUMENT = 'store';
2322

@@ -31,36 +30,41 @@ public function __construct(
3130
protected ReplicaManagerInterface $replicaManager,
3231
protected StoreManagerInterface $storeManager,
3332
protected StoreNameFetcher $storeNameFetcher,
33+
protected State $state,
3434
?string $name = null
3535
)
3636
{
37-
parent::__construct($name);
37+
parent::__construct($state, $name);
3838
}
3939

40-
/**
41-
* @inheritDoc
42-
*/
43-
protected function configure(): void
40+
protected function getReplicaCommandName(): string
41+
{
42+
return 'delete';
43+
}
44+
45+
protected function getCommandDescription(): string
46+
{
47+
return 'Delete associated replica indices in Algolia';
48+
}
49+
50+
protected function getStoreArgumentDescription(): string
51+
{
52+
return 'ID(s) for store(s) to delete replicas in Algolia (optional), if not specified, replicas for all stores will be deleted';
53+
}
54+
55+
protected function getAdditionalDefinition(): array
4456
{
45-
$this->setName('algolia:replicas:delete')
46-
->setDescription('Delete associated replica indices in Algolia')
47-
->setDefinition([
48-
new InputArgument(
49-
self::STORE_ARGUMENT,
50-
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
51-
'ID(s) for store(s) to delete replicas in Algolia (optional), if not specified, replicas for all stores will be deleted'
52-
),
53-
new InputOption(
54-
self::UNUSED_OPTION,
55-
'-' . self::UNUSED_OPTION_SHORTCUT,
56-
InputOption::VALUE_NONE,
57-
'Delete unused replicas only'
58-
)
59-
]);
60-
61-
parent::configure();
57+
return [
58+
new InputOption(
59+
self::UNUSED_OPTION,
60+
'-' . self::UNUSED_OPTION_SHORTCUT,
61+
InputOption::VALUE_NONE,
62+
'Delete unused replicas only'
63+
)
64+
];
6265
}
6366

67+
6468
protected function execute(InputInterface $input, OutputInterface $output): int
6569
{
6670
$this->output = $output;
@@ -69,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6973
$storeIds = (array) $input->getArgument(self::STORE_ARGUMENT);
7074
$unused = $input->getOption(self::UNUSED_OPTION);
7175

72-
$msg = 'Deleting' . ($unused ? ' unused ': ' ') . 'replicas for ' . ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
76+
$msg = 'Deleting' . ($unused ? ' unused ' : ' ') . 'replicas for ' . ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
7377
if ($storeIds) {
7478
$output->writeln("<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>');
7579
} else {
@@ -103,7 +107,7 @@ protected function getUnusedReplicas(array $storeIds): array
103107
{
104108
return array_reduce(
105109
$storeIds,
106-
function($allUnused, $storeId) {
110+
function ($allUnused, $storeId) {
107111
$unused = [];
108112
try {
109113
$unused = $this->replicaManager->getUnusedReplicaIndices($storeId);
@@ -162,7 +166,7 @@ protected function deleteReplicas(array $storeIds = [], bool $unused = false): v
162166
*/
163167
protected function deleteReplicasForStore(int $storeId, bool $unused = false): void
164168
{
165-
$this->output->writeln('<info>Deleting' . ($unused ? ' unused ': ' ') . 'replicas for ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
169+
$this->output->writeln('<info>Deleting' . ($unused ? ' unused ' : ' ') . 'replicas for ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
166170
$this->replicaManager->deleteReplicasFromAlgolia($storeId, $unused);
167171
}
168172

Console/Command/ReplicaSyncCommand.php

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,50 @@
99
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
1010
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
1111
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
12-
use Magento\Framework\App\Area;
1312
use Magento\Framework\App\State;
1413
use Magento\Framework\Console\Cli;
1514
use Magento\Framework\Exception\LocalizedException;
1615
use Magento\Framework\Exception\NoSuchEntityException;
1716
use Magento\Store\Model\StoreManagerInterface;
18-
use Symfony\Component\Console\Command\Command;
19-
use Symfony\Component\Console\Input\InputArgument;
2017
use Symfony\Component\Console\Input\InputInterface;
2118
use Symfony\Component\Console\Output\OutputInterface;
2219

23-
class ReplicaSyncCommand extends Command
20+
class ReplicaSyncCommand extends AbstractReplicaCommand
2421
{
25-
protected const STORE_ARGUMENT = 'store';
2622

2723
protected ?OutputInterface $output = null;
2824

2925
public function __construct(
30-
protected State $state,
26+
3127
protected ProductHelper $productHelper,
3228
protected ReplicaManagerInterface $replicaManager,
3329
protected StoreManagerInterface $storeManager,
3430
protected StoreNameFetcher $storeNameFetcher,
31+
State $state,
3532
?string $name = null
3633
)
3734
{
38-
parent::__construct($name);
35+
parent::__construct($state, $name);
3936
}
4037

41-
/**
42-
* @inheritDoc
43-
*/
44-
protected function configure(): void
38+
protected function getReplicaCommandName(): string
39+
{
40+
return 'sync';
41+
}
42+
43+
protected function getStoreArgumentDescription(): string
44+
{
45+
return 'ID(s) for store(s) to be synced with Algolia (optional), if not specified all stores will be synced';
46+
}
47+
48+
protected function getCommandDescription(): string
4549
{
46-
$this->setName('algolia:replicas:sync')
47-
->setDescription('Sync configured sorting attributes in Magento to Algolia replica indices')
48-
->setDefinition([
49-
new InputArgument(
50-
self::STORE_ARGUMENT,
51-
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
52-
'ID(s) for store(s) to be synced with Algolia (optional), if not specified all stores will be synced'
53-
)
54-
]);
55-
56-
parent::configure();
50+
return 'Sync configured sorting attributes in Magento to Algolia replica indices';
51+
}
52+
53+
protected function getAdditionalDefinition(): array
54+
{
55+
return [];
5756
}
5857

5958
/**
@@ -96,14 +95,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9695
return Cli::RETURN_SUCCESS;
9796
}
9897

99-
protected function setAreaCode(): void {
100-
try {
101-
$this->state->setAreaCode(Area::AREA_CRONTAB);
102-
} catch (LocalizedException) {
103-
// Area code is already set - nothing to do
104-
}
105-
}
106-
10798
/**
10899
* @param int[] $storeIds
109100
* @return void
@@ -119,7 +110,7 @@ protected function syncReplicas(array $storeIds = []): void
119110
$this->syncReplicasForStore($storeId);
120111
}
121112
} else {
122-
$this->syncReplicasForAllStores();
113+
$this->syncReplicasForAllStores();
123114
}
124115
}
125116

@@ -134,8 +125,7 @@ protected function syncReplicasForStore(int $storeId): void
134125
$this->output->writeln('<info>Syncing ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
135126
try {
136127
$this->replicaManager->syncReplicasToAlgolia($storeId, $this->productHelper->getIndexSettings($storeId));
137-
}
138-
catch (BadRequestException $e) {
128+
} catch (BadRequestException $e) {
139129
$this->output->writeln('<error>Failed syncing replicas for store "' . $this->storeNameFetcher->getStoreName($storeId) . '": ' . $e->getMessage() . '</error>');
140130
throw $e;
141131
}

0 commit comments

Comments
 (0)