8
8
use Algolia \AlgoliaSearch \Exception \ReplicaLimitExceededException ;
9
9
use Algolia \AlgoliaSearch \Exception \TooManyCustomerGroupsAsReplicasException ;
10
10
use Algolia \AlgoliaSearch \Exceptions \AlgoliaException ;
11
+ use Algolia \AlgoliaSearch \Exceptions \ExceededRetriesException ;
11
12
use Algolia \AlgoliaSearch \Helper \AlgoliaHelper ;
12
13
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
13
14
use Algolia \AlgoliaSearch \Helper \Logger ;
14
15
use Algolia \AlgoliaSearch \Registry \ReplicaState ;
16
+ use Algolia \AlgoliaSearch \Service \IndexNameFetcher ;
15
17
use Algolia \AlgoliaSearch \Validator \VirtualReplicaValidatorFactory ;
16
18
use Magento \Framework \Exception \LocalizedException ;
17
19
use Magento \Framework \Exception \NoSuchEntityException ;
@@ -50,6 +52,7 @@ public function __construct(
50
52
protected AlgoliaHelper $ algoliaHelper ,
51
53
protected ReplicaState $ replicaState ,
52
54
protected VirtualReplicaValidatorFactory $ validatorFactory ,
55
+ protected IndexNameFetcher $ indexNameFetcher ,
53
56
protected Logger $ logger
54
57
)
55
58
{}
@@ -62,7 +65,7 @@ public function __construct(
62
65
* @throws NoSuchEntityException
63
66
* @throws LocalizedException
64
67
*/
65
- protected function hasReplicaConfigurationChanged (string $ primaryIndexName , int $ storeId ): bool
68
+ protected function hasReplicaConfigurationChanged (int $ storeId ): bool
66
69
{
67
70
switch ($ this ->replicaState ->getChangeState ($ storeId )) {
68
71
case ReplicaState::REPLICA_STATE_CHANGED :
@@ -71,14 +74,16 @@ protected function hasReplicaConfigurationChanged(string $primaryIndexName, int
71
74
return false ;
72
75
case ReplicaState::REPLICA_STATE_UNKNOWN :
73
76
default :
77
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
74
78
$ old = $ this ->getMagentoReplicaConfigurationFromAlgolia ($ primaryIndexName );
75
- $ new = $ this ->transformSortingIndicesToReplicaSetting ($ this ->configHelper ->getSortingIndices ($ primaryIndexName , $ storeId ));
79
+ $ new = $ this ->transformSortingIndicesToReplicaSetting ($ this ->configHelper ->getSortingIndices ($ storeId ));
76
80
sort ($ old );
77
81
sort ($ new );
78
82
return $ old !== $ new ;
79
83
}
80
84
}
81
85
86
+
82
87
/**
83
88
* @param $primaryIndexName
84
89
* @param bool $refreshCache
@@ -183,20 +188,19 @@ function ($sort) use ($mode) {
183
188
* In order to avoid interfering with replicas configured directly in the Algolia dashboard,
184
189
* we must know which replica indices are Magento managed and which are not.
185
190
*
186
- * @param string $primaryIndexName
187
191
* @param int $storeId
188
192
* @param bool $refreshCache
189
193
* @return array
190
194
* @throws LocalizedException
191
195
* @throws NoSuchEntityException
192
196
*/
193
- protected function getMagentoReplicaSettingsFromConfig (string $ primaryIndexName , int $ storeId , bool $ refreshCache = false ): array
197
+ protected function getMagentoReplicaSettingsFromConfig (int $ storeId , bool $ refreshCache = false ): array
194
198
{
195
199
if ($ refreshCache || !isset ($ this ->_magentoReplicaPossibleConfig [$ storeId ])) {
196
200
$ sortConfig = $ this ->replicaState ->getChangeState ($ storeId ) === ReplicaState::REPLICA_STATE_CHANGED
197
201
? array_merge ($ this ->replicaState ->getOriginalSortConfiguration ($ storeId ), $ this ->replicaState ->getUpdatedSortConfiguration ($ storeId ))
198
202
: null ;
199
- $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ primaryIndexName , $ storeId , null , $ sortConfig );
203
+ $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ storeId , null , $ sortConfig );
200
204
$ this ->_magentoReplicaPossibleConfig [$ storeId ] = array_merge (
201
205
$ this ->transformSortingIndicesToReplicaSetting ($ sortingIndices , self ::REPLICA_TRANSFORM_MODE_STANDARD ),
202
206
$ this ->transformSortingIndicesToReplicaSetting ($ sortingIndices , self ::REPLICA_TRANSFORM_MODE_VIRTUAL )
@@ -208,27 +212,28 @@ protected function getMagentoReplicaSettingsFromConfig(string $primaryIndexName,
208
212
/**
209
213
* @inheritDoc
210
214
*/
211
- public function syncReplicasToAlgolia (string $ primaryIndexName , int $ storeId , array $ primaryIndexSettings ): void
215
+ public function syncReplicasToAlgolia (int $ storeId , array $ primaryIndexSettings ): void
212
216
{
213
217
if ($ this ->isReplicaSyncEnabled ($ storeId )
214
- && $ this ->hasReplicaConfigurationChanged ($ primaryIndexName , $ storeId )
215
- && $ this ->isReplicaConfigurationValid ($ primaryIndexName , $ storeId )) {
216
- $ addedReplicas = $ this ->setReplicasOnPrimaryIndex ($ primaryIndexName , $ storeId );
217
- $ this ->configureRanking ($ primaryIndexName , $ storeId , $ addedReplicas , $ primaryIndexSettings );
218
+ && $ this ->hasReplicaConfigurationChanged ($ storeId )
219
+ && $ this ->isReplicaConfigurationValid ($ storeId )) {
220
+ $ addedReplicas = $ this ->setReplicasOnPrimaryIndex ($ storeId );
221
+ $ this ->configureRanking ($ storeId , $ addedReplicas , $ primaryIndexSettings );
218
222
}
219
223
}
220
224
221
225
/**
222
- * @param string $indexName
223
226
* @param int $storeId
224
227
* @return string[] Replicas added or modified by this operation
228
+ * @throws AlgoliaException
225
229
* @throws LocalizedException
226
230
* @throws NoSuchEntityException
227
- * @throws AlgoliaException
231
+ * @throws ExceededRetriesException
228
232
*/
229
- protected function setReplicasOnPrimaryIndex (string $ indexName , int $ storeId ): array
233
+ protected function setReplicasOnPrimaryIndex (int $ storeId ): array
230
234
{
231
- $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ indexName , $ storeId );
235
+ $ indexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
236
+ $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ storeId );
232
237
$ newMagentoReplicasSetting = $ this ->transformSortingIndicesToReplicaSetting ($ sortingIndices );
233
238
$ oldMagentoReplicasSetting = $ this ->getMagentoReplicaConfigurationFromAlgolia ($ indexName );
234
239
$ nonMagentoReplicasSetting = $ this ->getNonMagentoReplicaConfigurationFromAlgolia ($ indexName );
@@ -263,16 +268,16 @@ protected function setReplicasOnPrimaryIndex(string $indexName, int $storeId): a
263
268
}
264
269
265
270
/**
266
- * @param string $primaryIndexName
267
271
* @param int $storeId
268
272
* @return bool
269
273
* @throws LocalizedException
270
274
* @throws NoSuchEntityException
271
275
* @throws ReplicaLimitExceededException
276
+ * @throws TooManyCustomerGroupsAsReplicasException
272
277
*/
273
- protected function isReplicaConfigurationValid (string $ primaryIndexName , int $ storeId ): bool
278
+ protected function isReplicaConfigurationValid (int $ storeId ): bool
274
279
{
275
- $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ primaryIndexName , $ storeId );
280
+ $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ storeId );
276
281
$ validator = $ this ->validatorFactory ->create ();
277
282
if (!$ validator ->isReplicaConfigurationValid ($ sortingIndices )) {
278
283
$ postfix = "Please note that there can be no more than " . $ this ->getMaxVirtualReplicasPerIndex () . " virtual replicas per index. " ;
@@ -347,7 +352,6 @@ protected function deleteReplicas(array $replicasToDelete): void
347
352
348
353
/**
349
354
* Apply ranking settings to the added replica indices
350
- * @param string $primaryIndexName
351
355
* @param int $storeId
352
356
* @param string[] $replicas
353
357
* @param array<string, mixed> $primaryIndexSettings
@@ -356,9 +360,9 @@ protected function deleteReplicas(array $replicasToDelete): void
356
360
* @throws LocalizedException
357
361
* @throws NoSuchEntityException
358
362
*/
359
- protected function configureRanking (string $ primaryIndexName , int $ storeId , array $ replicas , array $ primaryIndexSettings ): void
363
+ protected function configureRanking (int $ storeId , array $ replicas , array $ primaryIndexSettings ): void
360
364
{
361
- $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ primaryIndexName , $ storeId );
365
+ $ sortingIndices = $ this ->configHelper ->getSortingIndices ($ storeId );
362
366
$ replicaDetails = array_filter (
363
367
$ sortingIndices ,
364
368
function ($ replica ) use ($ replicas ) {
0 commit comments