Skip to content

Commit c912fda

Browse files
committed
MAGE-1170: add IndexOptionsBuilder
1 parent bcb2e07 commit c912fda

File tree

5 files changed

+145
-97
lines changed

5 files changed

+145
-97
lines changed

Api/Data/IndexOptionsInterface.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface IndexOptionsInterface
1010

1111
const IS_TMP = 'is_tmp';
1212

13-
const ENFORCED_INDEX_NAME = 'enforced_index_name';
13+
const INDEX_NAME = 'index_name';
1414

1515
/**
1616
* Get field: store_id
@@ -34,9 +34,17 @@ public function getIndexSuffix(): ?string;
3434
public function isTemporaryIndex(): bool;
3535

3636
/**
37-
* Get field: enforced_index_name
37+
* Get field: index_name
3838
*
3939
* @return string|null
4040
*/
41-
public function getEnforcedIndexName(): ?string;
41+
public function getIndexName(): ?string;
42+
43+
/**
44+
* Set field: index_name
45+
*
46+
* @param string $indexName
47+
* @return void
48+
*/
49+
public function setIndexName(string $indexName): void;
4250
}

Helper/AlgoliaHelper.php

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace Algolia\AlgoliaSearch\Helper;
44

5-
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
65
use Algolia\AlgoliaSearch\Api\SearchClient;
76
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
87
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
9-
use Algolia\AlgoliaSearch\Model\IndexOptions;
108
use Algolia\AlgoliaSearch\Model\Search\ListIndicesResponse;
119
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
10+
use Algolia\AlgoliaSearch\Service\IndexOptionsBuilder;
1211
use Magento\Framework\App\Helper\AbstractHelper;
1312
use Exception;
1413
use Magento\Framework\App\Helper\Context;
@@ -22,7 +21,8 @@ class AlgoliaHelper extends AbstractHelper
2221
{
2322
public function __construct(
2423
Context $context,
25-
protected AlgoliaConnector $algoliaConnector
24+
protected AlgoliaConnector $algoliaConnector,
25+
protected IndexOptionsBuilder $indexOptionsBuilder
2626
){
2727
parent::__construct($context);
2828
}
@@ -67,7 +67,7 @@ public function listIndexes(?int $storeId = null)
6767
*/
6868
public function query(string $indexName, string $q, array $params, ?int $storeId = null): array
6969
{
70-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
70+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
7171

7272
return $this->algoliaConnector->query($indexOptions, $q, $params);
7373
}
@@ -77,11 +77,11 @@ public function query(string $indexName, string $q, array $params, ?int $storeId
7777
* @param array $objectIds
7878
* @param int|null $storeId
7979
* @return array<string, mixed>
80-
* @throws AlgoliaException|NoSuchEntityException
80+
* @throws AlgoliaException
8181
*/
8282
public function getObjects(string $indexName, array $objectIds, ?int $storeId = null): array
8383
{
84-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
84+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
8585

8686
return $this->algoliaConnector->getObjects($indexOptions, $objectIds);
8787
}
@@ -103,7 +103,7 @@ public function setSettings(
103103
string $mergeSettingsFrom = '',
104104
?int $storeId = null
105105
) {
106-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
106+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
107107

108108
$this->algoliaConnector->setSettings(
109109
$indexOptions,
@@ -122,7 +122,7 @@ public function setSettings(
122122
*/
123123
public function deleteIndex(string $indexName, ?int $storeId = null): void
124124
{
125-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
125+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
126126

127127
$this->algoliaConnector->deleteIndex($indexOptions);
128128
}
@@ -132,11 +132,11 @@ public function deleteIndex(string $indexName, ?int $storeId = null): void
132132
* @param string $indexName
133133
* @param int|null $storeId
134134
* @return void
135-
* @throws AlgoliaException
135+
* @throws AlgoliaException|NoSuchEntityException
136136
*/
137137
public function deleteObjects(array $ids, string $indexName, ?int $storeId = null): void
138138
{
139-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
139+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
140140

141141
$this->algoliaConnector->deleteObjects($ids, $indexOptions);
142142
}
@@ -150,8 +150,8 @@ public function deleteObjects(array $ids, string $indexName, ?int $storeId = nul
150150
*/
151151
public function moveIndex(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
152152
{
153-
$fromIndexOptions = $this->convertToIndexOptions($fromIndexName, $storeId);
154-
$toIndexOptions = $this->convertToIndexOptions($toIndexName, $storeId);
153+
$fromIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($fromIndexName, $storeId);
154+
$toIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($toIndexName, $storeId);
155155

156156
$this->algoliaConnector->moveIndex($fromIndexOptions, $toIndexOptions);
157157
}
@@ -176,7 +176,7 @@ public function generateSearchSecuredApiKey(string $key, array $params = [], ?in
176176
*/
177177
public function getSettings(string $indexName, ?int $storeId = null): array
178178
{
179-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
179+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
180180

181181
return $this->algoliaConnector->getSettings($indexOptions);
182182
}
@@ -192,7 +192,7 @@ public function getSettings(string $indexName, ?int $storeId = null): array
192192
*/
193193
public function saveObjects(string $indexName, array $objects, bool $isPartialUpdate = false, ?int $storeId = null): void
194194
{
195-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
195+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
196196

197197
$this->algoliaConnector->saveObjects($indexOptions, $objects, $isPartialUpdate);
198198
}
@@ -207,7 +207,7 @@ public function saveObjects(string $indexName, array $objects, bool $isPartialUp
207207
*/
208208
public function saveRule(array $rule, string $indexName, bool $forwardToReplicas = false, ?int $storeId = null): void
209209
{
210-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
210+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
211211

212212
$this->algoliaConnector->saveRule($rule, $indexOptions, $forwardToReplicas);
213213
}
@@ -223,7 +223,7 @@ public function saveRule(array $rule, string $indexName, bool $forwardToReplicas
223223
*/
224224
public function saveRules(string $indexName, array $rules, bool $forwardToReplicas = false, ?int $storeId = null): void
225225
{
226-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
226+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
227227

228228
$this->algoliaConnector->saveRules($indexOptions, $rules, $forwardToReplicas);
229229
}
@@ -243,7 +243,7 @@ public function deleteRule(
243243
?int $storeId = null
244244
) : void
245245
{
246-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
246+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
247247

248248
$this->algoliaConnector->deleteRule($indexOptions, $objectID, $forwardToReplicas);
249249
}
@@ -258,8 +258,8 @@ public function deleteRule(
258258
*/
259259
public function copySynonyms(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
260260
{
261-
$fromIndexOptions = $this->convertToIndexOptions($fromIndexName, $storeId);
262-
$toIndexOptions = $this->convertToIndexOptions($toIndexName, $storeId);
261+
$fromIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($fromIndexName, $storeId);
262+
$toIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($toIndexName, $storeId);
263263

264264
$this->algoliaConnector->copySynonyms($fromIndexOptions, $toIndexOptions);
265265
}
@@ -274,8 +274,8 @@ public function copySynonyms(string $fromIndexName, string $toIndexName, ?int $s
274274
*/
275275
public function copyQueryRules(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
276276
{
277-
$fromIndexOptions = $this->convertToIndexOptions($fromIndexName, $storeId);
278-
$toIndexOptions = $this->convertToIndexOptions($toIndexName, $storeId);
277+
$fromIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($fromIndexName, $storeId);
278+
$toIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($toIndexName, $storeId);
279279

280280
$this->algoliaConnector->copyQueryRules($fromIndexOptions, $toIndexOptions);
281281
}
@@ -290,7 +290,7 @@ public function copyQueryRules(string $fromIndexName, string $toIndexName, ?int
290290
*/
291291
public function searchRules(string $indexName, array $searchRulesParams = null, ?int $storeId = null)
292292
{
293-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
293+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
294294

295295
return $this->algoliaConnector->searchRules($indexOptions, $searchRulesParams);
296296
}
@@ -303,7 +303,7 @@ public function searchRules(string $indexName, array $searchRulesParams = null,
303303
*/
304304
public function clearIndex(string $indexName, ?int $storeId = null): void
305305
{
306-
$indexOptions = $this->convertToIndexOptions($indexName, $storeId);
306+
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
307307

308308
$this->algoliaConnector->clearIndex($indexOptions);
309309
}
@@ -338,22 +338,4 @@ public function getLastTaskId(?int $storeId = null): int
338338
{
339339
return $this->algoliaConnector->getLastTaskId($storeId);
340340
}
341-
342-
/**
343-
* Automatically converts information related to an index into a IndexOptions objects
344-
* This ensures compatibility of this deprecated class with the new method signatures of AlgoliaConnector
345-
*
346-
* @param string|null $indexName
347-
* @param int|null $storeId
348-
* @param bool|null $isTmp
349-
* @return IndexOptions
350-
*/
351-
protected function convertToIndexOptions(?string $indexName = null, ?int $storeId = null, ?bool $isTmp = false): IndexOptions
352-
{
353-
return new IndexOptions([
354-
IndexOptionsInterface::ENFORCED_INDEX_NAME => $indexName,
355-
IndexOptionsInterface::STORE_ID => $storeId,
356-
IndexOptionsInterface::IS_TMP => $isTmp
357-
]);
358-
}
359341
}

Model/IndexOptions.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,24 @@ public function isTemporaryIndex(): bool
4141
return $this->hasData(IndexOptionsInterface::IS_TMP) && $this->getData(IndexOptionsInterface::IS_TMP);
4242
}
4343

44+
4445
/**
45-
* In case an enforced index name is used, this will override the index names fetched by the IndexNameFetcher service
46+
* Returns the final index name computed by the IndexNameFetcher
4647
*
47-
* @return string|null
48+
* @return string
4849
*/
49-
public function getEnforcedIndexName(): ?string
50+
public function getIndexName(): string
5051
{
51-
return $this->hasData(IndexOptionsInterface::ENFORCED_INDEX_NAME) ?
52-
(string) $this->getData(IndexOptionsInterface::ENFORCED_INDEX_NAME) :
53-
null;
52+
return $this->getData(IndexOptionsInterface::INDEX_NAME);
53+
}
54+
55+
/**
56+
* @param string $indexName
57+
*
58+
* @return void
59+
*/
60+
public function setIndexName(string $indexName): void
61+
{
62+
$this->setData(IndexOptionsInterface::INDEX_NAME, $indexName);
5463
}
5564
}

0 commit comments

Comments
 (0)