Skip to content

Commit 3ae9a4c

Browse files
committed
MAGE-1134 changes after review
1 parent 7a73900 commit 3ae9a4c

13 files changed

+233
-288
lines changed

Helper/AlgoliaHelper.php

Lines changed: 47 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,11 @@
1212
use Magento\Framework\App\Helper\Context;
1313
use Magento\Framework\App\RequestInterface;
1414

15+
/**
16+
* @deprecated (will be removed in v3.16.0)
17+
*/
1518
class AlgoliaHelper extends AbstractHelper
1619
{
17-
/**
18-
* @var string Case-sensitive object ID key
19-
*/
20-
public const ALGOLIA_API_OBJECT_ID = 'objectID';
21-
22-
/**
23-
* @var string
24-
*/
25-
public const ALGOLIA_API_INDEX_NAME = 'indexName';
26-
27-
/**
28-
* @var string
29-
*/
30-
public const ALGOLIA_API_TASK_ID = 'taskID';
31-
32-
/**
33-
* @var int
34-
*/
35-
public const ALGOLIA_DEFAULT_SCOPE = 0;
36-
3720
public function __construct(
3821
Context $context,
3922
protected AlgoliaConnector $algoliaConnector
@@ -49,40 +32,14 @@ public function getRequest(): RequestInterface
4932
return $this->_getRequest();
5033
}
5134

52-
/**
53-
* Ensure AlgoliaConnector targets the application configured on a particular store
54-
*
55-
* @param int|null $storeId
56-
* @return void
57-
*/
58-
protected function handleStoreContext(int|null $storeId): void
59-
{
60-
if (!is_null($storeId)) {
61-
$this->algoliaConnector->setStoreId($storeId);
62-
}
63-
}
64-
65-
/**
66-
* Restore AlgoliaConnector default Scope
67-
*
68-
* @return void
69-
*/
70-
protected function restoreDefaultScope(): void
71-
{
72-
$this->algoliaConnector->setStoreId(self::ALGOLIA_DEFAULT_SCOPE);
73-
}
74-
7535
/**
7636
* @param int|null $storeId
7737
* @return SearchClient
7838
* @throws AlgoliaException
7939
*/
80-
public function getClient(int $storeId = null): SearchClient
40+
public function getClient(?int $storeId = null): SearchClient
8141
{
82-
$this->handleStoreContext($storeId);
83-
$client = $this->algoliaConnector->getClient();
84-
$this->restoreDefaultScope();
85-
return $client;
42+
return $this->algoliaConnector->getClient($storeId);
8643
}
8744

8845
/**
@@ -91,12 +48,9 @@ public function getClient(int $storeId = null): SearchClient
9148
* @return ListIndicesResponse|array<string,mixed>
9249
* @throws AlgoliaException
9350
*/
94-
public function listIndexes(int $storeId = null)
51+
public function listIndexes(?int $storeId = null)
9552
{
96-
$this->handleStoreContext($storeId);
97-
$indexes = $this->algoliaConnector->listIndexes();
98-
$this->restoreDefaultScope();
99-
return $indexes;
53+
return $this->algoliaConnector->listIndexes($storeId);
10054
}
10155

10256
/**
@@ -108,12 +62,9 @@ public function listIndexes(int $storeId = null)
10862
* @throws AlgoliaException
10963
* @internal This method is currently unstable and should not be used. It may be revisited ar fixed in a future version.
11064
*/
111-
public function query(string $indexName, string $q, array $params, int $storeId = null): array
65+
public function query(string $indexName, string $q, array $params, ?int $storeId = null): array
11266
{
113-
$this->handleStoreContext($storeId);
114-
$result = $this->algoliaConnector->query($indexName, $q, $params);
115-
$this->restoreDefaultScope();
116-
return $result;
67+
return $this->algoliaConnector->query($indexName, $q, $params, $storeId);
11768
}
11869

11970
/**
@@ -123,12 +74,9 @@ public function query(string $indexName, string $q, array $params, int $storeId
12374
* @return array<string, mixed>
12475
* @throws AlgoliaException
12576
*/
126-
public function getObjects(string $indexName, array $objectIds, int $storeId = null): array
77+
public function getObjects(string $indexName, array $objectIds, ?int $storeId = null): array
12778
{
128-
$this->handleStoreContext($storeId);
129-
$result = $this->algoliaConnector->getObjects($indexName, $objectIds);
130-
$this->restoreDefaultScope();
131-
return $result;
79+
return $this->algoliaConnector->getObjects($indexName, $objectIds, $storeId);
13280
}
13381

13482
/**
@@ -146,17 +94,16 @@ public function setSettings(
14694
bool $forwardToReplicas = false,
14795
bool $mergeSettings = false,
14896
string $mergeSettingsFrom = '',
149-
int $storeId = null
97+
?int $storeId = null
15098
) {
151-
$this->handleStoreContext($storeId);
15299
$this->algoliaConnector->setSettings(
153100
$indexName,
154101
$settings,
155102
$forwardToReplicas,
156103
$mergeSettings,
157-
$mergeSettingsFrom
104+
$mergeSettingsFrom,
105+
$storeId
158106
);
159-
$this->restoreDefaultScope();
160107
}
161108

162109
/**
@@ -165,11 +112,9 @@ public function setSettings(
165112
* @return void
166113
* @throws AlgoliaException
167114
*/
168-
public function deleteIndex(string $indexName, int $storeId = null): void
115+
public function deleteIndex(string $indexName, ?int $storeId = null): void
169116
{
170-
$this->handleStoreContext($storeId);
171-
$this->algoliaConnector->deleteIndex($indexName);
172-
$this->restoreDefaultScope();
117+
$this->algoliaConnector->deleteIndex($indexName, $storeId);
173118
}
174119

175120
/**
@@ -179,11 +124,9 @@ public function deleteIndex(string $indexName, int $storeId = null): void
179124
* @return void
180125
* @throws AlgoliaException
181126
*/
182-
public function deleteObjects(array $ids, string $indexName, int $storeId = null): void
127+
public function deleteObjects(array $ids, string $indexName, ?int $storeId = null): void
183128
{
184-
$this->handleStoreContext($storeId);
185-
$this->algoliaConnector->deleteObjects($ids, $indexName);
186-
$this->restoreDefaultScope();
129+
$this->algoliaConnector->deleteObjects($ids, $indexName, $storeId);
187130
}
188131

189132
/**
@@ -193,11 +136,9 @@ public function deleteObjects(array $ids, string $indexName, int $storeId = null
193136
* @return void
194137
* @throws AlgoliaException
195138
*/
196-
public function moveIndex(string $fromIndexName, string $toIndexName, int $storeId = null): void
139+
public function moveIndex(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
197140
{
198-
$this->handleStoreContext($storeId);
199-
$this->algoliaConnector->moveIndex($fromIndexName, $toIndexName);
200-
$this->restoreDefaultScope();
141+
$this->algoliaConnector->moveIndex($fromIndexName, $toIndexName, $storeId);
201142
}
202143

203144
/**
@@ -207,12 +148,9 @@ public function moveIndex(string $fromIndexName, string $toIndexName, int $store
207148
* @return string
208149
* @throws AlgoliaException
209150
*/
210-
public function generateSearchSecuredApiKey(string $key, array $params = [], int $storeId = null): string
151+
public function generateSearchSecuredApiKey(string $key, array $params = [], ?int $storeId = null): string
211152
{
212-
$this->handleStoreContext($storeId);
213-
$apiKey = $this->algoliaConnector->generateSearchSecuredApiKey($key, $params);
214-
$this->restoreDefaultScope();
215-
return $apiKey;
153+
return $this->algoliaConnector->generateSearchSecuredApiKey($key, $params, $storeId);
216154
}
217155

218156
/**
@@ -221,12 +159,9 @@ public function generateSearchSecuredApiKey(string $key, array $params = [], int
221159
* @return array<string, mixed>
222160
* @throws AlgoliaException
223161
*/
224-
public function getSettings(string $indexName, int $storeId = null): array
162+
public function getSettings(string $indexName, ?int $storeId = null): array
225163
{
226-
$this->handleStoreContext($storeId);
227-
$settings = $this->algoliaConnector->getSettings($indexName);
228-
$this->restoreDefaultScope();
229-
return $settings;
164+
return $this->algoliaConnector->getSettings($indexName, $storeId);
230165
}
231166

232167
/**
@@ -238,39 +173,34 @@ public function getSettings(string $indexName, int $storeId = null): array
238173
* @return void
239174
* @throws Exception
240175
*/
241-
public function saveObjects(string $indexName, array $objects, bool $isPartialUpdate = false, int $storeId = null): void
176+
public function saveObjects(string $indexName, array $objects, bool $isPartialUpdate = false, ?int $storeId = null): void
242177
{
243-
$this->handleStoreContext($storeId);
244-
$this->algoliaConnector->saveObjects($indexName, $objects, $isPartialUpdate);
245-
$this->restoreDefaultScope();
178+
$this->algoliaConnector->saveObjects($indexName, $objects, $isPartialUpdate, $storeId);
246179
}
247180

248181
/**
249182
* @param array<string, mixed> $rule
250183
* @param string $indexName
251184
* @param bool $forwardToReplicas
185+
* @param int|null $storeId
252186
* @return void
253187
* @throws AlgoliaException
254188
*/
255-
public function saveRule(array $rule, string $indexName, bool $forwardToReplicas = false, int $storeId = null): void
189+
public function saveRule(array $rule, string $indexName, bool $forwardToReplicas = false, ?int $storeId = null): void
256190
{
257-
$this->handleStoreContext($storeId);
258-
$this->algoliaConnector->saveRule($rule, $indexName, $forwardToReplicas);
259-
$this->restoreDefaultScope();
191+
$this->algoliaConnector->saveRule($rule, $indexName, $forwardToReplicas, $storeId);
260192
}
261193

262194
/**
263195
* @param string $indexName
264196
* @param array $rules
265197
* @param bool $forwardToReplicas
266-
* @param null $storeId
198+
* @param int|null $storeId
267199
* @return void
268200
*/
269-
public function saveRules(string $indexName, array $rules, bool $forwardToReplicas = false, $storeId = null): void
201+
public function saveRules(string $indexName, array $rules, bool $forwardToReplicas = false, ?int $storeId = null): void
270202
{
271-
$this->handleStoreContext($storeId);
272-
$this->algoliaConnector->saveRules($indexName, $rules, $forwardToReplicas);
273-
$this->restoreDefaultScope();
203+
$this->algoliaConnector->saveRules($indexName, $rules, $forwardToReplicas, $storeId);
274204
}
275205

276206
/**
@@ -285,12 +215,10 @@ public function deleteRule(
285215
string $indexName,
286216
string $objectID,
287217
bool $forwardToReplicas = false,
288-
int $storeId = null
218+
?int $storeId = null
289219
) : void
290220
{
291-
$this->handleStoreContext($storeId);
292-
$this->algoliaConnector->deleteRule($indexName, $objectID, $forwardToReplicas);
293-
$this->restoreDefaultScope();
221+
$this->algoliaConnector->deleteRule($indexName, $objectID, $forwardToReplicas, $storeId);
294222
}
295223

296224
/**
@@ -301,11 +229,9 @@ public function deleteRule(
301229
* @throws AlgoliaException
302230
* @throws ExceededRetriesException
303231
*/
304-
public function copySynonyms(string $fromIndexName, string $toIndexName, int $storeId = null): void
232+
public function copySynonyms(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
305233
{
306-
$this->handleStoreContext($storeId);
307-
$this->algoliaConnector->copySynonyms($fromIndexName, $toIndexName);
308-
$this->restoreDefaultScope();
234+
$this->algoliaConnector->copySynonyms($fromIndexName, $toIndexName, $storeId);
309235
}
310236

311237
/**
@@ -316,11 +242,9 @@ public function copySynonyms(string $fromIndexName, string $toIndexName, int $st
316242
* @throws AlgoliaException
317243
* @throws ExceededRetriesException
318244
*/
319-
public function copyQueryRules(string $fromIndexName, string $toIndexName, int $storeId = null): void
245+
public function copyQueryRules(string $fromIndexName, string $toIndexName, ?int $storeId = null): void
320246
{
321-
$this->handleStoreContext($storeId);
322-
$this->algoliaConnector->copyQueryRules($fromIndexName, $toIndexName);
323-
$this->restoreDefaultScope();
247+
$this->algoliaConnector->copyQueryRules($fromIndexName, $toIndexName, $storeId);
324248
}
325249

326250
/**
@@ -331,22 +255,20 @@ public function copyQueryRules(string $fromIndexName, string $toIndexName, int $
331255
*
332256
* @throws AlgoliaException
333257
*/
334-
public function searchRules(string $indexName, array$searchRulesParams = null, int $storeId = null)
258+
public function searchRules(string $indexName, array$searchRulesParams = null, ?int $storeId = null)
335259
{
336-
$this->handleStoreContext($storeId);
337-
$rules = $this->algoliaConnector->searchRules($indexName, $searchRulesParams);
338-
$this->restoreDefaultScope();
339-
return $rules;
260+
return $this->algoliaConnector->searchRules($indexName, $searchRulesParams, $storeId);
340261
}
341262

342263
/**
343264
* @param string $indexName
265+
* @param int|null $storeId
344266
* @return void
345267
* @throws AlgoliaException
346268
*/
347-
public function clearIndex(string $indexName): void
269+
public function clearIndex(string $indexName, ?int $storeId = null): void
348270
{
349-
$this->algoliaConnector->clearIndex($indexName);
271+
$this->algoliaConnector->clearIndex($indexName, $storeId);
350272
}
351273

352274
/**
@@ -357,11 +279,9 @@ public function clearIndex(string $indexName): void
357279
* @throws AlgoliaException
358280
* @throws ExceededRetriesException
359281
*/
360-
public function waitLastTask(int $storeId = null, string $lastUsedIndexName = null, int $lastTaskId = null): void
282+
public function waitLastTask(?int $storeId = null, ?string $lastUsedIndexName = null, ?int $lastTaskId = null): void
361283
{
362-
$this->handleStoreContext($storeId);
363-
$this->algoliaConnector->waitLastTask($lastUsedIndexName, $lastTaskId);
364-
$this->restoreDefaultScope();
284+
$this->algoliaConnector->waitLastTask($storeId, $lastUsedIndexName, $lastTaskId);
365285
}
366286

367287
/**
@@ -374,14 +294,10 @@ public function castProductObject(&$productData): void
374294
}
375295

376296
/**
377-
* @param int|null $storeId
378297
* @return int
379298
*/
380-
public function getLastTaskId(int $storeId = null): int
299+
public function getLastTaskId(): int
381300
{
382-
$this->handleStoreContext($storeId);
383-
$lastTaskId = $this->algoliaConnector->getLastTaskId();
384-
$this->restoreDefaultScope();
385-
return $lastTaskId;
301+
return $this->algoliaConnector->getLastTaskId();
386302
}
387303
}

Helper/ConfigHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Helper;
44

55
use Algolia\AlgoliaSearch\Api\Product\ReplicaManagerInterface;
6+
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
67
use Magento;
78
use Magento\Cookie\Helper\Cookie as CookieHelper;
89
use Magento\Directory\Model\Currency as DirCurrency;
@@ -1482,7 +1483,7 @@ public function getAttributesToRetrieve($groupId)
14821483
}
14831484
}
14841485
$attributes = array_merge($attributes, [
1485-
AlgoliaHelper::ALGOLIA_API_OBJECT_ID,
1486+
AlgoliaConnector::ALGOLIA_API_OBJECT_ID,
14861487
'name',
14871488
'url',
14881489
'visibility_search',

Helper/Data.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Algolia\AlgoliaSearch\Exceptions\ExceededRetriesException;
77
use Algolia\AlgoliaSearch\Logger\DiagnosticsLogger;
88
use Algolia\AlgoliaSearch\Service\AdditionalSection\IndexBuilder as AdditionalSectionIndexBuilder;
9+
use Algolia\AlgoliaSearch\Service\AlgoliaConnector;
910
use Algolia\AlgoliaSearch\Service\Category\IndexBuilder as CategoryIndexBuilder;
1011
use Algolia\AlgoliaSearch\Service\IndexNameFetcher;
1112
use Algolia\AlgoliaSearch\Service\Page\IndexBuilder as PageIndexBuilder;
@@ -224,7 +225,7 @@ public function getBaseIndexName(int $storeId = null): string
224225
public function getIndexDataByStoreIds(): array
225226
{
226227
$indexNames = [];
227-
$indexNames[AlgoliaHelper::ALGOLIA_DEFAULT_SCOPE] = $this->buildIndexData();
228+
$indexNames[AlgoliaConnector::ALGOLIA_DEFAULT_SCOPE] = $this->buildIndexData();
228229
foreach ($this->storeManager->getStores() as $store) {
229230
$indexNames[$store->getId()] = $this->buildIndexData($store);
230231
}

0 commit comments

Comments
 (0)