You are here

public function EntityShareClientCliService::ioPull in Entity Share 8

Same name and namespace in other branches
  1. 8.3 modules/entity_share_client/src/Service/EntityShareClientCliService.php \Drupal\entity_share_client\Service\EntityShareClientCliService::ioPull()
  2. 8.2 modules/entity_share_client/src/Service/EntityShareClientCliService.php \Drupal\entity_share_client\Service\EntityShareClientCliService::ioPull()

Handle the pull interaction.

Parameters

string $remote_id: The remote website id to import from.

string $channel_id: The remote channel id to import.

\Symfony\Component\Console\Style\StyleInterface|\ConfigSplitDrush8Io $io: The $io interface of the cli tool calling.

callable $t: The translation function akin to t().

File

modules/entity_share_client/src/Service/EntityShareClientCliService.php, line 93

Class

EntityShareClientCliService
Class EntityShareClientCliService.

Namespace

Drupal\entity_share_client\Service

Code

public function ioPull($remote_id, $channel_id, $io, callable $t) {
  Timer::start('io-pull');
  $remotes = Remote::loadMultiple();

  // Check that the remote website exists.
  if (!isset($remotes[$remote_id])) {
    $io
      ->error($t('There is no remote website configured with the id: @remote_id.', [
      '@remote_id' => $remote_id,
    ]));
    return;
  }
  $remote = $remotes[$remote_id];
  $channel_infos = $this->remoteManager
    ->getChannelsInfos($remote);

  // Check that the channel exists.
  if (!isset($channel_infos[$channel_id])) {
    $io
      ->error($t('There is no channel configured or accessible with the id: @channel_id.', [
      '@channel_id' => $channel_id,
    ]));
    return;
  }

  // Import channel content and loop on pagination.
  $this->jsonapiHelper
    ->setRemote($remote);
  $http_client = $this->remoteManager
    ->prepareJsonApiClient($remote);
  $channel_url = $channel_infos[$channel_id]['url'];
  while ($channel_url) {
    $io
      ->text($t('Beginning to import content from URL: @url', [
      '@url' => $channel_url,
    ]));
    $json_response = $http_client
      ->get($channel_url)
      ->getBody()
      ->getContents();
    $json = Json::decode($json_response);
    $imported_entities = $this->jsonapiHelper
      ->importEntityListData($this->jsonapiHelper
      ->prepareData($json['data']));
    $io
      ->text($t('@number entities have been imported.', [
      '@number' => count($imported_entities),
    ]));
    if (isset($json['links']['next'])) {
      $channel_url = $json['links']['next'];
    }
    else {
      $channel_url = FALSE;
    }
  }
  Timer::stop('io-pull');
  $io
    ->success($t('Channel successfully pulled. Execution time @time ms.', [
    '@time' => Timer::read('io-pull'),
  ]));
}