Skip to content

Commit c1fd8ab

Browse files
committed
MAGE-848 Refactor shared CLI info messages
1 parent 1046a2f commit c1fd8ab

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

Console/Command/AbstractReplicaCommand.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Algolia\AlgoliaSearch\Console\Command;
44

5+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
56
use Magento\Framework\App\Area;
67
use Magento\Framework\App\State;
78
use Magento\Framework\Exception\LocalizedException;
@@ -18,8 +19,9 @@ abstract class AbstractReplicaCommand extends Command
1819
protected ?InputInterface $input = null;
1920

2021
public function __construct(
21-
protected State $state,
22-
?string $name = null
22+
protected State $state,
23+
protected StoreNameFetcher $storeNameFetcher,
24+
?string $name = null
2325
)
2426
{
2527
parent::__construct($name);
@@ -70,9 +72,35 @@ protected function setAreaCode(): void
7072
}
7173
}
7274

75+
/**
76+
* @param InputInterface $input
77+
* @return int[]
78+
*/
7379
protected function getStoreIds(InputInterface $input): array
7480
{
7581
return (array) $input->getArgument(self::STORE_ARGUMENT);
7682
}
7783

84+
/**
85+
* @param int[] $storeIds
86+
* @return string
87+
*/
88+
protected function getOperationTargetLabel(array $storeIds): string
89+
{
90+
return ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
91+
}
92+
93+
/**
94+
* Generate a CLI operation announcement based on passed store arguments
95+
* @param string $msg Use {{target} in message as a placeholder for inserting the generated target label
96+
* @param int[] $storeIds
97+
* @return string
98+
*/
99+
protected function decorateOperationAnnouncementMessage(string $msg, array $storeIds): string
100+
{
101+
$msg = str_replace('{{target}}', $this->getOperationTargetLabel($storeIds), $msg);
102+
return ($storeIds)
103+
? "<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>'
104+
: "<info>$msg</info>";
105+
}
78106
}

Console/Command/ReplicaDeleteCommand.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ class ReplicaDeleteCommand extends AbstractReplicaCommand implements ReplicaDele
2525
public function __construct(
2626
protected ReplicaManagerInterface $replicaManager,
2727
protected StoreManagerInterface $storeManager,
28-
protected StoreNameFetcher $storeNameFetcher,
29-
protected State $state,
28+
State $state,
29+
StoreNameFetcher $storeNameFetcher,
3030
?string $name = null
3131
)
3232
{
33-
parent::__construct($state, $name);
33+
parent::__construct($state, $storeNameFetcher, $name);
3434
}
3535

3636
protected function getReplicaCommandName(): string
@@ -68,12 +68,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6868
$storeIds = $this->getStoreIds($input);
6969
$unused = $input->getOption(self::UNUSED_OPTION);
7070

71-
$msg = 'Deleting' . ($unused ? ' unused ' : ' ') . 'replicas for ' . ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
72-
if ($storeIds) {
73-
$output->writeln("<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>');
74-
} else {
75-
$output->writeln("<info>$msg</info>");
76-
}
71+
$output->writeln(
72+
$this->decorateOperationAnnouncementMessage(
73+
'Deleting' . ($unused ? ' unused ' : ' ') . 'replicas for {{target}}',
74+
$storeIds
75+
)
76+
);
7777

7878
if ($unused) {
7979
$unusedReplicas = $this->getUnusedReplicas($storeIds);

Console/Command/ReplicaRebuildCommand.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
1414
use Magento\Framework\App\State;
1515
use Magento\Framework\Console\Cli;
16+
use Magento\Store\Model\StoreManagerInterface;
1617
use Symfony\Component\Console\Input\InputInterface;
1718
use Symfony\Component\Console\Output\OutputInterface;
1819

@@ -24,14 +25,15 @@ class ReplicaRebuildCommand
2425
use ReplicaDeleteCommandTrait;
2526

2627
public function __construct(
27-
protected ReplicaManagerInterface $replicaManager,
28-
protected StoreNameFetcher $storeNameFetcher,
2928
protected ProductHelper $productHelper,
29+
protected ReplicaManagerInterface $replicaManager,
30+
protected StoreManagerInterface $storeManager,
3031
State $state,
32+
StoreNameFetcher $storeNameFetcher,
3133
?string $name = null
3234
)
3335
{
34-
parent::__construct($state, $name);
36+
parent::__construct($state, $storeNameFetcher, $name);
3537
}
3638

3739
protected function getReplicaCommandName(): string
@@ -61,12 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6163

6264
$storeIds = $this->getStoreIds($input);
6365

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-
}
66+
$output->writeln($this->decorateOperationAnnouncementMessage('Rebuilding replicas for {{target}}', $storeIds));
7067

7168
$this->deleteReplicas($storeIds);
7269
try {

Console/Command/ReplicaSyncCommand.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Algolia\AlgoliaSearch\Api\Console\ReplicaSyncCommandInterface;
66
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
7+
use Algolia\AlgoliaSearch\Console\Traits\ReplicaSyncCommandTrait;
78
use Algolia\AlgoliaSearch\Exception\ReplicaLimitExceededException;
89
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
910
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
@@ -17,7 +18,6 @@
1718
use Magento\Store\Model\StoreManagerInterface;
1819
use Symfony\Component\Console\Input\InputInterface;
1920
use Symfony\Component\Console\Output\OutputInterface;
20-
use Algolia\AlgoliaSearch\Console\Traits\ReplicaSyncCommandTrait;
2121

2222
class ReplicaSyncCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface
2323
{
@@ -28,27 +28,27 @@ public function __construct(
2828
protected ProductHelper $productHelper,
2929
protected ReplicaManagerInterface $replicaManager,
3030
protected StoreManagerInterface $storeManager,
31-
protected StoreNameFetcher $storeNameFetcher,
3231
State $state,
32+
StoreNameFetcher $storeNameFetcher,
3333
?string $name = null
3434
)
3535
{
36-
parent::__construct($state, $name);
36+
parent::__construct($state, $storeNameFetcher, $name);
3737
}
3838

3939
protected function getReplicaCommandName(): string
4040
{
4141
return 'sync';
4242
}
4343

44-
protected function getStoreArgumentDescription(): string
44+
protected function getCommandDescription(): string
4545
{
46-
return 'ID(s) for store(s) to be synced with Algolia (optional), if not specified all stores will be synced';
46+
return 'Sync configured sorting attributes in Magento to Algolia replica indices';
4747
}
4848

49-
protected function getCommandDescription(): string
49+
protected function getStoreArgumentDescription(): string
5050
{
51-
return 'Sync configured sorting attributes in Magento to Algolia replica indices';
51+
return 'ID(s) for store(s) to be synced with Algolia (optional), if not specified all stores will be synced';
5252
}
5353

5454
protected function getAdditionalDefinition(): array
@@ -73,12 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7373

7474
$storeIds = $this->getStoreIds($input);
7575

76-
$msg = 'Syncing replicas for ' . ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
77-
if ($storeIds) {
78-
$output->writeln("<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>');
79-
} else {
80-
$output->writeln("<info>$msg</info>");
81-
}
76+
$output->writeln($this->decorateOperationAnnouncementMessage('Syncing replicas for {{target}}', $storeIds));
8277

8378
try {
8479
$this->syncReplicas($storeIds);

0 commit comments

Comments
 (0)