Skip to content

Commit 8ff3a54

Browse files
committed
MAGE-938 Add store name fetcher service
1 parent a38c014 commit 8ff3a54

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

Console/Command/ReplicaCommand.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88
use Algolia\AlgoliaSearch\Exceptions\BadRequestException;
99
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
1010
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
11+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
1112
use Magento\Framework\App\Area;
1213
use Magento\Framework\App\State;
1314
use Magento\Framework\Console\Cli;
1415
use Magento\Framework\Exception\LocalizedException;
1516
use Magento\Framework\Exception\NoSuchEntityException;
1617
use Magento\Store\Model\StoreManagerInterface;
1718
use Symfony\Component\Console\Command\Command;
18-
use Symfony\Component\Console\Input\InputInterface;
1919
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
2021
use Symfony\Component\Console\Output\OutputInterface;
2122

2223
class ReplicaCommand extends Command
@@ -25,9 +26,6 @@ class ReplicaCommand extends Command
2526

2627
protected ?OutputInterface $output = null;
2728

28-
/** @var string[] */
29-
protected array $_storeNames = [];
30-
3129
/**
3230
* @param ProductHelper $productHelper
3331
* @param ReplicaManagerInterface $replicaManager
@@ -39,6 +37,7 @@ public function __construct(
3937
protected ProductHelper $productHelper,
4038
protected ReplicaManagerInterface $replicaManager,
4139
protected StoreManagerInterface $storeManager,
40+
protected StoreNameFetcher $storeNameFetcher,
4241
?string $name = null
4342
)
4443
{
@@ -82,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8281
/** @var string[] $storeNames */
8382
$storeNames = array_map(
8483
function($storeId) {
85-
return $this->getStoreName($storeId);
84+
return $this->storeNameFetcher->getStoreName($storeId);
8685
},
8786
$storeIds
8887
);
@@ -108,17 +107,6 @@ function($storeId) {
108107
return Cli::RETURN_SUCCESS;
109108
}
110109

111-
/**
112-
* @throws NoSuchEntityException
113-
*/
114-
protected function getStoreName(int $storeId): string
115-
{
116-
if (!isset($this->_storeNames[$storeId])) {
117-
$this->_storeNames[$storeId] = $this->storeManager->getStore($storeId)->getName();
118-
}
119-
return $this->_storeNames[$storeId];
120-
}
121-
122110
/**
123111
* @param int[] $storeIds
124112
* @return void
@@ -146,12 +134,12 @@ protected function syncReplicas(array $storeIds = []): void
146134
*/
147135
protected function syncReplicasForStore(int $storeId): void
148136
{
149-
$this->output->writeln('<info>Syncing ' . $this->getStoreName($storeId) . '...</info>');
137+
$this->output->writeln('<info>Syncing ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
150138
try {
151139
$this->replicaManager->syncReplicasToAlgolia($storeId, $this->productHelper->getIndexSettings($storeId));
152140
}
153141
catch (BadRequestException $e) {
154-
$this->output->writeln('<error>Failed syncing replicas for store "' . $this->getStoreName($storeId) . '": ' . $e->getMessage() . '</error>');
142+
$this->output->writeln('<error>Failed syncing replicas for store "' . $this->storeNameFetcher->getStoreName($storeId) . '": ' . $e->getMessage() . '</error>');
155143
throw $e;
156144
}
157145
}

Service/Product/ReplicaManager.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Algolia\AlgoliaSearch\Helper\Logger;
1515
use Algolia\AlgoliaSearch\Registry\ReplicaState;
1616
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
17+
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
1718
use Algolia\AlgoliaSearch\Validator\VirtualReplicaValidatorFactory;
1819
use Magento\Framework\Exception\LocalizedException;
1920
use Magento\Framework\Exception\NoSuchEntityException;
@@ -48,6 +49,7 @@ public function __construct(
4849
protected ReplicaState $replicaState,
4950
protected VirtualReplicaValidatorFactory $validatorFactory,
5051
protected IndexNameFetcher $indexNameFetcher,
52+
protected StoreNameFetcher $storeNameFetcher,
5153
protected SortingTransformer $sortingTransformer,
5254
protected Logger $logger
5355
)
@@ -251,17 +253,18 @@ protected function isReplicaConfigurationValid(int $storeId): bool
251253
$sortingIndices = $this->sortingTransformer->getSortingIndices($storeId);
252254
$validator = $this->validatorFactory->create();
253255
if (!$validator->isReplicaConfigurationValid($sortingIndices)) {
256+
$storeName = $this->storeNameFetcher->getStoreName($storeId) . " (Store ID=$storeId)";
254257
$postfix = "Please note that there can be no more than " . $this->getMaxVirtualReplicasPerIndex() . " virtual replicas per index.";
255258
if ($this->revertReplicaConfig($storeId)) {
256259
$postfix .= ' Reverting to previous configuration.';
257260
}
258261
if ($validator->isTooManyCustomerGroups()) {
259-
throw (new TooManyCustomerGroupsAsReplicasException(__("You have too many customer groups to enable virtual replicas on the pricing sort for store $storeId. $postfix")))
262+
throw (new TooManyCustomerGroupsAsReplicasException(__("You have too many customer groups to enable virtual replicas on the pricing sort for $storeName. $postfix")))
260263
->withReplicaCount($validator->getReplicaCount())
261264
->withPriceSortReplicaCount($validator->getPriceSortReplicaCount());
262265
}
263266
else {
264-
throw (new ReplicaLimitExceededException(__("Replica limit exceeded for store ID $storeId. $postfix")))
267+
throw (new ReplicaLimitExceededException(__("Replica limit exceeded for $storeName. $postfix")))
265268
->withReplicaCount($validator->getReplicaCount());
266269
}
267270
}

Service/StoreNameFetcher.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service;
4+
5+
use Magento\Framework\Exception\NoSuchEntityException;
6+
use Magento\Store\Model\StoreManagerInterface;
7+
8+
class StoreNameFetcher
9+
{
10+
/** @var string[] */
11+
protected array $_storeNames = [];
12+
13+
public function __construct(
14+
protected StoreManagerInterface $storeManager
15+
)
16+
{}
17+
18+
/**
19+
* @throws NoSuchEntityException
20+
*/
21+
public function getStoreName(int $storeId): string
22+
{
23+
if (!isset($this->_storeNames[$storeId])) {
24+
$this->_storeNames[$storeId] = $this->storeManager->getStore($storeId)->getName();
25+
}
26+
return $this->_storeNames[$storeId];
27+
}
28+
}

0 commit comments

Comments
 (0)