You are here

public function ImportService::importChannel in Entity Share 8.3

Import all the entities on a channel.

Parameters

\Drupal\entity_share_client\ImportContext $context: The import context.

Overrides ImportServiceInterface::importChannel

File

modules/entity_share_client/src/Service/ImportService.php, line 155

Class

ImportService
Class ImportService.

Namespace

Drupal\entity_share_client\Service

Code

public function importChannel(ImportContext $context) {
  if (!$this
    ->prepareImport($context)) {
    return;
  }
  $log_variables = [
    '@remote_id' => $context
      ->getRemoteId(),
    '@channel_id' => $context
      ->getChannelId(),
  ];
  $channel_count = $context
    ->getRemoteChannelCount();

  // Check how much content is in the channel if the count has not been
  // provided before.
  if (empty($channel_count)) {
    $url_uuid = $this->runtimeImportContext
      ->getChannelUrlUuid();
    $response = $this->remoteManager
      ->jsonApiRequest($this->runtimeImportContext
      ->getRemote(), 'GET', $url_uuid);
    $json = Json::decode((string) $response
      ->getBody());
    if (isset($json['errors'])) {
      $this->logger
        ->error('An error occurred while requesting the UUID URL for the remote website @remote_id and channel @channel_id', $log_variables);
      $this->messenger
        ->addError($this
        ->t('An error occurred while requesting the UUID URL for the remote website @remote_id and channel @channel_id', $log_variables));
      return;
    }
    elseif (!isset($json['meta']['count'])) {
      $this->logger
        ->error('There is no count of the number of entities to import for the remote website @remote_id and channel @channel_id', $log_variables);
      $this->messenger
        ->addError($this
        ->t('There is no count of the number of entities to import for the remote website @remote_id and channel @channel_id', $log_variables));
      return;
    }
    $channel_count = $json['meta']['count'];
  }
  if ($channel_count == 0) {
    $this->logger
      ->info('Nothing to import for the remote website @remote_id and channel @channel_id', $log_variables);
    $this->messenger
      ->addMessage($this
      ->t('Nothing to import for the remote website @remote_id and channel @channel_id', $log_variables));
    return;
  }

  // Using the number of entities on the channel, we can generate all the
  // urls of the channel's pages, and so prepare all the operations.
  $step = 50;
  $url = $this->runtimeImportContext
    ->getChannelUrl();
  $parsed_url = UrlHelper::parse($url);
  $parsed_url['query']['page']['limit'] = $step;

  // If the count is a multiple of the step, the last offset is equal to the
  // number of content and so the last offset will have no data.
  // So we remove 1.
  $operations = [];
  if ($channel_count >= $step) {
    $offsets = range(0, $channel_count - 1, $step);
    foreach ($offsets as $offset) {
      $parsed_url['query']['page']['offset'] = $offset;
      $query = UrlHelper::buildQuery($parsed_url['query']);
      $prepared_url = $parsed_url['path'] . '?' . $query;
      $operations[] = [
        '\\Drupal\\entity_share_client\\ImportBatchHelper::importUrlBatch',
        [
          $context,
          $prepared_url,
        ],
      ];
    }
  }
  else {
    $operations[] = [
      '\\Drupal\\entity_share_client\\ImportBatchHelper::importUrlBatch',
      [
        $context,
        $url,
      ],
    ];
  }
  $batch = [
    'title' => $this
      ->t('Import channel'),
    'operations' => $operations,
    'progress_message' => $this
      ->t('Imported pages: @current of @total.'),
    'finished' => '\\Drupal\\entity_share_client\\ImportBatchHelper::importUrlBatchFinished',
  ];
  batch_set($batch);
}