Skip to content

Commit

Permalink
refactor: perf improvement (prevent twice settings pulling)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mustafa Şükrü Kapusuz committed May 19, 2023
1 parent 24f6bdf commit 34711b4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions includes/indices/class-algolia-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* @since 1.0.0
*/
abstract class Algolia_Index {
const AC_INDEX_NOT_EXISTS_EXCEPTION_MSG = 'Index does not exist';

/**
* Default index settings
*
Expand Down
22 changes: 17 additions & 5 deletions includes/indices/settings/class-algolia-primary-index-settings.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\SearchIndex;
use WebDevStudios\WPSWA\Algolia\AlgoliaSearch\Exceptions\NotFoundException;

class Algolia_Primary_Index_Settings implements Algolia_Index_Settings {
protected Algolia_Index $index;
Expand All @@ -27,12 +28,23 @@ public function get_local_settings(): array {
return $this->get_index()->get_default_settings();
}

/**
* If the Indice is not found, return empty settings. In that case, a new index is created.
*
* @throws Exception Varius exceptions can be thrown by Algolia Client except for NotFoundException.
* @return array
*/
public function get_remote_settings(): array {
if ( ! $this->get_index()->exists() ) {
return [];
}
try {
return $this->get_algolia_index()->getSettings();
} catch ( NotFoundException $e ) {
// being more strict, catch only index does not exists case.
if ( $e->getMessage() === Algolia_Index::AC_INDEX_NOT_EXISTS_EXCEPTION_MSG ) {
return [];
}

return $this->get_algolia_index()->getSettings();
throw $e;
}
}

public function get_settings_needs_sync(): array {
Expand Down Expand Up @@ -63,7 +75,7 @@ public function push( $overrides = [] ): bool {
}

try {
$this->get_algolia_index()->setSettings( $settings );
$this->get_algolia_index()->setSettings( $settings ); // Creates new indice if doesn't exist.
} catch ( Exception $e ) {
return false;
}
Expand Down

0 comments on commit 34711b4

Please sign in to comment.