You are here

final class MigrationCommands in Lightning Workflow 8.2

Same name and namespace in other branches
  1. 8.3 modules/lightning_scheduler/src/Commands/MigrationCommands.php \Drupal\lightning_scheduler\Commands\MigrationCommands

Provides Drush commands for migrating scheduler data to the new base fields.

This class is final for the same reason that the Migrator service is: the migration is not an API and should not be extended or re-used.

Hierarchy

  • class \Drupal\lightning_scheduler\Commands\MigrationCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of MigrationCommands

1 string reference to 'MigrationCommands'
drush.services.yml in modules/lightning_scheduler/drush.services.yml
modules/lightning_scheduler/drush.services.yml
1 service uses MigrationCommands
lightning_scheduler.migration_commands in modules/lightning_scheduler/drush.services.yml
\Drupal\lightning_scheduler\Commands\MigrationCommands

File

modules/lightning_scheduler/src/Commands/MigrationCommands.php, line 16

Namespace

Drupal\lightning_scheduler\Commands
View source
final class MigrationCommands extends DrushCommands {

  /**
   * The migrator service.
   *
   * @var \Drupal\lightning_scheduler\Migrator
   */
  protected $migrator;

  /**
   * LightningSchedulerCommands constructor.
   *
   * @param \Drupal\lightning_scheduler\Migrator $migrator
   *   The migrator service.
   */
  public function __construct(Migrator $migrator, TranslationInterface $translation = NULL) {
    $this->migrator = $migrator;
  }

  /**
   * Migrates scheduled transition data to the new base fields.
   *
   * @param $entity_type_id
   *   (optional) The entity type ID to migrate.
   *
   * @command lightning:scheduler:migrate
   */
  public function migrate($entity_type_id = NULL) {
    $out = $this
      ->output();
    $entity_types = $this->migrator
      ->getEntityTypesToMigrate((array) $entity_type_id);
    if (empty($entity_types)) {
      if ($entity_type_id) {
        $out
          ->writeln("The {$entity_type_id} entity type does not need to be migrated.");
      }
      else {
        $out
          ->writeln('All migrations are complete.');
      }
      return;
    }
    $message = $this->migrator
      ->generatePreMigrationMessage($entity_types, FALSE);
    $out
      ->writeln((string) $message);
    $continue = $this
      ->confirm('Continue?');
    if (empty($continue)) {
      return;
    }
    foreach ($entity_types as $entity_type_id => $entity_type) {

      // Create a progress callback which will output a message for every ten
      // items migrated.
      $callback = function ($entity_type_id, $count) use ($entity_type, $out) {
        if ($count % 10 === 0) {
          $variables = [
            '@singular' => $entity_type
              ->getSingularLabel(),
            '@plural' => $entity_type
              ->getPluralLabel(),
          ];
          $message = new PluralTranslatableMarkup($count, '1 @singular migrated.', '@count @plural migrated.', $variables);
          $out
            ->writeln((string) $message);
        }
      };
      $this->migrator
        ->migrateAll($entity_type_id, $callback);
    }
  }

  /**
   * Deletes old scheduled transition data for an entity type without migrating.
   *
   * @param $entity_type_id
   *   The entity type ID to migrate.
   *
   * @command lightning:scheduler:purge
   */
  public function purge($entity_type_id) {
    $out = $this
      ->output();
    $entity_types = $this->migrator
      ->getEntityTypesToMigrate((array) $entity_type_id);
    if (empty($entity_types)) {
      $out
        ->writeln('The given entity type either does not need to be migrated, or it has already been migrated or purged.');
      return;
    }
    $message = "You are about to purge existing scheduled transitions for the given entity type. This will permanently delete scheduled transitions and cannot be undone.";
    $out
      ->writeln($message);
    $continue = $this
      ->confirm('Continue?');
    if (empty($continue)) {
      return;
    }
    $this->migrator
      ->purge($entity_type_id, 'scheduled_publication');
    $this->migrator
      ->purge($entity_type_id, 'scheduled_moderation_state');
    $this->migrator
      ->completeMigration($entity_type_id);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrationCommands::$migrator protected property The migrator service.
MigrationCommands::migrate public function Migrates scheduled transition data to the new base fields.
MigrationCommands::purge public function Deletes old scheduled transition data for an entity type without migrating.
MigrationCommands::__construct public function LightningSchedulerCommands constructor.