You are here

class MigrationImportSync in Migrate Tools 8.5

Same name and namespace in other branches
  1. 8.4 src/EventSubscriber/MigrationImportSync.php \Drupal\migrate_tools\EventSubscriber\MigrationImportSync

Import and sync source and destination.

Hierarchy

  • class \Drupal\migrate_tools\EventSubscriber\MigrationImportSync implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of MigrationImportSync

1 string reference to 'MigrationImportSync'
migrate_tools.services.yml in ./migrate_tools.services.yml
migrate_tools.services.yml
1 service uses MigrationImportSync
migrate_tools.migration_sync in ./migrate_tools.services.yml
Drupal\migrate_tools\EventSubscriber\MigrationImportSync

File

src/EventSubscriber/MigrationImportSync.php, line 17

Namespace

Drupal\migrate_tools\EventSubscriber
View source
class MigrationImportSync implements EventSubscriberInterface {

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $dispatcher;

  /**
   * MigrationImportSync constructor.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
   *   The event dispatcher.
   */
  public function __construct(EventDispatcherInterface $dispatcher) {
    $this->dispatcher = $dispatcher;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [];
    $events[MigrateEvents::PRE_IMPORT][] = [
      'sync',
    ];
    return $events;
  }

  /**
   * Event callback to sync source and destination.
   *
   * @param \Drupal\migrate\Event\MigrateImportEvent $event
   *   The migration import event.
   */
  public function sync(MigrateImportEvent $event) {
    $migration = $event
      ->getMigration();
    if (!empty($migration->syncSource)) {
      $id_map = $migration
        ->getIdMap();
      $id_map
        ->prepareUpdate();

      // Clone so that any generators aren't initialized prematurely.
      $source = clone $migration
        ->getSourcePlugin();
      $source
        ->rewind();
      $source_id_values = [];
      while ($source
        ->valid()) {
        $source_id_values[] = $source
          ->current()
          ->getSourceIdValues();
        $source
          ->next();
      }
      $id_map
        ->rewind();
      $destination = $migration
        ->getDestinationPlugin();
      while ($id_map
        ->valid()) {
        $map_source_id = $id_map
          ->currentSource();
        if (!in_array($map_source_id, $source_id_values, TRUE)) {
          $destination_ids = $id_map
            ->currentDestination();
          $this
            ->dispatchRowDeleteEvent(MigrateEvents::PRE_ROW_DELETE, $migration, $destination_ids);
          $this
            ->dispatchRowDeleteEvent(MigratePlusEvents::MISSING_SOURCE_ITEM, $migration, $destination_ids);
          $destination
            ->rollback($destination_ids);
          $this
            ->dispatchRowDeleteEvent(MigrateEvents::POST_ROW_DELETE, $migration, $destination_ids);
          $id_map
            ->delete($map_source_id);
        }
        $id_map
          ->next();
      }
      $this->dispatcher
        ->dispatch(MigrateEvents::POST_ROLLBACK, new MigrateRollbackEvent($migration));
    }
  }

  /**
   * Dispatches MigrateRowDeleteEvent event.
   *
   * @param string $event_name
   *   The event name to dispatch.
   * @param \Drupal\migrate\Plugin\MigrationInterface $migration
   *   The active migration.
   * @param array $destination_ids
   *   The destination identifier values of the record.
   */
  protected function dispatchRowDeleteEvent($event_name, MigrationInterface $migration, array $destination_ids) {

    // Symfony changing dispatcher so implementation could change.
    $this->dispatcher
      ->dispatch($event_name, new MigrateRowDeleteEvent($migration, $destination_ids));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrationImportSync::$dispatcher protected property The event dispatcher.
MigrationImportSync::dispatchRowDeleteEvent protected function Dispatches MigrateRowDeleteEvent event.
MigrationImportSync::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
MigrationImportSync::sync public function Event callback to sync source and destination.
MigrationImportSync::__construct public function MigrationImportSync constructor.