Skip to content

Commit f347bd0

Browse files
committed
MAGE-1113 Switch to AlgoliaConnector for 3.15+
1 parent f0d0bb4 commit f347bd0

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
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\Builder;
4+
5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
6+
7+
interface EntityIndexOptionsBuilderInterface
8+
{
9+
public function buildEntityIndexOptions(int $storeId, ?bool $isTmp = false): IndexOptionsInterface;
10+
}

Console/Command/SynonymDeduplicateCommand.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Algolia\AlgoliaSearch\Console\Command;
44

5-
use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
6-
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
5+
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
6+
use Algolia\AlgoliaSearch\Service\Product\IndexOptionsBuilder;
77
use Algolia\AlgoliaSearch\Service\StoreNameFetcher;
88
use Magento\Framework\App\State;
99
use Magento\Framework\Console\Cli;
@@ -15,11 +15,11 @@
1515
class SynonymDeduplicateCommand extends AbstractStoreCommand
1616
{
1717
public function __construct(
18-
protected AlgoliaHelper $algoliaHelper,
19-
protected IndexNameFetcher $indexNameFetcher,
18+
protected AlgoliaConnector $algoliaConnector,
2019
protected State $state,
2120
protected StoreNameFetcher $storeNameFetcher,
2221
protected StoreManagerInterface $storeManager,
22+
protected IndexOptionsBuilder $indexOptionsBuilder,
2323
?string $name = null
2424
) {
2525
parent::__construct($state, $storeNameFetcher, $name);
@@ -111,8 +111,9 @@ public function dedupeSynonyms(array $storeIds = []): void
111111
public function dedupeSynonymsForStore(int $storeId): void
112112
{
113113
$this->output->writeln('<info>De-duplicating synonyms for ' . $this->storeNameFetcher->getStoreName($storeId) . '...</info>');
114-
$indexName = $this->indexNameFetcher->getProductIndexName($storeId);
115-
$settings = $this->algoliaHelper->getSettings($indexName);
114+
115+
$indexOptions = $this->indexOptionsBuilder->buildEntityIndexOptions($storeId);
116+
$settings = $this->algoliaConnector->getSettings($indexOptions);
116117
$deduped = $this->dedupeSpecificSettings(['synonyms', 'altCorrections'], $settings);
117118

118119
//bring over as is (no de-dupe necessary)
@@ -123,9 +124,9 @@ public function dedupeSynonymsForStore(int $storeId): void
123124
// Updating the synonyms requires a separate endpoint which is not currently not exposed in the PHP API client
124125
// See https://www.algolia.com/doc/rest-api/search/#tag/Synonyms/operation/saveSynonyms
125126
// This method will clear and then overwrite ... (does not handle one way synonyms which are not exposed in settings)
126-
$this->algoliaHelper->clearSynonyms($indexName);
127-
$this->algoliaHelper->setSettings($indexName, $deduped, false, false);
128-
$this->algoliaHelper->waitLastTask($indexName);
127+
$this->algoliaConnector->clearSynonyms($indexOptions);
128+
$this->algoliaConnector->setSettings($indexOptions, $deduped, false, false);
129+
$this->algoliaConnector->waitLastTask($storeId);
129130
}
130131

131132
/**

Helper/AlgoliaHelper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,13 @@ public function deleteRule(
248248
$this->algoliaConnector->deleteRule($indexOptions, $objectID, $forwardToReplicas);
249249
}
250250

251-
public function clearSynonyms(string $indexName, bool $forwardToReplicas = false): void
251+
/**
252+
* @throws AlgoliaException|NoSuchEntityException
253+
*/
254+
public function clearSynonyms(string $indexName, bool $forwardToReplicas = false, ?int $storeId = null): void
252255
{
253-
$this->client->clearSynonyms($indexName, $forwardToReplicas);
256+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
257+
$this->algoliaConnector->clearSynonyms($indexOptions, $forwardToReplicas);
254258
}
255259

256260
/**

Model/IndexOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public function isTemporaryIndex(): bool
4545
/**
4646
* Returns the final index name computed by the IndexNameFetcher
4747
*
48-
* @return string
48+
* @return string|null
4949
*/
50-
public function getIndexName(): string
50+
public function getIndexName(): ?string
5151
{
5252
return $this->getData(IndexOptionsInterface::INDEX_NAME);
5353
}

Service/AlgoliaConnector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,14 @@ public function copySynonyms(IndexOptionsInterface $fromIndexOptions, IndexOptio
587587
$this->setLastOperationInfo($fromIndexOptions, $response);
588588
}
589589

590+
/**
591+
* @throws AlgoliaException
592+
*/
593+
public function clearSynonyms(IndexOptionsInterface $indexOptions, bool $forwardToReplicas = false): void
594+
{
595+
$this->getClient($indexOptions->getStoreId())->clearSynonyms($indexOptions->getIndexName(), $forwardToReplicas);
596+
}
597+
590598
/**
591599
* Warning: This method can't be performed across two different applications
592600
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Service\Product;
4+
5+
use Algolia\AlgoliaSearch\Api\Builder\EntityIndexOptionsBuilderInterface;
6+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
7+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
8+
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
9+
use Algolia\AlgoliaSearch\Service\IndexOptionsBuilder as BaseIndexOptionsBuilder;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
12+
class IndexOptionsBuilder extends BaseIndexOptionsBuilder implements EntityIndexOptionsBuilderInterface
13+
{
14+
/**
15+
* @throws NoSuchEntityException
16+
* @throws AlgoliaException
17+
*/
18+
public function buildEntityIndexOptions(int $storeId, ?bool $isTmp = false): IndexOptionsInterface
19+
{
20+
return $this->buildWithComputedIndex(ProductHelper::INDEX_NAME_SUFFIX, $storeId, $isTmp);
21+
}
22+
}

0 commit comments

Comments
 (0)