You are here

public function CliService::push in CMS Content Sync 2.1.x

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

Push all entities for a specific flow.

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

\GuzzleHttp\Exception\GuzzleException

File

src/Cli/CliService.php, line 312

Class

CliService

Namespace

Drupal\cms_content_sync\Cli

Code

public function push($io, $flow_id, $options) {
  $push_mode = $options['push_mode'];
  $flows = Flow::getAll();
  if (!is_null($push_mode)) {
    if ('automatic_manual' != $push_mode && 'automatic_manual_force' != $push_mode) {
      $io
        ->error('Invalid value detected for push_mode. Allowed values are: automatic_manual and automatic_manual_force.');
      return;
    }
  }
  foreach ($flows as $id => $flow) {
    if ($flow_id && $id != $flow_id) {
      continue;
    }

    /**
     * @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
     */
    $entity_type_manager = \Drupal::service('entity_type.manager');
    foreach ($flow
      ->getController()
      ->getEntityTypeConfig(null, null, true) as $entity_type_name => $bundles) {
      foreach ($bundles as $bundle_name => $config) {
        if ('automatic_manual' == $push_mode || 'automatic_manual_force' == $push_mode) {
          if (PushIntent::PUSH_AUTOMATICALLY != $config['export'] && PushIntent::PUSH_MANUALLY != $config['export']) {
            continue;
          }
        }
        else {
          if (PushIntent::PUSH_AUTOMATICALLY != $config['export']) {
            continue;
          }
        }
        $storage = $entity_type_manager
          ->getStorage($entity_type_name);
        $query = $storage
          ->getQuery();

        // Files don't have bundles, so this would lead to a fatal error then.
        if ($storage
          ->getEntityType()
          ->getKey('bundle')) {
          $query = $query
            ->condition($storage
            ->getEntityType()
            ->getKey('bundle'), $bundle_name);
        }
        $ids = $query
          ->execute();
        $total = count($ids);
        if (!$total) {
          $io
            ->text('Skipping ' . $entity_type_name . '.' . $bundle_name . ' as no entities match.');
          continue;
        }
        $success = 0;
        $io
          ->text('Starting to push ' . $total . ' ' . $entity_type_name . '.' . $bundle_name . ' entities.');
        foreach ($ids as $id) {
          $entity = $storage
            ->load($id);

          /**
           * @var \Drupal\cms_content_sync\Entity\EntityStatus[] $entity_status
           */
          $entity_status = EntityStatus::getInfosForEntity($entity
            ->getEntityTypeId(), $entity
            ->uuid(), [
            'flow' => $flow
              ->id(),
          ]);
          if ('automatic_manual' == $push_mode && (empty($entity_status) || is_null($entity_status[0]
            ->getLastPush()))) {
            continue;
          }

          /**
           * @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
           */
          $entity_type_manager = \Drupal::service('entity_type.manager');
          $entity = $entity_type_manager
            ->getStorage($entity_type_name)
            ->load($id);
          try {
            PushIntent::pushEntity($entity, PushIntent::PUSH_FORCED, SyncIntent::ACTION_CREATE, $flow);
            ++$success;
          } catch (\Exception $exception) {
            \Drupal::logger('cms_content_sync')
              ->notice('Entity could not be pushed, reason: %exception<br>Flow: @flow_id', [
              '%exception' => $exception
                ->getMessage(),
              '@flow_id' => $flow_id,
            ]);
          }
        }
        $io
          ->text('Successfully pushed ' . $success . ' entities.');
        if ($total != $success) {
          $io
            ->text('Failed to push ' . ($total - $success) . ' entities.');
        }
      }
    }
  }
}