You are here

class AcsfDuplicationScrubCommentHandler in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 src/Event/AcsfDuplicationScrubCommentHandler.php \Drupal\acsf\Event\AcsfDuplicationScrubCommentHandler

Handles the scrubbing of Drupal comments.

Hierarchy

Expanded class hierarchy of AcsfDuplicationScrubCommentHandler

2 files declare their use of AcsfDuplicationScrubCommentHandler
AcsfDuplicationCommands.php in acsf_duplication/src/Commands/AcsfDuplicationCommands.php
acsf_duplication.api.php in acsf_duplication/acsf_duplication.api.php
Documents hooks provided by the ACSF Duplication module.

File

src/Event/AcsfDuplicationScrubCommentHandler.php, line 8

Namespace

Drupal\acsf\Event
View source
class AcsfDuplicationScrubCommentHandler extends AcsfDuplicationScrubEntityHandler {

  /**
   * Constructor.
   *
   * @param AcsfEvent $event
   *   The event that has been initiated.
   */
  public function __construct(AcsfEvent $event) {
    $this->entityTypeId = 'comment';
    parent::__construct($event);
  }

  /**
   * Implements AcsfEventHandler::handle().
   */
  public function handle() {
    $options = $this->event->context['scrub_options'];
    if ($options['retain_content'] || !\Drupal::moduleHandler()
      ->moduleExists('comment')) {

      // We still want to log that we were here.
      $this
        ->consoleLog(dt('Entered @class', [
        '@class' => get_class($this),
      ]));
      return;
    }

    // If we're using the standard comment storage handler, replace it with
    // a handler that prefers scrubbing over consistency (i.e. in case of
    // exceptions thrown, loads things anyway).
    $original_class = get_class($this->entityTypeManager
      ->getStorage($this->entityTypeId));
    if ($original_class === 'Drupal\\comment\\CommentStorage') {

      // $this->entityTypeManager->handlers contains a.o. the current storage
      // class instance which we want to change for another one. We can only do
      // this through EntityTypeManager::clearCachedDefinitions() which
      // clears:
      // - handlers (all class instances per handler type / entity type);
      // - definitions (the full entity type (plugin) definitions, including
      //   the one in the cache backend).
      // We don't want to invalidate the cached definitions, but oh well...
      $this->entityTypeManager
        ->clearCachedDefinitions();

      // Now we want to set the (temporary) new class. For this there's only a
      // method in the entity type, not in the manager. The (plugin / definition
      // class for the) entity type will always be regenerated before setting
      // its storage handler, because all definitions were cleared.
      $this->entityTypeManager
        ->getDefinition($this->entityTypeId)
        ->setStorageClass('Drupal\\acsf\\Event\\AcsfDuplicationScrubCommentStorage');

      // Also: try to load/delete orphaned comments by ID (not by loading the
      // entities) in a custom method.
      $limit = $options['batch_' . $this->entityTypeId];
      if ($options['avoid_oom']) {
        $var_name = 'acsf_duplication_scrubbed_' . $this->entityTypeId;
        $max_id = $this->acsfVarStorage
          ->get($var_name, -1);
        $this->entityTypeManager
          ->getStorage($this->entityTypeId)
          ->deleteOrphanedItems($limit, $max_id);
      }
      else {

        // If 'avoid_oom' is not set, we should delete all orphaned comments
        // now. (The limit is basically only to keep the SQL from becoming too
        // long; processing isn't hugely expensive.)
        do {
          $orphaned_ids = $this->entityTypeManager
            ->getStorage($this->entityTypeId)
            ->deleteOrphanedItems($limit);
        } while ($orphaned_ids);
      }
    }
    parent::handle();

    // Clean up after ourselves.
    if ($original_class === 'Drupal\\comment\\CommentStorage') {
      $this->entityTypeManager
        ->clearCachedDefinitions();
      $this->entityTypeManager
        ->getDefinition($this->entityTypeId)
        ->setStorageClass($original_class);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function countRemaining() {
    if (!\Drupal::moduleHandler()
      ->moduleExists('comment')) {
      return 0;
    }
    return parent::countRemaining();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcsfDuplicationScrubCommentHandler::countRemaining public function Counts the entities that still need to be processed. Overrides AcsfDuplicationScrubEntityHandler::countRemaining
AcsfDuplicationScrubCommentHandler::handle public function Implements AcsfEventHandler::handle(). Overrides AcsfDuplicationScrubEntityHandler::handle
AcsfDuplicationScrubCommentHandler::__construct public function Constructor. Overrides AcsfDuplicationScrubEntityHandler::__construct
AcsfDuplicationScrubEntityHandler::$acsfVarStorage protected property The ACSF variable storage.
AcsfDuplicationScrubEntityHandler::$entityTypeId protected property The entity type to scrub.
AcsfDuplicationScrubEntityHandler::$entityTypeManager protected property The entity manager.
AcsfDuplicationScrubEntityHandler::$moduleHandler protected property The module handler.
AcsfDuplicationScrubEntityHandler::deleteEntities protected function Deletes entities. 1
AcsfDuplicationScrubEntityHandler::getBaseQuery protected function Gets an initialized entity query instance. 2
AcsfDuplicationScrubEntityHandler::getPreservedUsers protected function Gets a list of user IDs which should not be scrubbed. 1
AcsfDuplicationScrubEntityHandler::getSiteAdmins public function Gets a list of site admins.
AcsfEventHandler::$completed public property The time that the handler was completed.
AcsfEventHandler::$message public property Any messages triggered by the handler.
AcsfEventHandler::$started public property The time that the handler was started.
AcsfEventHandler::consoleLog public function Writes a log message to the console.