Skip to content

Commit ee49a5f

Browse files
committed
MAGE-1170: revamp IndexOptionsBuilder with buildWithX methods
1 parent c912fda commit ee49a5f

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

Helper/AlgoliaHelper.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
70+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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
80+
* @throws AlgoliaException|NoSuchEntityException
8181
*/
8282
public function getObjects(string $indexName, array $objectIds, ?int $storeId = null): array
8383
{
84-
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
84+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
106+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
125+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
126126

127127
$this->algoliaConnector->deleteIndex($indexOptions);
128128
}
@@ -136,7 +136,7 @@ public function deleteIndex(string $indexName, ?int $storeId = null): void
136136
*/
137137
public function deleteObjects(array $ids, string $indexName, ?int $storeId = null): void
138138
{
139-
$indexOptions = $this->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
139+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($fromIndexName, $storeId);
154-
$toIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($toIndexName, $storeId);
153+
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($fromIndexName, $storeId);
154+
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
179+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
195+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
210+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
226+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
246+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($fromIndexName, $storeId);
262-
$toIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($toIndexName, $storeId);
261+
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($fromIndexName, $storeId);
262+
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($fromIndexName, $storeId);
278-
$toIndexOptions = $this->indexOptionsBuilder->convertToIndexOptions($toIndexName, $storeId);
277+
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($fromIndexName, $storeId);
278+
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
293+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($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->indexOptionsBuilder->convertToIndexOptions($indexName, $storeId);
306+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
307307

308308
$this->algoliaConnector->clearIndex($indexOptions);
309309
}

Service/IndexOptionsBuilder.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ public function __construct(
2323
* @throws AlgoliaException
2424
* @throws NoSuchEntityException
2525
*/
26-
public function createIndexOptions(?string $indexSuffix = null, ?int $storeId = null, ?bool $isTmp = false): IndexOptions
26+
public function buildWithComputedIndex(
27+
?string $indexSuffix = null,
28+
?int $storeId = null,
29+
?bool $isTmp = false
30+
): IndexOptionsInterface
2731
{
2832
$indexOptions = new IndexOptions([
2933
IndexOptionsInterface::STORE_ID => $storeId,
3034
IndexOptionsInterface::INDEX_SUFFIX => $indexSuffix,
3135
IndexOptionsInterface::IS_TMP => $isTmp
3236
]);
3337

34-
$indexOptions->setIndexName($this->computeIndexName($indexOptions));
35-
36-
return $indexOptions;
38+
return $this->build($indexOptions);
3739
}
3840

3941
/**
@@ -42,13 +44,32 @@ public function createIndexOptions(?string $indexSuffix = null, ?int $storeId =
4244
* @param string|null $indexName
4345
* @param int|null $storeId
4446
* @return IndexOptions
47+
* @throws AlgoliaException
48+
* @throws NoSuchEntityException
4549
*/
46-
public function convertToIndexOptions(?string $indexName = null, ?int $storeId = null): IndexOptions
50+
public function buildWithEnforcedIndex(?string $indexName = null, ?int $storeId = null): IndexOptionsInterface
4751
{
48-
return new IndexOptions([
52+
$indexOptions = new IndexOptions([
4953
IndexOptionsInterface::INDEX_NAME => $indexName,
5054
IndexOptionsInterface::STORE_ID => $storeId
5155
]);
56+
57+
return $this->build($indexOptions);
58+
}
59+
60+
/**
61+
* Build IndexOptions object by setting its index name
62+
*
63+
* @param IndexOptionsInterface $indexOptions
64+
* @return IndexOptionsInterface
65+
* @throws AlgoliaException
66+
* @throws NoSuchEntityException
67+
*/
68+
protected function build(IndexOptionsInterface $indexOptions): IndexOptionsInterface
69+
{
70+
$indexOptions->setIndexName($this->computeIndexName($indexOptions));
71+
72+
return $indexOptions;
5273
}
5374

5475
/**
@@ -61,6 +82,10 @@ public function convertToIndexOptions(?string $indexName = null, ?int $storeId =
6182
*/
6283
protected function computeIndexName(IndexOptionsInterface $indexOptions): ?string
6384
{
85+
if (!is_null($indexOptions->getIndexName())) {
86+
return $indexOptions->getIndexName();
87+
}
88+
6489
if (is_null($indexOptions->getIndexSuffix())) {
6590
throw new AlgoliaException('Index suffix is mandatory in case no enforced index name is specified.');
6691
}

0 commit comments

Comments
 (0)