CleanState.php in Feeds 8.3
File
src/Feeds/State/CleanState.php
View source
<?php
namespace Drupal\feeds\Feeds\State;
use ArrayIterator;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\feeds\State;
use RuntimeException;
class CleanState extends State implements CleanStateInterface {
use DependencySerializationTrait;
const TABLE_NAME = 'feeds_clean_list';
protected $feedId;
protected $connection;
protected $initiated = FALSE;
protected $entityTypeId;
public function __construct($feed_id, Connection $connection = NULL) {
$this->feedId = $feed_id;
if (empty($connection)) {
$this->connection = \Drupal::database();
}
else {
$this->connection = $connection;
}
}
public function progress($total, $progress) {
if (!$this
->count()) {
$this
->setCompleted();
}
return parent::progress($total, $progress);
}
public function initiated() {
return $this->initiated;
}
public function setList(array $ids) {
$this->connection
->delete(static::TABLE_NAME)
->condition('feed_id', $this->feedId)
->execute();
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();
}
$this->initiated = TRUE;
}
public function getList() {
return $this->connection
->select(static::TABLE_NAME)
->fields(static::TABLE_NAME, [
'entity_id',
])
->condition('feed_id', $this->feedId)
->execute()
->fetchCol();
}
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);
}
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;
}
$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);
}
}
public function setEntityTypeId($entity_type_id) {
$this->entityTypeId = $entity_type_id;
}
public function getEntityTypeId() {
return $this->entityTypeId;
}
public function getIterator() {
return new ArrayIterator($this
->getList());
}
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();
}
}