You are here

class FeedExpireHandler in Feeds 8.3

Expires the items of a feed.

Hierarchy

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\feeds
View 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

Namesort descending Modifiers Type Description Overrides
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
EventDispatcherTrait::$_eventDispatcher private property The event dispatcher service.
EventDispatcherTrait::dispatchEvent protected function Dispatches an event.
EventDispatcherTrait::getEventDispatcher protected function Returns the event dispatcher service.
EventDispatcherTrait::setEventDispatcher public function Sets the event dispatcher service to use.
FeedExpireHandler::expireItem public function Expires a single item imported with the given feed.
FeedExpireHandler::getExpiredIds protected function Returns feed item ID's to expire.
FeedExpireHandler::postExpire public function Handles clean up tasks after expiring items is done.
FeedExpireHandler::startBatchExpire public function Starts a batch for expiring items.
FeedHandlerBase::batchSet protected function Adds a new batch.
FeedHandlerBase::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
FeedHandlerBase::__construct public function Constructs a new FeedHandlerBase object.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.