@@ -85,8 +85,7 @@ protected function hasReplicaConfigurationChanged(int $storeId): bool
85
85
return false ;
86
86
case ReplicaState::REPLICA_STATE_UNKNOWN :
87
87
default :
88
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
89
- $ old = $ this ->getMagentoReplicaConfigurationFromAlgolia ($ primaryIndexName , $ storeId );
88
+ $ old = $ this ->getMagentoReplicaConfigurationFromAlgolia ($ storeId );
90
89
$ new = $ this ->sortingTransformer ->transformSortingIndicesToReplicaSetting (
91
90
$ this ->sortingTransformer ->getSortingIndices ($ storeId , null , null , true )
92
91
);
@@ -98,14 +97,19 @@ protected function hasReplicaConfigurationChanged(int $storeId): bool
98
97
99
98
100
99
/**
101
- * @param $primaryIndexName
102
- * @param int|null $storeId
100
+ * @param int $storeId
103
101
* @param bool $refreshCache
102
+ * @param string|null $primaryIndexName
104
103
* @return string[]
105
104
* @throws LocalizedException
105
+ * @throws NoSuchEntityException
106
106
*/
107
- protected function getReplicaConfigurationFromAlgolia ($ primaryIndexName , int $ storeId = null , bool $ refreshCache = false ): array
107
+ protected function getReplicaConfigurationFromAlgolia (
108
+ int $ storeId ,
109
+ bool $ refreshCache = false ,
110
+ ?string $ primaryIndexName = null ): array
108
111
{
112
+ $ primaryIndexName ??= $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
109
113
if ($ refreshCache || !isset ($ this ->_algoliaReplicaConfig [$ primaryIndexName ])) {
110
114
try {
111
115
$ currentSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName , $ storeId );
@@ -134,20 +138,19 @@ protected function clearAlgoliaReplicaSettingCache($primaryIndexName = null): vo
134
138
* Obtain the replica configuration from Algolia but only those indices that are
135
139
* relevant to the Magento integration
136
140
*
137
- * @param string $primaryIndexName
138
- * @param int|null $storeId
141
+ * @param int $storeId
139
142
* @param bool $refreshCache
140
143
* @return string[] Array of replica index names
141
144
* @throws LocalizedException
142
145
* @throws NoSuchEntityException
143
146
*/
144
147
protected function getMagentoReplicaConfigurationFromAlgolia (
145
- string $ primaryIndexName ,
146
- int $ storeId = null ,
148
+ int $ storeId ,
147
149
bool $ refreshCache = false
148
150
): array
149
151
{
150
- $ algoliaReplicas = $ this ->getReplicaConfigurationFromAlgolia ($ primaryIndexName , $ storeId , $ refreshCache );
152
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
153
+ $ algoliaReplicas = $ this ->getReplicaConfigurationFromAlgolia ($ storeId , $ refreshCache , $ primaryIndexName );
151
154
$ magentoReplicas = $ this ->getMagentoReplicaSettings ($ primaryIndexName , $ algoliaReplicas );
152
155
return array_values (array_intersect ($ magentoReplicas , $ algoliaReplicas ));
153
156
}
@@ -185,15 +188,15 @@ protected function isMagentoReplicaIndex(string $replicaIndexName, int|string $s
185
188
}
186
189
187
190
/**
188
- * @param string $primaryIndexName
189
- * @param int|null $storeId
191
+ * @param int $storeId
190
192
* @return array
191
193
* @throws LocalizedException
192
194
* @throws NoSuchEntityException
193
195
*/
194
- protected function getNonMagentoReplicaConfigurationFromAlgolia (string $ primaryIndexName , int $ storeId = null ): array
196
+ protected function getNonMagentoReplicaConfigurationFromAlgolia (int $ storeId ): array
195
197
{
196
- $ algoliaReplicas = $ this ->getReplicaConfigurationFromAlgolia ($ primaryIndexName , $ storeId );
198
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
199
+ $ algoliaReplicas = $ this ->getReplicaConfigurationFromAlgolia ($ storeId );
197
200
$ magentoReplicas = $ this ->getMagentoReplicaSettings ($ primaryIndexName , $ algoliaReplicas );
198
201
return array_diff ($ algoliaReplicas , $ magentoReplicas );
199
202
}
@@ -252,8 +255,8 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
252
255
$ indexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
253
256
$ sortingIndices = $ this ->sortingTransformer ->getSortingIndices ($ storeId );
254
257
$ newMagentoReplicasSetting = $ this ->sortingTransformer ->transformSortingIndicesToReplicaSetting ($ sortingIndices );
255
- $ oldMagentoReplicasSetting = $ this ->getMagentoReplicaConfigurationFromAlgolia ($ indexName , $ storeId , true );
256
- $ nonMagentoReplicasSetting = $ this ->getNonMagentoReplicaConfigurationFromAlgolia ($ indexName , $ storeId );
258
+ $ oldMagentoReplicasSetting = $ this ->getMagentoReplicaConfigurationFromAlgolia ($ storeId , true );
259
+ $ nonMagentoReplicasSetting = $ this ->getNonMagentoReplicaConfigurationFromAlgolia ($ storeId );
257
260
$ oldMagentoReplicaIndices = $ this ->getBareIndexNamesFromReplicaSetting ($ oldMagentoReplicasSetting );
258
261
$ newMagentoReplicaIndices = $ this ->getBareIndexNamesFromReplicaSetting ($ newMagentoReplicasSetting );
259
262
@@ -273,7 +276,7 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
273
276
$ setReplicasTaskId = $ this ->algoliaHelper ->getLastTaskId ($ storeId );
274
277
$ this ->algoliaHelper ->waitLastTask ($ storeId , $ indexName , $ setReplicasTaskId );
275
278
$ this ->clearAlgoliaReplicaSettingCache ($ indexName );
276
- $ this ->deleteReplicas ($ replicasToDelete , false , false , $ storeId );
279
+ $ this ->deleteReplicas ($ storeId , $ replicasToDelete , false , false );
277
280
278
281
if (self ::_DEBUG ) {
279
282
$ this ->logger ->log (
@@ -369,23 +372,23 @@ protected function getBareIndexNameFromReplicaSetting(string $replicaSetting): s
369
372
/**
370
373
* Delete replica indices
371
374
*
375
+ * @param int $storeId
372
376
* @param array $replicasToDelete - which replicas to delete
373
377
* @param bool $waitLastTask - wait until deleting next replica (default: false)
374
378
* @param bool $safeMode - ensure replica is not attached to a primary index before attempting to delete (default: false)
375
- * @param int|null $storeId
376
379
* @return void
377
380
* @throws AlgoliaException
378
381
* @throws ExceededRetriesException
379
382
* @throws NoSuchEntityException
380
383
*/
381
384
protected function deleteReplicas (
385
+ int $ storeId ,
382
386
array $ replicasToDelete ,
383
387
bool $ waitLastTask = false ,
384
- bool $ safeMode = false ,
385
- ?int $ storeId = null ): void
388
+ bool $ safeMode = false ): void
386
389
{
387
390
foreach ($ replicasToDelete as $ deletedReplica ) {
388
- $ this ->deleteReplica ($ deletedReplica , $ safeMode , $ storeId );
391
+ $ this ->deleteReplica ($ storeId , $ deletedReplica , $ safeMode );
389
392
if ($ waitLastTask ) {
390
393
$ this ->algoliaHelper ->waitLastTask ($ storeId , $ deletedReplica );
391
394
}
@@ -397,12 +400,12 @@ protected function deleteReplicas(
397
400
* @throws ExceededRetriesException
398
401
* @throws NoSuchEntityException
399
402
*/
400
- protected function deleteReplica (string $ replicaIndexName , bool $ safeMode = false , ? int $ storeId = null ): void
403
+ protected function deleteReplica (int $ storeId , string $ replicaIndexName , bool $ safeMode = false ): void
401
404
{
402
405
if ($ safeMode ) {
403
406
$ settings = $ this ->algoliaHelper ->getSettings ($ replicaIndexName , $ storeId );
404
407
if (isset ($ settings [self ::ALGOLIA_SETTINGS_KEY_PRIMARY ])) {
405
- $ this ->detachReplica ($ settings [ self :: ALGOLIA_SETTINGS_KEY_PRIMARY ] , $ replicaIndexName , $ storeId );
408
+ $ this ->detachReplica ($ storeId , $ replicaIndexName , $ settings [ self :: ALGOLIA_SETTINGS_KEY_PRIMARY ] );
406
409
}
407
410
}
408
411
@@ -416,8 +419,10 @@ protected function deleteReplica(string $replicaIndexName, bool $safeMode = fals
416
419
* @throws AlgoliaException
417
420
* @throws NoSuchEntityException
418
421
*/
419
- protected function detachReplica (string $ primaryIndexName , string $ replicaIndexName , ?int $ storeId = null ): void
422
+ protected function detachReplica (int $ storeId , string $ replicaIndexName , ?string $ primaryIndexName = null ): void
420
423
{
424
+ $ primaryIndexName ??= $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
425
+
421
426
$ settings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName , $ storeId );
422
427
if (!isset ($ settings [self ::ALGOLIA_SETTINGS_KEY_REPLICAS ])) {
423
428
return ;
@@ -522,8 +527,9 @@ public function getMaxVirtualReplicasPerIndex() : int
522
527
/**
523
528
* @throws AlgoliaException
524
529
*/
525
- protected function clearReplicasSettingInAlgolia (string $ primaryIndexName , int $ storeId ): void
530
+ protected function clearReplicasSettingInAlgolia (int $ storeId ): void
526
531
{
532
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
527
533
$ this ->algoliaHelper ->setSettings (
528
534
$ primaryIndexName ,
529
535
[ self ::ALGOLIA_SETTINGS_KEY_REPLICAS => []],
@@ -544,12 +550,11 @@ public function deleteReplicasFromAlgolia(int $storeId, bool $unused = false): v
544
550
if ($ unused ) {
545
551
$ replicasToDelete = $ this ->getUnusedReplicaIndices ($ storeId );
546
552
} else {
547
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
548
- $ replicasToDelete = $ this ->getMagentoReplicaIndicesFromAlgolia ($ primaryIndexName );
549
- $ this ->clearReplicasSettingInAlgolia ($ primaryIndexName , $ storeId );
553
+ $ replicasToDelete = $ this ->getMagentoReplicaIndicesFromAlgolia ($ storeId );
554
+ $ this ->clearReplicasSettingInAlgolia ($ storeId );
550
555
}
551
556
552
- $ this ->deleteReplicas ($ replicasToDelete , true , true , $ storeId );
557
+ $ this ->deleteReplicas ($ storeId , $ replicasToDelete , true , true );
553
558
554
559
if ($ unused ) {
555
560
$ this ->clearUnusedReplicaIndicesCache ($ storeId );
@@ -559,9 +564,9 @@ public function deleteReplicasFromAlgolia(int $storeId, bool $unused = false): v
559
564
/**
560
565
* @throws LocalizedException
561
566
*/
562
- protected function getMagentoReplicaIndicesFromAlgolia (string $ primaryIndexName , $ storeId = null ): array
567
+ protected function getMagentoReplicaIndicesFromAlgolia (int $ storeId ): array
563
568
{
564
- return $ this ->getBareIndexNamesFromReplicaSetting ($ this ->getMagentoReplicaConfigurationFromAlgolia ($ primaryIndexName , $ storeId ));
569
+ return $ this ->getBareIndexNamesFromReplicaSetting ($ this ->getMagentoReplicaConfigurationFromAlgolia ($ storeId ));
565
570
}
566
571
567
572
/**
@@ -571,7 +576,7 @@ public function getUnusedReplicaIndices(int $storeId): array
571
576
{
572
577
$ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
573
578
if (!isset ($ this ->_unusedReplicaIndices [$ storeId ])) {
574
- $ currentReplicas = $ this ->getMagentoReplicaIndicesFromAlgolia ($ primaryIndexName );
579
+ $ currentReplicas = $ this ->getMagentoReplicaIndicesFromAlgolia ($ storeId );
575
580
$ unusedReplicas = [];
576
581
$ allIndices = $ this ->algoliaHelper ->listIndexes ($ storeId );
577
582
@@ -613,10 +618,9 @@ protected function getAllReplicaIndices(): array
613
618
$ replicaIndices = [];
614
619
$ storeIds = array_keys ($ this ->storeManager ->getStores ());
615
620
foreach ($ storeIds as $ storeId ) {
616
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
617
621
$ replicaIndices = array_merge (
618
622
$ replicaIndices ,
619
- $ this ->getMagentoReplicaIndicesFromAlgolia ($ primaryIndexName )
623
+ $ this ->getMagentoReplicaIndicesFromAlgolia ($ storeId )
620
624
);
621
625
}
622
626
return array_unique ($ replicaIndices );
0 commit comments