You are here

public function CliService::pull in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 src/Cli/CliService.php \Drupal\cms_content_sync\Cli\CliService::pull()
  2. 2.1.x src/Cli/CliService.php \Drupal\cms_content_sync\Cli\CliService::pull()

Kindly ask the Sync Core to pull all entities for a specific flow, or to force pull one specific entity.

Parameters

ICLIIO $io: The CLI service which allows interoperability

string $flow_id: The flow the entities should be pulled from

array $options: An array containing the option parameters provided by Drush

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\EdgeBox\SyncCore\Exception\SyncCoreException

File

src/Cli/CliService.php, line 109

Class

CliService

Namespace

Drupal\cms_content_sync\Cli

Code

public function pull($io, $flow_id, $options) {
  $force = $options['force'];
  $entity_type = $options['entity_type'];
  $entity_uuid = $options['entity_uuid'];
  if ('' != $entity_uuid && null == $entity_type) {
    $io
      ->error('If a specific entity_uuid should be pulled, the entity_type also has to be set.');
    return;
  }

  // @todo Allow pulling of all entities of a specific type for one specific flow.
  if ('' != $entity_type && null == $entity_uuid) {
    $io
      ->error('If the entity_type option is set, the entity_uuid to be pulled also has to be specified.');
    return;
  }
  if (!is_null($entity_uuid) && !is_null($entity_uuid)) {
    if (UUID::isValid($entity_uuid)) {

      // Pull a single entity.
      // @todo Allow pull for single entities which have not been pulled before.
      FlowPull::force_pull_entity($flow_id, $entity_type, $entity_uuid);
    }
    else {
      $io
        ->error('The specified entity_uuid is invalid.');
    }
  }
  else {

    // Pull all entities for the specified flow.
    $flows = Flow::getAll();
    foreach ($flows as $id => $flow) {
      if ($flow_id && $id != $flow_id) {
        continue;
      }
      $result = FlowPull::pullAll($flow, $force);
      if (empty($result)) {
        $io
          ->text('No automated pull configured for Flow: ' . $flow
          ->label());
        continue;
      }
      $io
        ->text('Started pulling for Flow: ' . $flow
        ->label());
      foreach ($result as $operation) {
        $operation
          ->execute();
        if (!($goal = $operation
          ->total())) {
          $io
            ->text('> Nothing to do for: ' . $operation
            ->getTypeMachineName() . '.' . $operation
            ->getBundleMachineName() . ' from ' . $operation
            ->getSourceName());
          continue;
        }
        $progress = 0;
        while ($progress < $goal) {
          if ($progress > 0) {
            sleep(5);
          }
          try {
            $progress = $operation
              ->progress();
          } catch (TimeoutException $e) {
            $io
              ->text('> Timeout when asking the Sync Core to report on the progress of pulling ' . $goal . ' ' . $operation
              ->getTypeMachineName() . '.' . $operation
              ->getBundleMachineName() . ' from ' . $operation
              ->getSourceName() . '. Will try again in 15 seconds...');
            sleep(15);
            continue;
          }
          if ($progress == $goal) {
            $io
              ->text('> Finished ' . $goal . ' operations for ' . $operation
              ->getTypeMachineName() . '.' . $operation
              ->getBundleMachineName() . ' from ' . $operation
              ->getSourceName());
          }
          elseif (0 == $progress) {
            sleep(5);
          }
          else {
            $io
              ->text('> Finished ' . $progress . ' of ' . $goal . ' operations for ' . $operation
              ->getTypeMachineName() . '.' . $operation
              ->getBundleMachineName() . ' from ' . $operation
              ->getSourceName() . ': ' . floor($progress / $goal * 100) . '%');
          }
        }
      }
    }
  }
}