You are here

public function SearchApiAlgoliaCommands::deleteFromAlgolia in Search API Algolia 3.0.x

Delete multiple objects from algolia.

@command search_api_algolia:delete

@aliases sapia-d

@option batch-size The number of items to check per batch run.

@usage drush sapia-d Fetch and delete objects in algolia. @usage drush sapia-d --batch-size=100 Fetch and delete objects in algolia with batch of 100.

Parameters

array $options: (optional) An array of options.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Commands/SearchApiAlgoliaCommands.php, line 68

Class

SearchApiAlgoliaCommands
Class Search Api Algolia commands.

Namespace

Drupal\search_api_algolia\Commands

Code

public function deleteFromAlgolia(array $options = [
  'batch-size' => NULL,
]) {
  $batch_size = $options['batch-size'] ?? 100;
  $batch = [
    'finished' => [
      __CLASS__,
      'batchFinish',
    ],
    'title' => dt('Deleting multiple Objects algolia'),
    'init_message' => dt('Starting objects deleting...'),
    'progress_message' => dt('Completed @current step of @total.'),
    'error_message' => dt('encountered error while deleting objects.'),
  ];
  $items = $this->connection
    ->select('search_api_algolia_deleted_items', 'sapi')
    ->fields('sapi', [
    'index_id',
    'object_id',
  ])
    ->execute()
    ->fetchAll();
  if (empty($items)) {
    $this->drupalLogger
      ->notice('No items left to process.');
    return;
  }
  foreach ($items as $item) {
    $itemsByIndex[$item->index_id][] = $item->object_id;
  }
  $batch['operations'][] = [
    [
      __CLASS__,
      'batchStart',
    ],
    [
      count($items),
    ],
  ];
  foreach ($itemsByIndex ?? [] as $index => $item_ids) {
    foreach (array_chunk($item_ids, $batch_size) as $chunk) {
      $batch['operations'][] = [
        [
          __CLASS__,
          'batchProcess',
        ],
        [
          $chunk,
          $index,
        ],
      ];
    }
  }

  // Prepare the output of processed items and show.
  batch_set($batch);
  drush_backend_batch_process();
}