You are here

public static function SearchApiAlgoliaCommands::batchProcess in Search API Algolia 3.0.x

Batch API callback; delete objects in algolia.

Parameters

array $object_ids: A batch size.

string $index_name: Algolia index name.

mixed|array $context: The batch current context.

Throws

\AlgoliaSearch\AlgoliaException

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Commands/SearchApiAlgoliaCommands.php, line 135

Class

SearchApiAlgoliaCommands
Class Search Api Algolia commands.

Namespace

Drupal\search_api_algolia\Commands

Code

public static function batchProcess(array $object_ids, $index_name, &$context) {

  // Store Algolia Connection credentials in context if not available.
  if (empty($context['app_id']) || empty($context['app_secret_admin'])) {
    $backend_config_algolia = \Drupal::config('search_api.server.algolia')
      ->get('backend_config');
    $context['app_id'] = $backend_config_algolia['application_id'];
    $context['app_secret_admin'] = $backend_config_algolia['api_key'];
  }

  // Load the Algolia Index.
  $client = SearchClient::create($context['app_id'], $context['app_secret_admin']);
  $index = $client
    ->initIndex($index_name);

  // Delete the objects in bulk.
  $index
    ->deleteObjects($object_ids);

  // Update the count of items processed.
  $context['results']['count'] += count($object_ids);

  // Remove the processed object ids from DB for the current index.
  \Drupal::database()
    ->delete('search_api_algolia_deleted_items')
    ->condition('object_id', $object_ids, 'IN')
    ->condition('index_id', $index_name)
    ->execute();

  // Nice message for user / console.
  $context['message'] = dt('Deleted items @count out of @total.', [
    '@count' => $context['results']['count'],
    '@total' => $context['results']['total'],
  ]);
}