class FeedExpireHandler in Feeds 8.3
Expires the items of a feed.
Hierarchy
- class \Drupal\feeds\FeedHandlerBase implements EntityHandlerInterface uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait, EventDispatcherTrait
- class \Drupal\feeds\FeedExpireHandler
Expanded class hierarchy of FeedExpireHandler
1 file declares its use of FeedExpireHandler
- FeedExpireHandlerTest.php in tests/
src/ Unit/ FeedExpireHandlerTest.php
File
- src/
FeedExpireHandler.php, line 13
Namespace
Drupal\feedsView source
class FeedExpireHandler extends FeedHandlerBase {
/**
* Starts a batch for expiring items.
*
* @param \Drupal\feeds\FeedInterface $feed
* The feed for which to expire items.
*/
public function startBatchExpire(FeedInterface $feed) {
try {
$feed
->lock();
} catch (LockException $e) {
$this
->messenger()
->addWarning($this
->t('The feed became locked before the expiring could begin.'));
return;
}
$feed
->clearStates();
$ids = $this
->getExpiredIds($feed);
if (!$ids) {
$feed
->unlock();
return;
}
$batch = [
'title' => $this
->t('Expiring: %title', [
'%title' => $feed
->label(),
]),
'init_message' => $this
->t('Expiring: %title', [
'%title' => $feed
->label(),
]),
'progress_message' => $this
->t('Expiring: %title', [
'%title' => $feed
->label(),
]),
'error_message' => $this
->t('An error occurred while expiring %title.', [
'%title' => $feed
->label(),
]),
];
foreach ($ids as $id) {
$batch['operations'][] = [
[
$this,
'expireItem',
],
[
$feed,
$id,
],
];
}
$batch['operations'][] = [
[
$this,
'postExpire',
],
[
$feed,
],
];
$this
->batchSet($batch);
}
/**
* Returns feed item ID's to expire.
*
* @param \Drupal\feeds\FeedInterface $feed
* The feed for which to get the expired item ID's.
*
* @return array
* A list of item ID's.
*/
protected function getExpiredIds(FeedInterface $feed) {
return $feed
->getType()
->getProcessor()
->getExpiredIds($feed);
}
/**
* Expires a single item imported with the given feed.
*
* @param \Drupal\feeds\FeedInterface $feed
* The feed for which to expire the item.
* @param int $item_id
* The ID of the item to expire. Usually this is an entity ID.
*
* @return float
* The progress being made on expiring.
*/
public function expireItem(FeedInterface $feed, $item_id) {
try {
$this
->dispatchEvent(FeedsEvents::INIT_EXPIRE, new InitEvent($feed));
$this
->dispatchEvent(FeedsEvents::EXPIRE, new ExpireEvent($feed, $item_id));
} catch (\RuntimeException $e) {
$this
->messenger()
->addError($e
->getMessage());
$feed
->clearStates();
$feed
->unlock();
} catch (\Exception $e) {
$feed
->clearStates();
$feed
->unlock();
throw $e;
}
return $feed
->progressExpiring();
}
/**
* Handles clean up tasks after expiring items is done.
*
* @param \Drupal\feeds\FeedInterface $feed
* The feed for which items got expired.
*/
public function postExpire(FeedInterface $feed) {
$state = $feed
->getState(StateInterface::EXPIRE);
if ($state->total) {
$this
->messenger()
->addStatus($this
->t('Expired @count items.', [
'@count' => $state->total,
]));
}
$feed
->clearStates();
$feed
->save();
$feed
->unlock();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EventDispatcherTrait:: |
private | property | The event dispatcher service. | |
EventDispatcherTrait:: |
protected | function | Dispatches an event. | |
EventDispatcherTrait:: |
protected | function | Returns the event dispatcher service. | |
EventDispatcherTrait:: |
public | function | Sets the event dispatcher service to use. | |
FeedExpireHandler:: |
public | function | Expires a single item imported with the given feed. | |
FeedExpireHandler:: |
protected | function | Returns feed item ID's to expire. | |
FeedExpireHandler:: |
public | function | Handles clean up tasks after expiring items is done. | |
FeedExpireHandler:: |
public | function | Starts a batch for expiring items. | |
FeedHandlerBase:: |
protected | function | Adds a new batch. | |
FeedHandlerBase:: |
public static | function |
Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface:: |
|
FeedHandlerBase:: |
public | function | Constructs a new FeedHandlerBase object. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |