You are here

class CleanState in Feeds 8.3

State for the clean stage.

Hierarchy

Expanded class hierarchy of CleanState

3 files declare their use of CleanState
EntityProcessorBaseTest.php in tests/src/Kernel/Feeds/Processor/EntityProcessorBaseTest.php
Feed.php in src/Entity/Feed.php
FeedRefreshTest.php in tests/src/Unit/Plugin/QueueWorker/FeedRefreshTest.php

File

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

Namespace

Drupal\feeds\Feeds\State
View source
class CleanState extends State implements CleanStateInterface {
  use DependencySerializationTrait;

  /**
   * The database table name.
   */
  const TABLE_NAME = 'feeds_clean_list';

  /**
   * The ID of the feed this state belongs to.
   *
   * @var int
   */
  protected $feedId;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Whether or not the list was initiated or not.
   *
   * @var bool
   */
  protected $initiated = FALSE;

  /**
   * The type of the entity ID's on the list.
   *
   * @var string
   */
  protected $entityTypeId;

  /**
   * Constructs a new CleanState.
   *
   * @param int $feed_id
   *   The ID of the feed this state belongs to.
   * @param \Drupal\Core\Database\Connection $connection
   *   (optional) The Connection object containing the feeds tables.
   */
  public function __construct($feed_id, Connection $connection = NULL) {
    $this->feedId = $feed_id;
    if (empty($connection)) {
      $this->connection = \Drupal::database();
    }
    else {
      $this->connection = $connection;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function progress($total, $progress) {
    if (!$this
      ->count()) {
      $this
        ->setCompleted();
    }
    return parent::progress($total, $progress);
  }

  /**
   * {@inheritdoc}
   */
  public function initiated() {
    return $this->initiated;
  }

  /**
   * {@inheritdoc}
   */
  public function setList(array $ids) {

    // Remove previous list first.
    $this->connection
      ->delete(static::TABLE_NAME)
      ->condition('feed_id', $this->feedId)
      ->execute();

    // Insert the list into the database.
    if (!empty($ids)) {
      $query = $this->connection
        ->insert(static::TABLE_NAME)
        ->fields([
        'feed_id',
        'entity_id',
      ]);
      foreach ($ids as $id) {
        $query
          ->values([
          'feed_id' => $this->feedId,
          'entity_id' => $id,
        ]);
      }
      $query
        ->execute();
    }

    // Set flag that initiating is done.
    $this->initiated = TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getList() {

    // Get all ID's.
    return $this->connection
      ->select(static::TABLE_NAME)
      ->fields(static::TABLE_NAME, [
      'entity_id',
    ])
      ->condition('feed_id', $this->feedId)
      ->execute()
      ->fetchCol();
  }

  /**
   * {@inheritdoc}
   */
  public function removeItem($entity_id) {
    $this->connection
      ->delete(static::TABLE_NAME)
      ->condition('feed_id', $this->feedId)
      ->condition('entity_id', $entity_id)
      ->execute();
    $this->total = $this
      ->count();
    $this
      ->progress($this->total, $this->updated);
  }

  /**
   * {@inheritdoc}
   */
  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);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setEntityTypeId($entity_type_id) {

    // @todo check for valid entity type id.
    $this->entityTypeId = $entity_type_id;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityTypeId() {
    return $this->entityTypeId;
  }

  /**
   * {@inheritdoc}
   */
  public function getIterator() {
    return new ArrayIterator($this
      ->getList());
  }

  /**
   * {@inheritdoc}
   */
  public function count() {
    return (int) $this->connection
      ->query('SELECT COUNT(feed_id) FROM {' . static::TABLE_NAME . '} WHERE feed_id = :feed_id', [
      ':feed_id' => $this->feedId,
    ])
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CleanState::$connection protected property The database connection.
CleanState::$entityTypeId protected property The type of the entity ID's on the list.
CleanState::$feedId protected property The ID of the feed this state belongs to.
CleanState::$initiated protected property Whether or not the list was initiated or not.
CleanState::count public function
CleanState::getEntityTypeId public function Returns the entity type ID of the entity ID's on the list. Overrides CleanStateInterface::getEntityTypeId
CleanState::getIterator public function
CleanState::getList public function Returns the list of entity ID's. Overrides CleanStateInterface::getList
CleanState::initiated public function Returns if the list is initiated. Overrides CleanStateInterface::initiated
CleanState::nextEntity public function Returns the next entity in the list and removes the ID from the list. Overrides CleanStateInterface::nextEntity
CleanState::progress public function Reports the progress of a batch. Overrides State::progress
CleanState::removeItem public function Removes a specific item from the list. Overrides CleanStateInterface::removeItem
CleanState::setEntityTypeId public function Sets the entity type ID of the entity ID's on the list. Overrides CleanStateInterface::setEntityTypeId
CleanState::setList public function Sets the list of entity ID's. Overrides CleanStateInterface::setList
CleanState::TABLE_NAME constant The database table name.
CleanState::__construct public function Constructs a new CleanState.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
State::$created public property The number of Feed items created.
State::$deleted public property The number of Feed items deleted.
State::$failed public property The number of failed Feed items.
State::$messages protected property The list of messages to display to the user.
State::$pointer public property Used as a pointer to store where left off. Must be serializable.
State::$progress public property Denotes the progress made. 1
State::$skipped public property The number of Feed items skipped.
State::$total public property The total number of items being processed.
State::$updated public property The number of Feed items updated.
State::displayMessages public function Shows the messages to the user. Overrides StateInterface::displayMessages
State::logMessages public function Logs all messages. Overrides StateInterface::logMessages
State::setCompleted public function Immediately completes the batch. Overrides StateInterface::setCompleted
State::setMessage public function Sets a message to display to the user. Overrides StateInterface::setMessage
StateInterface::BATCH_COMPLETE constant Batch operation complete.
StateInterface::CLEAN constant Denotes the clean stage.
StateInterface::CLEAR constant Denotes the clear stage.
StateInterface::EXPIRE constant Denotes the expire stage.
StateInterface::FETCH constant Denotes the fetch stage.
StateInterface::PARSE constant Denotes the parse stage.
StateInterface::PROCESS constant Denotes the process stage.
StateInterface::START constant The start time key.