You are here

public function AcsfDuplicationScrubEntityHandler::handle in Acquia Cloud Site Factory Connector 8

Same name and namespace in other branches
  1. 8.2 src/Event/AcsfDuplicationScrubEntityHandler.php \Drupal\acsf\Event\AcsfDuplicationScrubEntityHandler::handle()

Implements AcsfEventHandler::handle().

Overrides AcsfEventHandler::handle

3 calls to AcsfDuplicationScrubEntityHandler::handle()
AcsfDuplicationScrubCommentHandler::handle in src/Event/AcsfDuplicationScrubCommentHandler.php
Implements AcsfEventHandler::handle().
AcsfDuplicationScrubNodeHandler::handle in src/Event/AcsfDuplicationScrubNodeHandler.php
Implements AcsfEventHandler::handle().
AcsfDuplicationScrubUserHandler::handle in src/Event/AcsfDuplicationScrubUserHandler.php
Implements AcsfEventHandler::handle().
3 methods override AcsfDuplicationScrubEntityHandler::handle()
AcsfDuplicationScrubCommentHandler::handle in src/Event/AcsfDuplicationScrubCommentHandler.php
Implements AcsfEventHandler::handle().
AcsfDuplicationScrubNodeHandler::handle in src/Event/AcsfDuplicationScrubNodeHandler.php
Implements AcsfEventHandler::handle().
AcsfDuplicationScrubUserHandler::handle in src/Event/AcsfDuplicationScrubUserHandler.php
Implements AcsfEventHandler::handle().

File

src/Event/AcsfDuplicationScrubEntityHandler.php, line 71

Class

AcsfDuplicationScrubEntityHandler
Handles the scrubbing of Drupal entities.

Namespace

Drupal\acsf\Event

Code

public function handle() {
  drush_print(dt('Entered @class', [
    '@class' => get_class($this),
  ]));
  $options = $this->event->context['scrub_options'];
  $limit = $options['batch_' . $this->entityTypeId];
  $var_name = 'acsf_duplication_scrubbed_' . $this->entityTypeId;
  $entity_type = $this->entityTypeManager
    ->getDefinition($this->entityTypeId);
  do {

    // Get a range of entities we have not processed yet (i.e. do not query
    // entities that could not be deleted, multiple times.)
    // Deleting the highest entity first probably makes for less processing
    // for some entity types (e.g. for comments, since delete() does not need
    // to deal with comments that would be orphaned).
    $ids = $this
      ->getBaseQuery()
      ->range(0, $limit)
      ->sort($entity_type
      ->getKey('id'), 'DESC')
      ->execute();
    if ($ids) {

      // Delete the entities, one by one. This may be slower than mass
      // deleting them, but this way we can catch an exception without a mass
      // delete being fully rolled back.
      $entities = $this->entityTypeManager
        ->getStorage($this->entityTypeId)
        ->loadMultiple($ids);
      $this
        ->deleteEntities($entities);
      $this->acsfVarStorage
        ->set($var_name, min($ids), 'acsf_duplication_scrub');
    }
    else {

      // Make sure we won't run a query for the same IDs again.
      $this->acsfVarStorage
        ->set($var_name, 0, 'acsf_duplication_scrub');
      break;
    }

    // If out-of-memory protection is not set, we loop until all items are
    // processed.
    if ($options['avoid_oom']) {
      $this->event->dispatcher
        ->interrupt();
      break;
    }
  } while (TRUE);
}