You are here

public function EntityShareCronService::sync in Entity Share Cron 8.2

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

Synchronizes entities starting from provided channel URL.

Parameters

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

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

array $channel_info: Channel info.

int $page_limit: The maximum number of pages to process. The remaining will be enqueued.

Overrides EntityShareCronServiceInterface::sync

File

src/EntityShareCronService.php, line 101

Class

EntityShareCronService
Entity Share Cron service.

Namespace

Drupal\entity_share_cron

Code

public function sync($remote_id, $channel_id, array $channel_info, $page_limit = 0) {

  // Collects entities to import from each page.
  $data_to_import = [];
  $next_page = 1;
  $url = $channel_info['url'];
  while ($url) {

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

        // Enqueues the next page.
        $next_info = $channel_info;
        $next_info['url'] = $url;
        $this
          ->enqueue($remote_id, $channel_id, $next_info);
        $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);
  }
  if (empty($channel_config['operations']['update'])) {
    $this
      ->filterDataToImport($data_to_import, FALSE);
  }

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