Skip to content

Commit d61d008

Browse files
committed
MAGE-1097: change AlgoliaConnector method signatures
1 parent 8a1a1c5 commit d61d008

File tree

2 files changed

+223
-111
lines changed

2 files changed

+223
-111
lines changed

Helper/AlgoliaHelper.php

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace Algolia\AlgoliaSearch\Helper;
44

5+
use Algolia\AlgoliaSearch\Api\Data\IndexOptionsInterface;
56
use Algolia\AlgoliaSearch\Api\SearchClient;
67
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
78
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
9+
use Algolia\AlgoliaSearch\Model\IndexOptions;
810
use Algolia\AlgoliaSearch\Model\Search\ListIndicesResponse;
911
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
1012
use Magento\Framework\App\Helper\AbstractHelper;
1113
use Exception;
1214
use Magento\Framework\App\Helper\Context;
1315
use Magento\Framework\App\RequestInterface;
16+
use Magento\Framework\Exception\NoSuchEntityException;
1417

1518
/**
1619
* @deprecated (will be removed in v3.16.0)
@@ -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->convertToIndexOptions($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->convertToIndexOptions($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,25 +103,28 @@ public function setSettings(
96103
string $mergeSettingsFrom = '',
97104
?int $storeId = null
98105
) {
106+
$indexOptions = $this->convertToIndexOptions($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->convertToIndexOptions($indexName, $storeId);
126+
127+
$this->algoliaConnector->deleteIndex($indexOptions);
118128
}
119129

120130
/**
@@ -126,19 +136,24 @@ public function deleteIndex(string $indexName, ?int $storeId = null): void
126136
*/
127137
public function deleteObjects(array $ids, string $indexName, ?int $storeId = null): void
128138
{
129-
$this->algoliaConnector->deleteObjects($ids, $indexName, $storeId);
139+
$indexOptions = $this->convertToIndexOptions($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->convertToIndexOptions($fromIndexName, $storeId);
154+
$toIndexOptions = $this->convertToIndexOptions($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->convertToIndexOptions($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->convertToIndexOptions($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->convertToIndexOptions($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->convertToIndexOptions($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->convertToIndexOptions($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->convertToIndexOptions($fromIndexName, $storeId);
262+
$toIndexOptions = $this->convertToIndexOptions($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->convertToIndexOptions($fromIndexName, $storeId);
278+
$toIndexOptions = $this->convertToIndexOptions($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->convertToIndexOptions($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->convertToIndexOptions($indexName, $storeId);
307+
308+
$this->algoliaConnector->clearIndex($indexOptions);
272309
}
273310

274311
/**
@@ -301,4 +338,22 @@ public function getLastTaskId(?int $storeId = null): int
301338
{
302339
return $this->algoliaConnector->getLastTaskId($storeId);
303340
}
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+
}
304359
}

0 commit comments

Comments
 (0)