You are here

public function DirectoryFetcher::fetch in Feeds 8.3

Fetch content from a feed and return it.

Parameters

\Drupal\feeds\FeedInterface $feed: The feed to fetch results for.

\Drupal\feeds\StateInterface $state: The state object.

Return value

\Drupal\feeds\Result\FetcherResultInterface A fetcher result object.

Overrides FetcherInterface::fetch

File

src/Feeds/Fetcher/DirectoryFetcher.php, line 31

Class

DirectoryFetcher
Defines a directory fetcher.

Namespace

Drupal\feeds\Feeds\Fetcher

Code

public function fetch(FeedInterface $feed, StateInterface $state) {
  $path = $feed
    ->getSource();

  // Just return a file fetcher result if this is a file. Make sure to
  // re-validate the file extension in case the feed type settings have
  // changed.
  if (is_file($path)) {
    if (File::validateExtension($path, $this->configuration['allowed_extensions'])) {
      return new FetcherResult($path);
    }
    else {
      throw new \RuntimeException($this
        ->t('%source has an invalid file extension.', [
        '%source' => $path,
      ]));
    }
  }
  if (!is_dir($path) || !is_readable($path)) {
    throw new \RuntimeException($this
      ->t('%source is not a readable directory or file.', [
      '%source' => $path,
    ]));
  }

  // Batch if this is a directory.
  if (!isset($state->files)) {
    $state->files = $this
      ->listFiles($path);
    $state->total = count($state->files);
  }
  if ($state->files) {
    $file = array_shift($state->files);
    $state
      ->progress($state->total, $state->total - count($state->files));
    return new FetcherResult($file);
  }
  throw new EmptyFeedException();
}