You are here

public function ResourceWarmer::buildIdsBatch in JSON:API Boost 8

Same name and namespace in other branches
  1. 2.x src/Plugin/warmer/ResourceWarmer.php \Drupal\jsonapi_boost\Plugin\warmer\ResourceWarmer::buildIdsBatch()

Builds the next batch of IDs based on a position cursor.

Parameters

mixed $cursor: The position of the last generate batch.

Return value

array The array of item IDs.

Overrides WarmerInterface::buildIdsBatch

File

src/Plugin/warmer/ResourceWarmer.php, line 129

Class

ResourceWarmer
Warms JSON:API resources.

Namespace

Drupal\jsonapi_boost\Plugin\warmer

Code

public function buildIdsBatch($cursor) {
  $configuration = $this
    ->getConfiguration();
  if (empty($this->iids) && !empty($configuration['resource_types'])) {
    $resource_config_ids = array_filter(array_values($configuration['resource_types']));
    sort($resource_config_ids);
    $this->iids = array_reduce($resource_config_ids, function ($iids, $resource_config_id) {
      $resource_config = $this->entityTypeManager
        ->getStorage('jsonapi_resource_config')
        ->load($resource_config_id);
      $resource_type_name = $resource_config instanceof JsonapiResourceConfig ? $resource_config
        ->get('resourceType') : $resource_config_id;
      $resource_type = $this->resourceTypeRepository
        ->getByTypeName($resource_type_name);
      $entity_type_id = $resource_type
        ->getEntityTypeId();
      $entity_type = $this->entityTypeManager
        ->getDefinition($entity_type_id);
      $bundle_key = $entity_type
        ->getKey('bundle');
      $id_key = $entity_type
        ->getKey('id');
      $query = $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->getQuery();
      if ($id_key) {
        $query
          ->sort($id_key);
      }
      if ($bundle_key) {
        $query
          ->condition($bundle_key, $resource_type
          ->getBundle());
      }
      $results = $query
        ->execute();
      $entity_ids = array_filter((array) array_values($results));
      return array_unique(array_merge($iids, array_map(function ($id) use ($resource_config_id, $entity_type_id) {
        return sprintf('%s:%s:%s', $entity_type_id, $id, $resource_config_id);
      }, $entity_ids)));
    }, []);
  }
  $cursor_position = is_null($cursor) ? -1 : array_search($cursor, $this->iids);
  if ($cursor_position === FALSE) {
    return [];
  }
  return array_slice($this->iids, $cursor_position + 1, (int) $this
    ->getBatchSize());
}