Skip to content

Commit 901ce12

Browse files
committed
MAGE-1153 Refactor command classes for extending namespace
1 parent 271a031 commit 901ce12

File tree

6 files changed

+101
-83
lines changed

6 files changed

+101
-83
lines changed

Console/Command/AbstractReplicaCommand.php

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,12 @@
22

33
namespace Algolia\AlgoliaSearch\Console\Command;
44

5-
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
6-
use Magento\Framework\App\Area;
7-
use Magento\Framework\App\State;
8-
use Magento\Framework\Exception\LocalizedException;
9-
use Symfony\Component\Console\Command\Command;
10-
use Symfony\Component\Console\Input\InputArgument;
11-
use Symfony\Component\Console\Input\InputInterface;
12-
use Symfony\Component\Console\Output\OutputInterface;
135
use Symfony\Component\Console\Question\ConfirmationQuestion;
146

15-
abstract class AbstractReplicaCommand extends Command
7+
abstract class AbstractReplicaCommand extends AbstractStoreCommand
168
{
17-
protected const STORE_ARGUMENT = 'store';
18-
19-
protected ?OutputInterface $output = null;
20-
protected ?InputInterface $input = null;
21-
22-
public function __construct(
23-
protected State $state,
24-
protected StoreNameFetcher $storeNameFetcher,
25-
?string $name = null
26-
)
27-
{
28-
parent::__construct($name);
29-
}
30-
31-
abstract protected function getReplicaCommandName(): string;
32-
339
abstract protected function getCommandDescription(): string;
3410

35-
abstract protected function getStoreArgumentDescription(): string;
36-
3711
abstract protected function getAdditionalDefinition(): array;
3812

3913
/**
@@ -44,65 +18,16 @@ protected function configure(): void
4418
$definition = [$this->getStoreArgumentDefinition()];
4519
$definition = array_merge($definition, $this->getAdditionalDefinition());
4620

47-
$this->setName($this->getCommandName())
21+
$this->setName($this->getFullCommandName())
4822
->setDescription($this->getCommandDescription())
4923
->setDefinition($definition);
5024

5125
parent::configure();
5226
}
5327

54-
protected function getStoreArgumentDefinition(): InputArgument {
55-
return new InputArgument(
56-
self::STORE_ARGUMENT,
57-
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
58-
$this->getStoreArgumentDescription()
59-
);
60-
}
61-
62-
public function getCommandName(): string
63-
{
64-
return 'algolia:replicas:' . $this->getReplicaCommandName();
65-
}
66-
67-
protected function setAreaCode(): void
68-
{
69-
try {
70-
$this->state->setAreaCode(Area::AREA_CRONTAB);
71-
} catch (LocalizedException) {
72-
// Area code is already set - nothing to do
73-
}
74-
}
75-
76-
/**
77-
* @param InputInterface $input
78-
* @return int[]
79-
*/
80-
protected function getStoreIds(InputInterface $input): array
81-
{
82-
return (array) $input->getArgument(self::STORE_ARGUMENT);
83-
}
84-
85-
/**
86-
* @param int[] $storeIds
87-
* @return string
88-
*/
89-
protected function getOperationTargetLabel(array $storeIds): string
90-
{
91-
return ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
92-
}
93-
94-
/**
95-
* Generate a CLI operation announcement based on passed store arguments
96-
* @param string $msg Use {{target} in message as a placeholder for inserting the generated target label
97-
* @param int[] $storeIds
98-
* @return string
99-
*/
100-
protected function decorateOperationAnnouncementMessage(string $msg, array $storeIds): string
28+
protected function getCommandPrefix(): string
10129
{
102-
$msg = str_replace('{{target}}', $this->getOperationTargetLabel($storeIds), $msg);
103-
return ($storeIds)
104-
? "<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>'
105-
: "<info>$msg</info>";
30+
return parent::getCommandPrefix() . 'replicas:';
10631
}
10732

10833
protected function confirmOperation(string $okMessage = '', string $cancelMessage = 'Operation cancelled'): bool
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Console\Command;
4+
5+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
6+
use Magento\Framework\App\Area;
7+
use Magento\Framework\App\State;
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
use Symfony\Component\Console\Command\Command;
11+
use Symfony\Component\Console\Input\InputArgument;
12+
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
15+
abstract class AbstractStoreCommand extends Command
16+
{
17+
protected const STORE_ARGUMENT = 'store';
18+
19+
protected ?OutputInterface $output = null;
20+
protected ?InputInterface $input = null;
21+
22+
abstract protected function getStoreArgumentDescription(): string;
23+
abstract protected function getCommandName(): string;
24+
25+
public function __construct(
26+
protected State $state,
27+
protected StoreNameFetcher $storeNameFetcher,
28+
?string $name = null
29+
)
30+
{
31+
parent::__construct($name);
32+
}
33+
34+
protected function getCommandPrefix(): string
35+
{
36+
return 'algolia:';
37+
}
38+
39+
protected function getFullCommandName(): string
40+
{
41+
return $this->getCommandPrefix() . $this->getCommandName();
42+
}
43+
44+
protected function setAreaCode(): void
45+
{
46+
try {
47+
$this->state->setAreaCode(Area::AREA_CRONTAB);
48+
} catch (LocalizedException) {
49+
// Area code is already set - nothing to do
50+
}
51+
}
52+
53+
/**
54+
* @param InputInterface $input
55+
* @return int[]
56+
*/
57+
protected function getStoreIds(InputInterface $input): array
58+
{
59+
return (array) $input->getArgument(self::STORE_ARGUMENT);
60+
}
61+
62+
protected function getStoreArgumentDefinition(): InputArgument {
63+
return new InputArgument(
64+
self::STORE_ARGUMENT,
65+
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
66+
$this->getStoreArgumentDescription()
67+
);
68+
}
69+
70+
/**
71+
* @param int[] $storeIds
72+
* @return string
73+
*/
74+
protected function getOperationTargetLabel(array $storeIds): string
75+
{
76+
return ($storeIds ? count($storeIds) : 'all') . ' store' . (!$storeIds || count($storeIds) > 1 ? 's' : '');
77+
}
78+
79+
/**
80+
* Generate a CLI operation announcement based on passed store arguments
81+
* @param string $msg Use {{target} in message as a placeholder for inserting the generated target label
82+
* @param int[] $storeIds
83+
* @return string
84+
* @throws NoSuchEntityException
85+
*/
86+
protected function decorateOperationAnnouncementMessage(string $msg, array $storeIds): string
87+
{
88+
$msg = str_replace('{{target}}', $this->getOperationTargetLabel($storeIds), $msg);
89+
return ($storeIds)
90+
? "<info>$msg: " . join(", ", $this->storeNameFetcher->getStoreNames($storeIds)) . '</info>'
91+
: "<info>$msg</info>";
92+
}
93+
}

Console/Command/ReplicaDeleteCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(
3333
parent::__construct($state, $storeNameFetcher, $name);
3434
}
3535

36-
protected function getReplicaCommandName(): string
36+
protected function getCommandName(): string
3737
{
3838
return 'delete';
3939
}

Console/Command/ReplicaDisableVirtualCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct(
4242
parent::__construct($state, $storeNameFetcher, $name);
4343
}
4444

45-
protected function getReplicaCommandName(): string
45+
protected function getCommandName(): string
4646
{
4747
return 'disable-virtual-replicas';
4848
}

Console/Command/ReplicaRebuildCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(
3838
parent::__construct($appState, $storeNameFetcher, $name);
3939
}
4040

41-
protected function getReplicaCommandName(): string
41+
protected function getCommandName(): string
4242
{
4343
return 'rebuild';
4444
}

Console/Command/ReplicaSyncCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
parent::__construct($appState, $storeNameFetcher, $name);
3636
}
3737

38-
protected function getReplicaCommandName(): string
38+
protected function getCommandName(): string
3939
{
4040
return 'sync';
4141
}

0 commit comments

Comments
 (0)