You are here

class BatchManager in Workbench Moderation to Content Moderation 8

Manages communication between Batch API and the migration manager.

Hierarchy

Expanded class hierarchy of BatchManager

1 string reference to 'BatchManager'
wbm2cm.services.yml in ./wbm2cm.services.yml
wbm2cm.services.yml
1 service uses BatchManager
wbm2cm.batch_manager in ./wbm2cm.services.yml
\Drupal\wbm2cm\BatchManager

File

src/BatchManager.php, line 12

Namespace

Drupal\wbm2cm
View source
class BatchManager {

  /**
   * Processing operations should be stopped.
   *
   * @var bool
   */
  protected $isProcessingStopped = FALSE;

  /**
   * The migration manager.
   *
   * @var \Drupal\wbm2cm\MigrateManager
   */
  protected $manager;

  /**
   * The key value store for the wbm2cm module.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $keyValueStore;

  /**
   * Logger service.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Instantiate BatchManager.
   *
   * @param \Drupal\wbm2cm\MigrateManager $manager
   *   The migration manager.
   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $key_value_factory
   *   The key value store factory.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(MigrateManager $manager, KeyValueFactoryInterface $key_value_factory, LoggerInterface $logger) {
    $this->manager = $manager;
    $this->batchStore = $key_value_factory
      ->get('wbm2cm_batch');
    $this->logger = $logger;
  }

  /**
   * Determine if a particular step is complete.
   *
   * @param string $step
   *   The name of the step, e.g. "step1".
   *
   * @return bool
   *   True if complete and no action needs taken, else false.
   */
  public function isStepComplete($step) {
    if ($this->batchStore
      ->has($step)) {
      return 'complete' == $this->batchStore
        ->get($step);
    }
    return FALSE;
  }

  /**
   * Set a particular step to complete.
   *
   * @param string $step
   *   The name of the step, e.g. "step1".
   */
  public function setStepComplete($step) {
    $this->batchStore
      ->set($step, 'complete');
  }

  /**
   * Set a particular step to incomplete.
   *
   * @param string $step
   *   The name of the step, e.g. "step1".
   */
  public function setStepIncomplete($step) {
    $this->batchStore
      ->set($step, 'incomplete');
  }

  /**
   * Stop processing operations.
   */
  protected function stopProcessing() {
    $this->isProcessingStopped = TRUE;
  }

  /**
   * Determine if operations should stop processing.
   *
   * @return bool
   *   True if operations should not be processed, else false.
   */
  protected function isProcessingStopped() {
    return $this->isProcessingStopped;
  }

  /**
   * Determine if a particular step can be processed.
   *
   * @return bool
   *   True if it can be processed, else false.
   */
  public function isStepSkipped($step) {
    if ($this
      ->isProcessingStopped()) {
      $this->logger
        ->info('Step %step is skipped because: %reason', [
        '%step' => $step,
        '%reason' => 'Processing is stopped',
      ]);
      return TRUE;
    }
    if ($this
      ->isStepComplete($step)) {
      $this->logger
        ->info('Step %step is skipped because: %reason', [
        '%step' => $step,
        '%reason' => 'Step is complete',
      ]);
      return TRUE;
    }
    $this->logger
      ->info('Step %step is NOT skipped', [
      '%step' => $step,
    ]);
    return FALSE;
  }

  /**
   * States and transitions are stored in key value (i.e. the Workflow entity is created).
   */
  public function step1(&$context) {
    if ($this
      ->isStepSkipped('step1')) {
      return;
    }
    $this->manager
      ->saveWorkbenchModerationStatesAndTransitions();
    $this
      ->setStepComplete('step1');
    $context['message'] = 'Saving Workbench Moderation states and transitions to key value storage.';
  }

  /**
   * Entity state maps are stored in key value.
   */
  public function step2(&$context) {
    if ($this
      ->isStepSkipped('step2')) {
      return;
    }
    $this->manager
      ->saveWorkbenchModerationSateMap();
    $context['message'] = 'Saving Workbench Moderation entity states to key value storage.';
  }

  /**
   * WBM uninstalled.
   */
  public function step3(&$context) {
    if ($this
      ->isStepSkipped('step3')) {
      return;
    }
    $this->manager
      ->uninstallWorkbenchModeration();
    $this
      ->setStepComplete('step3');
    $context['message'] = 'Uninstalling Workbench Moderation.';
  }

  /**
   * Workflows installed.
   */
  public function step4(&$context) {
    if ($this
      ->isStepSkipped('step4')) {
      return;
    }
    $this->manager
      ->installWorkflows();
    $this
      ->setStepComplete('step4');
    $context['message'] = 'Installing Workflows module.';
  }

  /**
   * CM installed.
   */
  public function step5(&$context) {
    if ($this
      ->isStepSkipped('step5')) {
      return;
    }
    $this->manager
      ->installContentModeration();
    $this
      ->setStepComplete('step5');
    $context['message'] = 'Installing Content Moderation module.';
  }

  /**
   * States and transitions are migrated (i.e. the Workflow entity is created).
   */
  public function step6(&$context) {
    if ($this
      ->isStepSkipped('step6')) {
      return;
    }
    $this->manager
      ->recreateWorkbenchModerationWorkflow();
    $this
      ->setStepComplete('step6');
    $context['message'] = 'Importing states and transitions from key value storage to Workflows.';
  }

  /**
   * Entity state maps are migrated.
   */
  public function step7(&$context) {
    if ($this
      ->isStepSkipped('step7')) {
      return;
    }
    $this->manager
      ->recreateModerationStatesOnEntities();
    $this
      ->setStepComplete('step7');
    $context['message'] = 'Importing entity moderation states from key value storage to Content Moderation.';
  }

  /**
   * All keyvalue temporary state is cleaned up except for progress state.
   */
  public function step8(&$context) {
    if ($this
      ->isStepSkipped('step8')) {
      return;
    }
    $this->manager
      ->cleanupKeyValue();
    $this
      ->setStepComplete('step8');

    // This is the last step, so set the migration as finished.
    $this->manager
      ->setFinished();
    $context['message'] = 'Clean up key value storage.';
  }

  /**
   * Finalize the batch process.
   */
  public function finished($success, $results, $operations) {
    if ($success) {
      $message = t('Migration complete. You can now uninstall this module.');
    }
    else {

      // @todo change this to useful error message
      $message = t('Finished with an error.');
    }
    drupal_set_message($message);
    return new RedirectResponse(\Drupal::url('wbm2cm.overview', [], [
      'absolute' => TRUE,
    ]));
  }

  /**
   * Purge all key value stores used by the batch manager.
   *
   * Note: this should only be used during the module's uninstall.
   */
  public function purgeAllKeyValueStores() {
    $this->batchStore
      ->deleteAll();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BatchManager::$isProcessingStopped protected property Processing operations should be stopped.
BatchManager::$keyValueStore protected property The key value store for the wbm2cm module.
BatchManager::$logger protected property Logger service.
BatchManager::$manager protected property The migration manager.
BatchManager::finished public function Finalize the batch process.
BatchManager::isProcessingStopped protected function Determine if operations should stop processing.
BatchManager::isStepComplete public function Determine if a particular step is complete.
BatchManager::isStepSkipped public function Determine if a particular step can be processed.
BatchManager::purgeAllKeyValueStores public function Purge all key value stores used by the batch manager.
BatchManager::setStepComplete public function Set a particular step to complete.
BatchManager::setStepIncomplete public function Set a particular step to incomplete.
BatchManager::step1 public function States and transitions are stored in key value (i.e. the Workflow entity is created).
BatchManager::step2 public function Entity state maps are stored in key value.
BatchManager::step3 public function WBM uninstalled.
BatchManager::step4 public function Workflows installed.
BatchManager::step5 public function CM installed.
BatchManager::step6 public function States and transitions are migrated (i.e. the Workflow entity is created).
BatchManager::step7 public function Entity state maps are migrated.
BatchManager::step8 public function All keyvalue temporary state is cleaned up except for progress state.
BatchManager::stopProcessing protected function Stop processing operations.
BatchManager::__construct public function Instantiate BatchManager.