Skip to content

Commit 6a442b6

Browse files
committed
MAGE-1097: remove static methods in AlgoliaConnector
1 parent d61d008 commit 6a442b6

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

Service/AlgoliaConnector.php

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,20 @@ class AlgoliaConnector
5858
/** @var bool */
5959
protected bool $userAgentsAdded = false;
6060

61-
protected static ?string $lastUsedIndexName;
61+
/**
62+
* @var string|null
63+
*/
64+
protected ?string $lastUsedIndexName;
6265

63-
protected static ?string $lastTaskId;
66+
/**
67+
* @var string|null
68+
*/
69+
protected ?string $lastTaskId;
6470

65-
protected static ?array $lastTaskInfoByStore;
71+
/**
72+
* @var array|null
73+
*/
74+
protected ?array $lastTaskInfoByStore;
6675

6776
public function __construct(
6877
protected ConfigHelper $config,
@@ -238,21 +247,23 @@ public function setSettings(
238247

239248
$res = $this->getClient($indexOptions->getStoreId())->setSettings($indexName, $settings, $forwardToReplicas);
240249

241-
self::setLastOperationInfo($indexName, $res, $indexOptions->getStoreId());
250+
$this->setLastOperationInfo($indexOptions, $res);
242251
}
243252

244253
/**
245-
* @param string $indexName
254+
* @param IndexOptionsInterface $indexOptions
246255
* @param array $requests
247-
* @param int|null $storeId
248256
* @return array<string, mixed>
249257
* @throws AlgoliaException
258+
* @throws NoSuchEntityException
250259
*/
251-
protected function performBatchOperation(string $indexName, array $requests, ?int $storeId = null): array
260+
protected function performBatchOperation(IndexOptionsInterface $indexOptions, array $requests): array
252261
{
253-
$response = $this->getClient($storeId)->batch($indexName, [ 'requests' => $requests ] );
262+
$indexName = $this->getIndexName($indexOptions);
254263

255-
self::setLastOperationInfo($indexName, $response, $storeId);
264+
$response = $this->getClient($indexOptions->getStoreId())->batch($indexName, [ 'requests' => $requests ] );
265+
266+
$this->setLastOperationInfo($indexOptions, $response);
256267

257268
return $response;
258269
}
@@ -268,14 +279,14 @@ public function deleteIndex(IndexOptionsInterface $indexOptions): void
268279

269280
$res = $this->getClient($indexOptions->getStoreId())->deleteIndex($indexName);
270281

271-
self::setLastOperationInfo($indexName, $res, $indexOptions->getStoreId());
282+
$this->setLastOperationInfo($indexOptions, $res);
272283
}
273284

274285
/**
275286
* @param array $ids
276287
* @param IndexOptionsInterface $indexOptions
277288
* @return void
278-
* @throws AlgoliaException
289+
* @throws AlgoliaException|NoSuchEntityException
279290
*/
280291
public function deleteObjects(array $ids, IndexOptionsInterface $indexOptions): void
281292
{
@@ -293,9 +304,7 @@ function ($id) {
293304
)
294305
);
295306

296-
$indexName = $this->getIndexName($indexOptions);
297-
298-
$this->performBatchOperation($indexName, $requests, $indexOptions->getStoreId());
307+
$this->performBatchOperation($indexOptions, $requests);
299308
}
300309

301310
/**
@@ -317,7 +326,7 @@ public function moveIndex(IndexOptions $fromIndexOptions, IndexOptions $toIndexO
317326
'destination' => $toIndexName
318327
]
319328
);
320-
self::setLastOperationInfo($toIndexName, $response, $toIndexOptions->getStoreId());
329+
$this->setLastOperationInfo($toIndexOptions, $response);
321330
}
322331

323332
/**
@@ -464,22 +473,25 @@ function ($object) use ($action) {
464473
)
465474
);
466475

467-
$this->performBatchOperation($indexName, $requests, $indexOptions->getStoreId());
476+
$this->performBatchOperation($indexOptions, $requests);
468477
}
469478

470479
/**
471-
* @param string $indexName
480+
* @param IndexOptionsInterface $indexOptions
472481
* @param array $response
473-
* @param int|null $storeId
474482
* @return void
483+
* @throws NoSuchEntityException
475484
*/
476-
protected static function setLastOperationInfo(string $indexName, array $response, ?int $storeId = null): void
485+
protected function setLastOperationInfo(IndexOptionsInterface $indexOptions, array $response): void
477486
{
478-
self::$lastUsedIndexName = $indexName;
479-
self::$lastTaskId = $response[self::ALGOLIA_API_TASK_ID] ?? null;
487+
$indexName = $this->getIndexName($indexOptions);
488+
$storeId = $indexOptions->getStoreId();
489+
490+
$this->lastUsedIndexName = $indexName;
491+
$this->lastTaskId = $response[self::ALGOLIA_API_TASK_ID] ?? null;
480492

481493
if (!is_null($storeId)) {
482-
self::$lastTaskInfoByStore[$storeId] = [
494+
$this->lastTaskInfoByStore[$storeId] = [
483495
'indexName' => $indexName,
484496
'taskId' => $response[self::ALGOLIA_API_TASK_ID] ?? null
485497
];
@@ -505,7 +517,7 @@ public function saveRule(array $rule, IndexOptionsInterface $indexOptions, bool
505517
$forwardToReplicas
506518
);
507519

508-
self::setLastOperationInfo($indexName, $res, $indexOptions->getStoreId());
520+
$this->setLastOperationInfo($indexOptions, $res);
509521
}
510522

511523
/**
@@ -522,7 +534,7 @@ public function saveRules(IndexOptionsInterface $indexOptions, array $rules, boo
522534

523535
$res = $this->getClient($indexOptions->getStoreId())->saveRules($indexName, $rules, $forwardToReplicas);
524536

525-
self::setLastOperationInfo($indexName, $res, $indexOptions->getStoreId());
537+
$this->setLastOperationInfo($indexOptions, $res);
526538
}
527539

528540

@@ -544,7 +556,7 @@ public function deleteRule(
544556

545557
$res = $this->getClient($indexOptions->getStoreId())->deleteRule($indexName, $objectID, $forwardToReplicas);
546558

547-
self::setLastOperationInfo($indexName, $res, $indexOptions->getStoreId());
559+
$this->setLastOperationInfo($indexOptions, $res);
548560
}
549561

550562
/**
@@ -567,7 +579,7 @@ public function copySynonyms(IndexOptionsInterface $fromIndexOptions, IndexOptio
567579
'scope' => ['synonyms']
568580
]
569581
);
570-
self::setLastOperationInfo($fromIndexName, $response, $toIndexOptions->getStoreId());
582+
$this->setLastOperationInfo($fromIndexOptions, $response);
571583
}
572584

573585
/**
@@ -590,7 +602,7 @@ public function copyQueryRules(IndexOptionsInterface $fromIndexOptions, IndexOpt
590602
'scope' => ['rules']
591603
]
592604
);
593-
self::setLastOperationInfo($fromIndexName, $response, $toIndexOptions->getStoreId());
605+
$this->setLastOperationInfo($fromIndexOptions, $response);
594606
}
595607

596608
/**
@@ -620,7 +632,7 @@ public function clearIndex(IndexOptionsInterface $indexOptions): void
620632

621633
$res = $this->getClient($indexOptions->getStoreId())->clearObjects($indexName);
622634

623-
self::setLastOperationInfo($indexName, $res, $indexOptions->getStoreId());
635+
$this->setLastOperationInfo($indexOptions, $res);
624636
}
625637

626638
/**
@@ -632,18 +644,18 @@ public function clearIndex(IndexOptionsInterface $indexOptions): void
632644
public function waitLastTask(?int $storeId = null, ?string $lastUsedIndexName = null, ?int $lastTaskId = null): void
633645
{
634646
if (is_null($lastUsedIndexName)) {
635-
if (!is_null($storeId) && isset(self::$lastTaskInfoByStore[$storeId])) {
636-
$lastUsedIndexName = self::$lastTaskInfoByStore[$storeId]['indexName'];
637-
} elseif (isset(self::$lastUsedIndexName)){
638-
$lastUsedIndexName = self::$lastUsedIndexName;
647+
if (!is_null($storeId) && isset($this->lastTaskInfoByStore[$storeId])) {
648+
$lastUsedIndexName = $this->lastTaskInfoByStore[$storeId]['indexName'];
649+
} elseif (isset($this->lastUsedIndexName)){
650+
$lastUsedIndexName = $this->lastUsedIndexName;
639651
}
640652
}
641653

642654
if (is_null($lastTaskId)) {
643-
if (!is_null($storeId) && isset(self::$lastTaskInfoByStore[$storeId])) {
644-
$lastTaskId = self::$lastTaskInfoByStore[$storeId]['taskId'];
645-
} elseif (isset(self::$lastTaskId)){
646-
$lastTaskId = self::$lastTaskId;
655+
if (!is_null($storeId) && isset($this->lastTaskInfoByStore[$storeId])) {
656+
$lastTaskId = $this->lastTaskInfoByStore[$storeId]['taskId'];
657+
} elseif (isset($this->lastTaskId)){
658+
$lastTaskId = $this->lastTaskId;
647659
}
648660
}
649661

@@ -883,10 +895,10 @@ public function getLastTaskId(?int $storeId = null): int|null
883895
{
884896
$lastTaskId = null;
885897

886-
if (!is_null($storeId) && isset(self::$lastTaskInfoByStore[$storeId])) {
887-
$lastTaskId = self::$lastTaskInfoByStore[$storeId]['taskId'];
888-
} elseif (isset(self::$lastTaskId)){
889-
$lastTaskId = self::$lastTaskId;
898+
if (!is_null($storeId) && isset($this->lastTaskInfoByStore[$storeId])) {
899+
$lastTaskId = $this->lastTaskInfoByStore[$storeId]['taskId'];
900+
} elseif (isset($this->lastTaskId)){
901+
$lastTaskId = $this->lastTaskId;
890902
}
891903

892904
return $lastTaskId;

0 commit comments

Comments
 (0)