You are here

class FeedImportHandler in Feeds 8.3

Runs the actual import on a feed.

Hierarchy

Expanded class hierarchy of FeedImportHandler

1 file declares its use of FeedImportHandler
FeedImportHandlerTest.php in tests/src/Unit/FeedImportHandlerTest.php

File

src/FeedImportHandler.php, line 12

Namespace

Drupal\feeds
View source
class FeedImportHandler extends FeedHandlerBase {

  /**
   * Imports the whole feed at once.
   *
   * @param \Drupal\feeds\FeedInterface $feed
   *   The feed to import for.
   *
   * @throws \Exception
   *   In case of an error.
   */
  public function import(FeedInterface $feed) {
    $this
      ->getExecutable(FeedsExecutable::class)
      ->processItem($feed, FeedsExecutable::BEGIN);
  }

  /**
   * Starts importing a feed via the batch API.
   *
   * @param \Drupal\feeds\FeedInterface $feed
   *   The feed to import.
   *
   * @throws \Drupal\feeds\Exception\LockException
   *   Thrown if a feed is locked.
   */
  public function startBatchImport(FeedInterface $feed) {
    $this
      ->getExecutable(FeedsBatchExecutable::class)
      ->processItem($feed, FeedsBatchExecutable::BEGIN);
  }

  /**
   * Starts importing a feed via cron.
   *
   * @param \Drupal\feeds\FeedInterface $feed
   *   The feed to queue.
   *
   * @throws \Drupal\feeds\Exception\LockException
   *   Thrown if a feed is locked.
   */
  public function startCronImport(FeedInterface $feed) {
    if ($feed
      ->isLocked()) {
      $args = [
        '@id' => $feed
          ->bundle(),
        '@fid' => $feed
          ->id(),
      ];
      throw new LockException($this
        ->t('The feed @id / @fid is locked.', $args));
    }
    $this
      ->getExecutable(FeedsQueueExecutable::class)
      ->processItem($feed, FeedsQueueExecutable::BEGIN);

    // Add timestamp to avoid queueing item more than once.
    $feed
      ->setQueuedTime($this
      ->getRequestTime());
    $feed
      ->save();
  }

  /**
   * Handles a push import.
   *
   * @param \Drupal\feeds\FeedInterface $feed
   *   The feed receiving the push.
   * @param string $payload
   *   The feed contents.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   (optional) The file system service.
   */
  public function pushImport(FeedInterface $feed, $payload, FileSystemInterface $file_system = NULL) {
    $feed
      ->lock();
    $fetcher_result = new RawFetcherResult($payload, $file_system);
    $this
      ->getExecutable(FeedsQueueExecutable::class)
      ->processItem($feed, FeedsQueueExecutable::PARSE, [
      'fetcher_result' => $fetcher_result,
    ]);
  }

  /**
   * Returns the timestamp for the current request.
   *
   * @return int
   *   A Unix timestamp.
   */
  protected function getRequestTime() {
    return \Drupal::time()
      ->getRequestTime();
  }

  /**
   * Returns the executable.
   *
   * @param string $class
   *   The class to load.
   *
   * @return \Drupal\feeds\FeedsExecutableInterface
   *   A feeds executable.
   */
  protected function getExecutable($class) {
    return \Drupal::service('class_resolver')
      ->getInstanceFromDefinition($class);
  }

}

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.
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.
FeedImportHandler::getExecutable protected function Returns the executable.
FeedImportHandler::getRequestTime protected function Returns the timestamp for the current request.
FeedImportHandler::import public function Imports the whole feed at once.
FeedImportHandler::pushImport public function Handles a push import.
FeedImportHandler::startBatchImport public function Starts importing a feed via the batch API.
FeedImportHandler::startCronImport public function Starts importing a feed via cron.
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.