You are here

public function EntityShareCronService::sync in Entity Share Cron 3.0.x

Same name and namespace in other branches
  1. 8.2 src/EntityShareCronService.php \Drupal\entity_share_cron\EntityShareCronService::sync()
  2. 8 src/EntityShareCronService.php \Drupal\entity_share_cron\EntityShareCronService::sync()

Synchronizes entities starting from provided channel.

Parameters

string $remote_id: The ID of the remote the channel belongs to.

string $channel_id: The ID of the channel to be synchronized.

null|string $url: The url of the page to enqueue for import. NULL if starting from the first page.

Overrides EntityShareCronServiceInterface::sync

File

src/EntityShareCronService.php, line 88

Class

EntityShareCronService
Entity Share Cron service.

Namespace

Drupal\entity_share_cron

Code

public function sync($remote_id, $channel_id, $url) {
  $page_limit = $this->config
    ->get('page_limit');
  $channel_config = $this
    ->getChannelConfig($remote_id, $channel_id);
  $import_context = new ImportContext($remote_id, $channel_id, $channel_config['import_config']);
  if (!$this->importService
    ->prepareImport($import_context)) {
    return;
  }

  // Collects entities to import from each page.
  $data_to_import = [];
  $next_page = 1;
  if (is_null($url)) {
    $url = $this->importService
      ->getRuntimeImportContext()
      ->getChannelUrl();
  }
  while ($url) {

    // Performs request to get the list of entities.
    $page_data = $this
      ->getPage($url);
    $data_to_import = array_merge($data_to_import, $page_data['data']);
    $next_page++;
    $url = $page_data['next'];
    if ($url && $page_limit != 0 && $next_page > $page_limit) {

      // Enqueues the next page after the limit.
      $this
        ->enqueue($remote_id, $channel_id, $url);
      $url = FALSE;
    }
  }

  // Removes data to import according to enabled operations.
  $channel_config = $this
    ->getChannelConfig($remote_id, $channel_id);
  if (empty($channel_config['operations']['create'])) {
    $this
      ->filterDataToImport($data_to_import, TRUE);
  }
  if (empty($channel_config['operations']['update'])) {
    $this
      ->filterDataToImport($data_to_import, FALSE);
  }

  // Imports the data.
  $this->importService
    ->importEntityListData($data_to_import);
}