Skip to content

Commit 9394804

Browse files
authored
Merge pull request #1668 from algolia/feat/MAGE-1170-indexoptions
MAGE-1170: Handle Index options with `IndexNameFetcher`
2 parents 50d37d6 + ee49a5f commit 9394804

File tree

6 files changed

+444
-139
lines changed

6 files changed

+444
-139
lines changed

Api/Data/IndexOptionsInterface.php

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\Api\Data;
4+
5+
interface IndexOptionsInterface
6+
{
7+
const STORE_ID = 'store_id';
8+
9+
const INDEX_SUFFIX = 'index_suffix';
10+
11+
const IS_TMP = 'is_tmp';
12+
13+
const INDEX_NAME = 'index_name';
14+
15+
/**
16+
* Get field: store_id
17+
*
18+
* @return int|null
19+
*/
20+
public function getStoreId(): ?int;
21+
22+
/**
23+
* Get field: index_suffix
24+
*
25+
* @return string|null
26+
*/
27+
public function getIndexSuffix(): ?string;
28+
29+
/**
30+
* Get field: is_tmp
31+
*
32+
* @return bool
33+
*/
34+
public function isTemporaryIndex(): bool;
35+
36+
/**
37+
* Get field: index_name
38+
*
39+
* @return string|null
40+
*/
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;
50+
}

Helper/AlgoliaHelper.php

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
88
use Algolia\AlgoliaSearch\Model\Search\ListIndicesResponse;
99
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
10+
use Algolia\AlgoliaSearch\Service\IndexOptionsBuilder;
1011
use Magento\Framework\App\Helper\AbstractHelper;
1112
use Exception;
1213
use Magento\Framework\App\Helper\Context;
1314
use Magento\Framework\App\RequestInterface;
15+
use Magento\Framework\Exception\NoSuchEntityException;
1416

1517
/**
1618
* @deprecated (will be removed in v3.16.0)
@@ -19,7 +21,8 @@ class AlgoliaHelper extends AbstractHelper
1921
{
2022
public function __construct(
2123
Context $context,
22-
protected AlgoliaConnector $algoliaConnector
24+
protected AlgoliaConnector $algoliaConnector,
25+
protected IndexOptionsBuilder $indexOptionsBuilder
2326
){
2427
parent::__construct($context);
2528
}
@@ -59,24 +62,28 @@ public function listIndexes(?int $storeId = null)
5962
* @param array $params
6063
* @param int|null $storeId
6164
* @return array<string, mixed>
62-
* @throws AlgoliaException
65+
* @throws AlgoliaException|NoSuchEntityException
6366
* @internal This method is currently unstable and should not be used. It may be revisited ar fixed in a future version.
6467
*/
6568
public function query(string $indexName, string $q, array $params, ?int $storeId = null): array
6669
{
67-
return $this->algoliaConnector->query($indexName, $q, $params, $storeId);
70+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
71+
72+
return $this->algoliaConnector->query($indexOptions, $q, $params);
6873
}
6974

7075
/**
7176
* @param string $indexName
7277
* @param array $objectIds
7378
* @param int|null $storeId
7479
* @return array<string, mixed>
75-
* @throws AlgoliaException
80+
* @throws AlgoliaException|NoSuchEntityException
7681
*/
7782
public function getObjects(string $indexName, array $objectIds, ?int $storeId = null): array
7883
{
79-
return $this->algoliaConnector->getObjects($indexName, $objectIds, $storeId);
84+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
85+
86+
return $this->algoliaConnector->getObjects($indexOptions, $objectIds);
8087
}
8188

8289
/**
@@ -86,7 +93,7 @@ public function getObjects(string $indexName, array $objectIds, ?int $storeId =
8693
* @param bool $mergeSettings
8794
* @param string $mergeSettingsFrom
8895
* @param int|null $storeId
89-
* @throws AlgoliaException
96+
* @throws AlgoliaException|NoSuchEntityException
9097
*/
9198
public function setSettings(
9299
$indexName,
@@ -96,49 +103,57 @@ public function setSettings(
96103
string $mergeSettingsFrom = '',
97104
?int $storeId = null
98105
) {
106+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
107+
99108
$this->algoliaConnector->setSettings(
100-
$indexName,
109+
$indexOptions,
101110
$settings,
102111
$forwardToReplicas,
103112
$mergeSettings,
104-
$mergeSettingsFrom,
105-
$storeId
113+
$mergeSettingsFrom
106114
);
107115
}
108116

109117
/**
110118
* @param string $indexName
111119
* @param int|null $storeId
112120
* @return void
113-
* @throws AlgoliaException
121+
* @throws AlgoliaException|NoSuchEntityException
114122
*/
115123
public function deleteIndex(string $indexName, ?int $storeId = null): void
116124
{
117-
$this->algoliaConnector->deleteIndex($indexName, $storeId);
125+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
126+
127+
$this->algoliaConnector->deleteIndex($indexOptions);
118128
}
119129

120130
/**
121131
* @param array $ids
122132
* @param string $indexName
123133
* @param int|null $storeId
124134
* @return void
125-
* @throws AlgoliaException
135+
* @throws AlgoliaException|NoSuchEntityException
126136
*/
127137
public function deleteObjects(array $ids, string $indexName, ?int $storeId = null): void
128138
{
129-
$this->algoliaConnector->deleteObjects($ids, $indexName, $storeId);
139+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
140+
141+
$this->algoliaConnector->deleteObjects($ids, $indexOptions);
130142
}
131143

132144
/**
133145
* @param string $fromIndexName
134146
* @param string $toIndexName
135147
* @param int|null $storeId
136148
* @return void
137-
* @throws AlgoliaException
149+
* @throws AlgoliaException|NoSuchEntityException
138150
*/
139151
public function moveIndex(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
140152
{
141-
$this->algoliaConnector->moveIndex($fromIndexName, $toIndexName, $storeId);
153+
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($fromIndexName, $storeId);
154+
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($toIndexName, $storeId);
155+
156+
$this->algoliaConnector->moveIndex($fromIndexOptions, $toIndexOptions);
142157
}
143158

144159
/**
@@ -157,11 +172,13 @@ public function generateSearchSecuredApiKey(string $key, array $params = [], ?in
157172
* @param string $indexName
158173
* @param int|null $storeId
159174
* @return array<string, mixed>
160-
* @throws AlgoliaException
175+
* @throws AlgoliaException|NoSuchEntityException
161176
*/
162177
public function getSettings(string $indexName, ?int $storeId = null): array
163178
{
164-
return $this->algoliaConnector->getSettings($indexName, $storeId);
179+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
180+
181+
return $this->algoliaConnector->getSettings($indexOptions);
165182
}
166183

167184
/**
@@ -175,7 +192,9 @@ public function getSettings(string $indexName, ?int $storeId = null): array
175192
*/
176193
public function saveObjects(string $indexName, array $objects, bool $isPartialUpdate = false, ?int $storeId = null): void
177194
{
178-
$this->algoliaConnector->saveObjects($indexName, $objects, $isPartialUpdate, $storeId);
195+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
196+
197+
$this->algoliaConnector->saveObjects($indexOptions, $objects, $isPartialUpdate);
179198
}
180199

181200
/**
@@ -184,11 +203,13 @@ public function saveObjects(string $indexName, array $objects, bool $isPartialUp
184203
* @param bool $forwardToReplicas
185204
* @param int|null $storeId
186205
* @return void
187-
* @throws AlgoliaException
206+
* @throws AlgoliaException|NoSuchEntityException
188207
*/
189208
public function saveRule(array $rule, string $indexName, bool $forwardToReplicas = false, ?int $storeId = null): void
190209
{
191-
$this->algoliaConnector->saveRule($rule, $indexName, $forwardToReplicas, $storeId);
210+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
211+
212+
$this->algoliaConnector->saveRule($rule, $indexOptions, $forwardToReplicas);
192213
}
193214

194215
/**
@@ -197,10 +218,14 @@ public function saveRule(array $rule, string $indexName, bool $forwardToReplicas
197218
* @param bool $forwardToReplicas
198219
* @param int|null $storeId
199220
* @return void
221+
* @throws AlgoliaException
222+
* @throws NoSuchEntityException
200223
*/
201224
public function saveRules(string $indexName, array $rules, bool $forwardToReplicas = false, ?int $storeId = null): void
202225
{
203-
$this->algoliaConnector->saveRules($indexName, $rules, $forwardToReplicas, $storeId);
226+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
227+
228+
$this->algoliaConnector->saveRules($indexOptions, $rules, $forwardToReplicas);
204229
}
205230

206231
/**
@@ -209,7 +234,7 @@ public function saveRules(string $indexName, array $rules, bool $forwardToReplic
209234
* @param bool $forwardToReplicas
210235
* @param int|null $storeId
211236
* @return void
212-
* @throws AlgoliaException
237+
* @throws AlgoliaException|NoSuchEntityException
213238
*/
214239
public function deleteRule(
215240
string $indexName,
@@ -218,7 +243,9 @@ public function deleteRule(
218243
?int $storeId = null
219244
) : void
220245
{
221-
$this->algoliaConnector->deleteRule($indexName, $objectID, $forwardToReplicas, $storeId);
246+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
247+
248+
$this->algoliaConnector->deleteRule($indexOptions, $objectID, $forwardToReplicas);
222249
}
223250

224251
/**
@@ -227,11 +254,14 @@ public function deleteRule(
227254
* @param int|null $storeId
228255
* @return void
229256
* @throws AlgoliaException
230-
* @throws ExceededRetriesException
257+
* @throws ExceededRetriesException|NoSuchEntityException
231258
*/
232259
public function copySynonyms(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
233260
{
234-
$this->algoliaConnector->copySynonyms($fromIndexName, $toIndexName, $storeId);
261+
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($fromIndexName, $storeId);
262+
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($toIndexName, $storeId);
263+
264+
$this->algoliaConnector->copySynonyms($fromIndexOptions, $toIndexOptions);
235265
}
236266

237267
/**
@@ -240,11 +270,14 @@ public function copySynonyms(string $fromIndexName, string $toIndexName, ?int $s
240270
* @param int|null $storeId
241271
* @return void
242272
* @throws AlgoliaException
243-
* @throws ExceededRetriesException
273+
* @throws ExceededRetriesException|NoSuchEntityException
244274
*/
245275
public function copyQueryRules(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
246276
{
247-
$this->algoliaConnector->copyQueryRules($fromIndexName, $toIndexName, $storeId);
277+
$fromIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($fromIndexName, $storeId);
278+
$toIndexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($toIndexName, $storeId);
279+
280+
$this->algoliaConnector->copyQueryRules($fromIndexOptions, $toIndexOptions);
248281
}
249282

250283
/**
@@ -253,22 +286,26 @@ public function copyQueryRules(string $fromIndexName, string $toIndexName, ?int
253286
* @param int|null $storeId
254287
* @return array
255288
*
256-
* @throws AlgoliaException
289+
* @throws AlgoliaException|NoSuchEntityException
257290
*/
258-
public function searchRules(string $indexName, array$searchRulesParams = null, ?int $storeId = null)
291+
public function searchRules(string $indexName, array $searchRulesParams = null, ?int $storeId = null)
259292
{
260-
return $this->algoliaConnector->searchRules($indexName, $searchRulesParams, $storeId);
293+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
294+
295+
return $this->algoliaConnector->searchRules($indexOptions, $searchRulesParams);
261296
}
262297

263298
/**
264299
* @param string $indexName
265300
* @param int|null $storeId
266301
* @return void
267-
* @throws AlgoliaException
302+
* @throws AlgoliaException|NoSuchEntityException
268303
*/
269304
public function clearIndex(string $indexName, ?int $storeId = null): void
270305
{
271-
$this->algoliaConnector->clearIndex($indexName, $storeId);
306+
$indexOptions = $this->indexOptionsBuilder->buildWithEnforcedIndex($indexName, $storeId);
307+
308+
$this->algoliaConnector->clearIndex($indexOptions);
272309
}
273310

274311
/**

Model/IndexOptions.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Model;
4+
5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
6+
use Magento\Framework\DataObject;
7+
8+
class IndexOptions extends DataObject implements IndexOptionsInterface
9+
{
10+
/**
11+
* Return the current store_id; if the method returns null, the Magento default store will be used
12+
*
13+
* @return int|null
14+
*/
15+
public function getStoreId(): ?int
16+
{
17+
return $this->hasData(IndexOptionsInterface::STORE_ID) ?
18+
(int) $this->getData(IndexOptionsInterface::STORE_ID) :
19+
null;
20+
}
21+
22+
/**
23+
* Suffix usually refers to the entity to index (_products, _categories, _pages, ...)
24+
*
25+
* @return string|null
26+
*/
27+
public function getIndexSuffix(): ?string
28+
{
29+
return $this->hasData(IndexOptionsInterface::INDEX_SUFFIX) ?
30+
(string) $this->getData(IndexOptionsInterface::INDEX_SUFFIX) :
31+
null;
32+
}
33+
34+
/**
35+
* Temporary indices can be used in case of full reindexing
36+
*
37+
* @return bool
38+
*/
39+
public function isTemporaryIndex(): bool
40+
{
41+
return $this->hasData(IndexOptionsInterface::IS_TMP) && $this->getData(IndexOptionsInterface::IS_TMP);
42+
}
43+
44+
45+
/**
46+
* Returns the final index name computed by the IndexNameFetcher
47+
*
48+
* @return string
49+
*/
50+
public function getIndexName(): string
51+
{
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);
63+
}
64+
}

0 commit comments

Comments
 (0)