9
9
use Algolia \AlgoliaSearch \Exception \TooManyCustomerGroupsAsReplicasException ;
10
10
use Algolia \AlgoliaSearch \Exceptions \AlgoliaException ;
11
11
use Algolia \AlgoliaSearch \Exceptions \ExceededRetriesException ;
12
- use Algolia \AlgoliaSearch \Helper \AlgoliaHelper ;
13
12
use Algolia \AlgoliaSearch \Helper \ConfigHelper ;
14
13
use Algolia \AlgoliaSearch \Logger \DiagnosticsLogger ;
15
14
use Algolia \AlgoliaSearch \Registry \ReplicaState ;
15
+ use Algolia \AlgoliaSearch \Service \AlgoliaConnector ;
16
16
use Algolia \AlgoliaSearch \Service \IndexNameFetcher ;
17
17
use Algolia \AlgoliaSearch \Service \StoreNameFetcher ;
18
18
use Algolia \AlgoliaSearch \Validator \VirtualReplicaValidatorFactory ;
@@ -57,7 +57,8 @@ class ReplicaManager implements ReplicaManagerInterface
57
57
58
58
public function __construct (
59
59
protected ConfigHelper $ configHelper ,
60
- protected AlgoliaHelper $ algoliaHelper ,
60
+ protected AlgoliaConnector $ algoliaConnector ,
61
+ protected IndexOptionsBuilder $ indexOptionsBuilder ,
61
62
protected ReplicaState $ replicaState ,
62
63
protected VirtualReplicaValidatorFactory $ validatorFactory ,
63
64
protected IndexNameFetcher $ indexNameFetcher ,
@@ -99,20 +100,18 @@ protected function hasReplicaConfigurationChanged(int $storeId): bool
99
100
/**
100
101
* @param int $storeId
101
102
* @param bool $refreshCache
102
- * @param string|null $primaryIndexName
103
103
* @return string[]
104
104
* @throws LocalizedException
105
105
* @throws NoSuchEntityException
106
106
*/
107
107
protected function getReplicaConfigurationFromAlgolia (
108
108
int $ storeId ,
109
- bool $ refreshCache = false ,
110
- ?string $ primaryIndexName = null ): array
109
+ bool $ refreshCache = false ): array
111
110
{
112
- $ primaryIndexName ?? = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
111
+ $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
113
112
if ($ refreshCache || !isset ($ this ->_algoliaReplicaConfig [$ primaryIndexName ])) {
114
113
try {
115
- $ currentSettings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName , $ storeId );
114
+ $ currentSettings = $ this ->algoliaConnector ->getSettings ($ this -> indexOptionsBuilder -> buildEntityIndexOptions ( $ storeId) );
116
115
$ this ->_algoliaReplicaConfig [$ primaryIndexName ] = array_key_exists (self ::ALGOLIA_SETTINGS_KEY_REPLICAS , $ currentSettings )
117
116
? $ currentSettings [self ::ALGOLIA_SETTINGS_KEY_REPLICAS ]
118
117
: [];
@@ -143,15 +142,18 @@ protected function clearAlgoliaReplicaSettingCache($primaryIndexName = null): vo
143
142
* @return string[] Array of replica index names
144
143
* @throws LocalizedException
145
144
* @throws NoSuchEntityException
145
+ * @throws AlgoliaException
146
146
*/
147
147
protected function getMagentoReplicaConfigurationFromAlgolia (
148
148
int $ storeId ,
149
149
bool $ refreshCache = false
150
150
): array
151
151
{
152
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
153
- $ algoliaReplicas = $ this ->getReplicaConfigurationFromAlgolia ($ storeId , $ refreshCache , $ primaryIndexName );
154
- $ magentoReplicas = $ this ->getMagentoReplicaSettings ($ primaryIndexName , $ algoliaReplicas );
152
+ $ algoliaReplicas = $ this ->getReplicaConfigurationFromAlgolia ($ storeId , $ refreshCache );
153
+ $ magentoReplicas = $ this ->getMagentoReplicaSettings (
154
+ $ this ->indexNameFetcher ->getProductIndexName ($ storeId ),
155
+ $ algoliaReplicas
156
+ );
155
157
return array_values (array_intersect ($ magentoReplicas , $ algoliaReplicas ));
156
158
}
157
159
@@ -265,16 +267,12 @@ protected function setReplicasOnPrimaryIndex(int $storeId): array
265
267
$ replicasToRank = $ this ->getBareIndexNamesFromReplicaSetting (array_diff ($ newMagentoReplicasSetting , $ oldMagentoReplicasSetting ));
266
268
$ replicasToUpdate = array_diff ($ replicasToRank , $ replicasToAdd );
267
269
268
- $ this ->algoliaHelper ->setSettings (
269
- $ indexName ,
270
- [self ::ALGOLIA_SETTINGS_KEY_REPLICAS => array_merge ($ newMagentoReplicasSetting , $ nonMagentoReplicasSetting )],
271
- false ,
272
- false ,
273
- '' ,
274
- $ storeId
270
+ $ this ->algoliaConnector ->setSettings (
271
+ $ this ->indexOptionsBuilder ->buildEntityIndexOptions ($ storeId ),
272
+ [self ::ALGOLIA_SETTINGS_KEY_REPLICAS => array_merge ($ newMagentoReplicasSetting , $ nonMagentoReplicasSetting )]
275
273
);
276
- $ setReplicasTaskId = $ this ->algoliaHelper ->getLastTaskId ($ storeId );
277
- $ this ->algoliaHelper ->waitLastTask ($ storeId , $ indexName , $ setReplicasTaskId );
274
+ $ setReplicasTaskId = $ this ->algoliaConnector ->getLastTaskId ($ storeId );
275
+ $ this ->algoliaConnector ->waitLastTask ($ storeId , $ indexName , $ setReplicasTaskId );
278
276
$ this ->clearAlgoliaReplicaSettingCache ($ indexName );
279
277
$ this ->deleteReplicas ($ storeId , $ replicasToDelete , false , false );
280
278
@@ -390,7 +388,7 @@ protected function deleteReplicas(
390
388
foreach ($ replicasToDelete as $ deletedReplica ) {
391
389
$ this ->deleteReplica ($ storeId , $ deletedReplica , $ safeMode );
392
390
if ($ waitLastTask ) {
393
- $ this ->algoliaHelper ->waitLastTask ($ storeId , $ deletedReplica );
391
+ $ this ->algoliaConnector ->waitLastTask ($ storeId , $ deletedReplica );
394
392
}
395
393
}
396
394
}
@@ -402,14 +400,15 @@ protected function deleteReplicas(
402
400
*/
403
401
protected function deleteReplica (int $ storeId , string $ replicaIndexName , bool $ safeMode = false ): void
404
402
{
403
+ $ indexOptions = $ this ->indexOptionsBuilder ->buildWithEnforcedIndex ($ replicaIndexName , $ storeId );
405
404
if ($ safeMode ) {
406
- $ settings = $ this ->algoliaHelper ->getSettings ($ replicaIndexName , $ storeId );
405
+ $ settings = $ this ->algoliaConnector ->getSettings ($ indexOptions );
407
406
if (isset ($ settings [self ::ALGOLIA_SETTINGS_KEY_PRIMARY ])) {
408
- $ this ->detachReplica ($ storeId , $ replicaIndexName, $ settings [ self :: ALGOLIA_SETTINGS_KEY_PRIMARY ] );
407
+ $ this ->detachReplica ($ storeId , $ replicaIndexName );
409
408
}
410
409
}
411
410
412
- $ this ->algoliaHelper ->deleteIndex ($ replicaIndexName , $ storeId );
411
+ $ this ->algoliaConnector ->deleteIndex ($ indexOptions );
413
412
}
414
413
415
414
/**
@@ -419,24 +418,20 @@ protected function deleteReplica(int $storeId, string $replicaIndexName, bool $s
419
418
* @throws AlgoliaException
420
419
* @throws NoSuchEntityException
421
420
*/
422
- protected function detachReplica (int $ storeId , string $ replicaIndexName, ? string $ primaryIndexName = null ): void
421
+ protected function detachReplica (int $ storeId , string $ replicaIndexName ): void
423
422
{
424
- $ primaryIndexName ?? = $ this ->indexNameFetcher -> getProductIndexName ($ storeId );
423
+ $ indexOptions = $ this ->indexOptionsBuilder -> buildEntityIndexOptions ($ storeId );
425
424
426
- $ settings = $ this ->algoliaHelper ->getSettings ($ primaryIndexName , $ storeId );
425
+ $ settings = $ this ->algoliaConnector ->getSettings ($ indexOptions );
427
426
if (!isset ($ settings [self ::ALGOLIA_SETTINGS_KEY_REPLICAS ])) {
428
427
return ;
429
428
}
430
- $ newReplicas = $ this ->removeReplicaFromReplicaSetting ($ settings [self ::ALGOLIA_SETTINGS_KEY_REPLICAS ], $ replicaIndexName );
431
- $ this ->algoliaHelper ->setSettings (
432
- $ primaryIndexName ,
433
- [ self ::ALGOLIA_SETTINGS_KEY_REPLICAS => $ newReplicas ],
434
- false ,
435
- false ,
436
- '' ,
437
- $ storeId
429
+ $ newReplicas = $ this ->removeReplicaFromReplicaSetting (
430
+ $ settings [self ::ALGOLIA_SETTINGS_KEY_REPLICAS ],
431
+ $ replicaIndexName
438
432
);
439
- $ this ->algoliaHelper ->waitLastTask ($ storeId , $ primaryIndexName );
433
+ $ this ->algoliaConnector ->setSettings ($ indexOptions , [ self ::ALGOLIA_SETTINGS_KEY_REPLICAS => $ newReplicas ]);
434
+ $ this ->algoliaConnector ->waitLastTask ($ storeId );
440
435
}
441
436
442
437
/**
@@ -478,32 +473,18 @@ function($replica) use ($replicas) {
478
473
}
479
474
);
480
475
foreach ($ replicaDetails as $ replica ) {
481
- $ replicaName = $ replica ['name ' ];
476
+ $ indexOptions = $ this -> indexOptionsBuilder -> buildWithEnforcedIndex ( $ replica ['name ' ], $ storeId ) ;
482
477
// Virtual replicas - relevant sort
483
478
if (!empty ($ replica [self ::SORT_KEY_VIRTUAL_REPLICA ])) {
484
479
$ customRanking = array_key_exists ('customRanking ' , $ primaryIndexSettings )
485
480
? $ primaryIndexSettings ['customRanking ' ]
486
481
: [];
487
482
array_unshift ($ customRanking , $ replica ['ranking ' ][0 ]);
488
- $ this ->algoliaHelper ->setSettings (
489
- $ replicaName ,
490
- [ 'customRanking ' => $ customRanking ],
491
- false ,
492
- false ,
493
- '' ,
494
- $ storeId
495
- );
483
+ $ this ->algoliaConnector ->setSettings ($ indexOptions , [ 'customRanking ' => $ customRanking ]);
496
484
// Standard replicas - exhaustive sort
497
485
} else {
498
486
$ primaryIndexSettings ['ranking ' ] = $ replica ['ranking ' ];
499
- $ this ->algoliaHelper ->setSettings (
500
- $ replicaName ,
501
- $ primaryIndexSettings ,
502
- false ,
503
- false ,
504
- '' ,
505
- $ storeId
506
- );
487
+ $ this ->algoliaConnector ->setSettings ($ indexOptions , $ primaryIndexSettings );
507
488
}
508
489
}
509
490
}
@@ -526,20 +507,13 @@ public function getMaxVirtualReplicasPerIndex() : int
526
507
527
508
/**
528
509
* @throws AlgoliaException
510
+ * @throws NoSuchEntityException
529
511
*/
530
512
protected function clearReplicasSettingInAlgolia (int $ storeId ): void
531
513
{
532
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
533
- $ this ->algoliaHelper ->setSettings (
534
- $ primaryIndexName ,
535
- [ self ::ALGOLIA_SETTINGS_KEY_REPLICAS => []],
536
- false ,
537
- false ,
538
- '' ,
539
- $ storeId
540
- )
541
- ;
542
- $ this ->algoliaHelper ->waitLastTask ($ storeId , $ primaryIndexName );
514
+ $ indexOptions = $ this ->indexOptionsBuilder ->buildEntityIndexOptions ($ storeId );
515
+ $ this ->algoliaConnector ->setSettings ($ indexOptions , [ self ::ALGOLIA_SETTINGS_KEY_REPLICAS => []]);
516
+ $ this ->algoliaConnector ->waitLastTask ($ storeId );
543
517
}
544
518
545
519
/**
@@ -578,7 +552,7 @@ public function getUnusedReplicaIndices(int $storeId): array
578
552
if (!isset ($ this ->_unusedReplicaIndices [$ storeId ])) {
579
553
$ currentReplicas = $ this ->getMagentoReplicaIndicesFromAlgolia ($ storeId );
580
554
$ unusedReplicas = [];
581
- $ allIndices = $ this ->algoliaHelper ->listIndexes ($ storeId );
555
+ $ allIndices = $ this ->algoliaConnector ->listIndexes ($ storeId );
582
556
583
557
foreach ($ allIndices ['items ' ] as $ indexInfo ) {
584
558
$ indexName = $ indexInfo ['name ' ];
0 commit comments