You are here

public function CleanState::nextEntity in Feeds 8.3

Returns the next entity in the list and removes the ID from the list.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: (optional) The entity storage.

Return value

\Drupal\Core\Entity\EntityInterface|null Returns the next the entity in the list, if found.

Overrides CleanStateInterface::nextEntity

File

src/Feeds/State/CleanState.php, line 143

Class

CleanState
State for the clean stage.

Namespace

Drupal\feeds\Feeds\State

Code

public function nextEntity(EntityStorageInterface $storage = NULL) {
  if (!$this
    ->initiated()) {
    return;
  }
  $entity_id = $this->connection
    ->queryRange('SELECT entity_id FROM {' . static::TABLE_NAME . '} WHERE feed_id = :feed_id', 0, 1, [
    ':feed_id' => $this->feedId,
  ])
    ->fetchField();
  if (!$entity_id) {
    return;
  }

  // Claim the item, remove it from the list.
  $this
    ->removeItem($entity_id);
  if (!$storage) {
    $entity_type_id = $this
      ->getEntityTypeId();
    if (!$entity_type_id) {
      throw new RuntimeException('The clean state does not have an entity type assigned.');
    }
    $storage = \Drupal::entityTypeManager()
      ->getStorage($this
      ->getEntityTypeId());
  }
  $entity = $storage
    ->load($entity_id);
  if ($entity instanceof EntityInterface) {
    return $entity;
  }
  else {
    return $this
      ->nextEntity($storage);
  }
}