You are here

public static function ImportBatchHelper::importUrlBatch in Entity Share 8.3

Batch operation.

Parameters

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

string $url: The URL to request.

array|\ArrayAccess $context: Batch context information.

File

modules/entity_share_client/src/ImportBatchHelper.php, line 31

Class

ImportBatchHelper
Class ImportBatchHelper.

Namespace

Drupal\entity_share_client

Code

public static function importUrlBatch(ImportContext $import_context, $url, &$context) {

  /** @var \Drupal\entity_share_client\Service\ImportServiceInterface $import_service */
  $import_service = \Drupal::service('entity_share_client.import_service');
  $import_prepared = $import_service
    ->prepareImport($import_context);
  if (!$import_prepared) {
    $context['finished'] = 1;
    return;
  }
  if (empty($context['sandbox'])) {
    $response = $import_service
      ->jsonApiRequest('GET', $url);
    $json = Json::decode((string) $response
      ->getBody());
    $entity_list_data = EntityShareUtility::prepareData($json['data']);
    $context['sandbox']['entity_list_data'] = $entity_list_data;
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($entity_list_data);
    $context['sandbox']['batch_size'] = \Drupal::getContainer()
      ->getParameter('entity_share_client.batch_size');
  }
  if (!isset($context['results']['imported_entity_ids'])) {
    $context['results']['imported_entity_ids'] = [];
  }
  $sub_data = array_slice($context['sandbox']['entity_list_data'], $context['sandbox']['progress'], $context['sandbox']['batch_size']);
  $import_service
    ->importEntityListData($sub_data);
  $context['results']['imported_entity_ids'] = NestedArray::mergeDeep($context['results']['imported_entity_ids'], $import_service
    ->getRuntimeImportContext()
    ->getImportedEntities());
  $context['sandbox']['progress'] += count($sub_data);
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}