Skip to content

Commit 8a09601

Browse files
committed
MAGE-938 Introduce interfaces and traits to console command classes
1 parent 4120a4e commit 8a09601

7 files changed

+146
-99
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Api\Console;
4+
5+
interface ReplicaDeleteCommandInterface
6+
{
7+
public function deleteReplicas(array $storeIds = [], bool $unused = false): void;
8+
public function deleteReplicasForStore(int $storeId, bool $unused = false): void;
9+
public function deleteReplicasForAllStores(bool $unused = false): void;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Api\Console;
4+
5+
interface ReplicaSyncCommandInterface
6+
{
7+
public function syncReplicas(array $storeIds = []): void;
8+
public function syncReplicasForStore(int $storeId): void;
9+
public function syncReplicasForAllStores(): void;
10+
}

Console/Command/AbstractReplicaCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
use Symfony\Component\Console\Command\Command;
99
use Symfony\Component\Console\Input\InputArgument;
1010
use Symfony\Component\Console\Input\InputInterface;
11+
use Symfony\Component\Console\Output\OutputInterface;
1112

1213
abstract class AbstractReplicaCommand extends Command
1314
{
1415
protected const STORE_ARGUMENT = 'store';
1516

17+
protected ?OutputInterface $output = null;
18+
protected ?InputInterface $input = null;
19+
1620
public function __construct(
1721
protected State $state,
1822
?string $name = null

Console/Command/ReplicaDeleteCommand.php

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@
22

33
namespace Algolia\AlgoliaSearch\Console\Command;
44

5+
use Algolia\AlgoliaSearch\Api\Console\ReplicaDeleteCommandInterface;
56
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6-
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
7+
use Algolia\AlgoliaSearch\Console\Traits\ReplicaDeleteCommandTrait;
78
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
89
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
910
use Magento\Framework\App\State;
1011
use Magento\Framework\Console\Cli;
11-
use Magento\Framework\Exception\LocalizedException;
12-
use Magento\Framework\Exception\NoSuchEntityException;
1312
use Magento\Store\Model\StoreManagerInterface;
1413
use Symfony\Component\Console\Input\InputInterface;
1514
use Symfony\Component\Console\Input\InputOption;
1615
use Symfony\Component\Console\Output\OutputInterface;
1716
use Symfony\Component\Console\Question\ConfirmationQuestion;
1817

19-
class ReplicaDeleteCommand extends AbstractReplicaCommand
18+
class ReplicaDeleteCommand extends AbstractReplicaCommand implements ReplicaDeleteCommandInterface
2019
{
20+
use ReplicaDeleteCommandTrait;
21+
2122
protected const UNUSED_OPTION = 'unused';
2223
protected const UNUSED_OPTION_SHORTCUT = 'u';
2324

24-
protected ?OutputInterface $output = null;
25-
protected ?InputInterface $input = null;
26-
2725
public function __construct(
2826
protected ReplicaManagerInterface $replicaManager,
2927
protected StoreManagerInterface $storeManager,
@@ -62,7 +60,6 @@ protected function getAdditionalDefinition(): array
6260
];
6361
}
6462

65-
6663
protected function execute(InputInterface $input, OutputInterface $output): int
6764
{
6865
$this->output = $output;
@@ -141,43 +138,4 @@ protected function confirmDeleteUnused(array $unusedReplicas): bool
141138
return true;
142139
}
143140

144-
/**
145-
* @throws NoSuchEntityException
146-
* @throws AlgoliaException
147-
* @throws LocalizedException
148-
*/
149-
protected function deleteReplicas(array $storeIds = [], bool $unused = false): void
150-
{
151-
if (count($storeIds)) {
152-
foreach ($storeIds as $storeId) {
153-
$this->deleteReplicasForStore($storeId, $unused);
154-
}
155-
} else {
156-
$this->deleteReplicasForAllStores($unused);
157-
}
158-
}
159-
160-
/**
161-
* @throws NoSuchEntityException
162-
* @throws LocalizedException
163-
* @throws AlgoliaException
164-
*/
165-
protected function deleteReplicasForStore(int $storeId, bool $unused = false): void
166-
{
167-
$this->output->writeln('<info>Deleting' . ($unused ? ' unused ' : ' ') . 'replicas for ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
168-
$this->replicaManager->deleteReplicasFromAlgolia($storeId, $unused);
169-
}
170-
171-
/**
172-
* @throws NoSuchEntityException
173-
* @throws LocalizedException
174-
* @throws AlgoliaException
175-
*/
176-
protected function deleteReplicasForAllStores(bool $unused = false): void
177-
{
178-
$storeIds = array_keys($this->storeManager->getStores());
179-
foreach ($storeIds as $storeId) {
180-
$this->deleteReplicasForStore($storeId, $unused);
181-
}
182-
}
183141
}

Console/Command/ReplicaSyncCommand.php

Lines changed: 4 additions & 52 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\Api\Console\ReplicaSyncCommandInterface;
56
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
67
use Algolia\AlgoliaSearch\Exception\ReplicaLimitExceededException;
78
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
@@ -16,11 +17,11 @@
1617
use Magento\Store\Model\StoreManagerInterface;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Output\OutputInterface;
20+
use Algolia\AlgoliaSearch\Console\Traits\ReplicaSyncCommandTrait;
1921

20-
class ReplicaSyncCommand extends AbstractReplicaCommand
22+
class ReplicaSyncCommand extends AbstractReplicaCommand implements ReplicaSyncCommandInterface
2123
{
22-
23-
protected ?OutputInterface $output = null;
24+
use ReplicaSyncCommandTrait;
2425

2526
public function __construct(
2627

@@ -94,53 +95,4 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9495
return Cli::RETURN_SUCCESS;
9596
}
9697

97-
/**
98-
* @param int[] $storeIds
99-
* @return void
100-
* @throws AlgoliaException
101-
* @throws ExceededRetriesException
102-
* @throws LocalizedException
103-
* @throws NoSuchEntityException
104-
*/
105-
protected function syncReplicas(array $storeIds = []): void
106-
{
107-
if (count($storeIds)) {
108-
foreach ($storeIds as $storeId) {
109-
$this->syncReplicasForStore($storeId);
110-
}
111-
} else {
112-
$this->syncReplicasForAllStores();
113-
}
114-
}
115-
116-
/**
117-
* @throws NoSuchEntityException
118-
* @throws ExceededRetriesException
119-
* @throws AlgoliaException
120-
* @throws LocalizedException
121-
*/
122-
protected function syncReplicasForStore(int $storeId): void
123-
{
124-
$this->output->writeln('<info>Syncing ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
125-
try {
126-
$this->replicaManager->syncReplicasToAlgolia($storeId, $this->productHelper->getIndexSettings($storeId));
127-
} catch (BadRequestException $e) {
128-
$this->output->writeln('<error>Failed syncing replicas for store "' . $this->storeNameFetcher->getStoreName($storeId) . '": ' . $e->getMessage() . '</error>');
129-
throw $e;
130-
}
131-
}
132-
133-
/**
134-
* @throws NoSuchEntityException
135-
* @throws ExceededRetriesException
136-
* @throws AlgoliaException
137-
* @throws LocalizedException
138-
*/
139-
protected function syncReplicasForAllStores(): void
140-
{
141-
$storeIds = array_keys($this->storeManager->getStores());
142-
foreach ($storeIds as $storeId) {
143-
$this->syncReplicasForStore($storeId);
144-
}
145-
}
14698
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Console\Traits;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Magento\Framework\Exception\LocalizedException;
7+
use Magento\Framework\Exception\NoSuchEntityException;
8+
9+
trait ReplicaDeleteCommandTrait
10+
{
11+
/**
12+
* @throws NoSuchEntityException
13+
* @throws AlgoliaException
14+
* @throws LocalizedException
15+
*/
16+
public function deleteReplicas(array $storeIds = [], bool $unused = false): void
17+
{
18+
if (count($storeIds)) {
19+
foreach ($storeIds as $storeId) {
20+
$this->deleteReplicasForStore($storeId, $unused);
21+
}
22+
} else {
23+
$this->deleteReplicasForAllStores($unused);
24+
}
25+
}
26+
27+
/**
28+
* @throws NoSuchEntityException
29+
* @throws LocalizedException
30+
* @throws AlgoliaException
31+
*/
32+
public function deleteReplicasForStore(int $storeId, bool $unused = false): void
33+
{
34+
$this->output->writeln('<info>Deleting' . ($unused ? ' unused ' : ' ') . 'replicas for ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
35+
$this->replicaManager->deleteReplicasFromAlgolia($storeId, $unused);
36+
}
37+
38+
/**
39+
* @throws NoSuchEntityException
40+
* @throws LocalizedException
41+
* @throws AlgoliaException
42+
*/
43+
public function deleteReplicasForAllStores(bool $unused = false): void
44+
{
45+
$storeIds = array_keys($this->storeManager->getStores());
46+
foreach ($storeIds as $storeId) {
47+
$this->deleteReplicasForStore($storeId, $unused);
48+
}
49+
}
50+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Console\Traits;
4+
5+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
6+
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
7+
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
8+
use Magento\Framework\Exception\LocalizedException;
9+
use Magento\Framework\Exception\NoSuchEntityException;
10+
11+
trait ReplicaSyncCommandTrait
12+
{
13+
14+
/**
15+
* @param int[] $storeIds
16+
* @return void
17+
* @throws AlgoliaException
18+
* @throws ExceededRetriesException
19+
* @throws LocalizedException
20+
* @throws NoSuchEntityException
21+
*/
22+
public function syncReplicas(array $storeIds = []): void
23+
{
24+
if (count($storeIds)) {
25+
foreach ($storeIds as $storeId) {
26+
$this->syncReplicasForStore($storeId);
27+
}
28+
} else {
29+
$this->syncReplicasForAllStores();
30+
}
31+
}
32+
33+
/**
34+
* @throws NoSuchEntityException
35+
* @throws ExceededRetriesException
36+
* @throws AlgoliaException
37+
* @throws LocalizedException
38+
*/
39+
public function syncReplicasForStore(int $storeId): void
40+
{
41+
$this->output->writeln('<info>Syncing ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
42+
try {
43+
$this->replicaManager->syncReplicasToAlgolia($storeId, $this->productHelper->getIndexSettings($storeId));
44+
} catch (BadRequestException $e) {
45+
$this->output->writeln('<error>Failed syncing replicas for store "' . $this->storeNameFetcher->getStoreName($storeId) . '": ' . $e->getMessage() . '</error>');
46+
throw $e;
47+
}
48+
}
49+
50+
/**
51+
* @throws NoSuchEntityException
52+
* @throws ExceededRetriesException
53+
* @throws AlgoliaException
54+
* @throws LocalizedException
55+
*/
56+
public function syncReplicasForAllStores(): void
57+
{
58+
$storeIds = array_keys($this->storeManager->getStores());
59+
foreach ($storeIds as $storeId) {
60+
$this->syncReplicasForStore($storeId);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)