You are here

public static function UserSyncBatch::progress in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_user_sync/src/UserSyncBatch.php \Drupal\crm_core_user_sync\UserSyncBatch::progress()

Batch operation callback.

File

modules/crm_core_user_sync/src/UserSyncBatch.php, line 15

Class

UserSyncBatch
Methods for running the ConfigImporter in a batch.

Namespace

Drupal\crm_core_user_sync

Code

public static function progress(&$context) {
  $userStorage = \Drupal::entityTypeManager()
    ->getStorage('user');
  if (empty($context['sandbox'])) {
    $max = $userStorage
      ->getQuery()
      ->condition('uid', 1, '>')
      ->count()
      ->execute();
    $context['sandbox']['max'] = $max;
    $context['sandbox']['progress'] = 0;
    $context['results']['synced'] = 0;
    $context['sandbox']['last_uid'] = 1;
  }
  $limit = 20;
  $uids = $userStorage
    ->getQuery()
    ->sort('uid')
    ->condition('uid', $context['sandbox']['last_uid'], '>')
    ->range(0, $limit)
    ->execute();
  $accounts = $userStorage
    ->loadMultiple($uids);
  foreach ($accounts as $account) {
    if (\Drupal::service('crm_core_user_sync.relation')
      ->relate($account)) {
      $context['results']['synced']++;
    }
    $context['sandbox']['last_uid'] = $account
      ->id();
    $context['sandbox']['progress']++;
  }
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}