You are here

class FetcherResult in Feeds 8.3

The default fetcher result object.

Hierarchy

Expanded class hierarchy of FetcherResult

5 files declare their use of FetcherResult
CsvParserTest.php in tests/src/Unit/Feeds/Parser/CsvParserTest.php
DirectoryFetcher.php in src/Feeds/Fetcher/DirectoryFetcher.php
FeedRefreshTest.php in tests/src/Unit/Plugin/QueueWorker/FeedRefreshTest.php
FetcherResultTest.php in tests/src/Unit/Result/FetcherResultTest.php
UploadFetcher.php in src/Feeds/Fetcher/UploadFetcher.php

File

src/Result/FetcherResult.php, line 10

Namespace

Drupal\feeds\Result
View source
class FetcherResult implements FetcherResultInterface {

  /**
   * The filepath of the fetched item.
   *
   * @var string
   */
  protected $filePath;

  /**
   * Constructs a new FetcherResult object.
   *
   * @param string $file_path
   *   The path to the result file.
   */
  public function __construct($file_path) {
    $this->filePath = $file_path;
  }

  /**
   * {@inheritdoc}
   */
  public function getRaw() {
    $this
      ->checkFile();
    return $this
      ->sanitizeRaw(file_get_contents($this->filePath));
  }

  /**
   * {@inheritdoc}
   */
  public function getFilePath() {
    $this
      ->checkFile();
    return $this
      ->sanitizeFile();
  }

  /**
   * Checks that a file exists and is readable.
   *
   * @throws \RuntimeException
   *   Thrown if the file isn't readable or writable.
   */
  protected function checkFile() {
    if (!file_exists($this->filePath)) {
      throw new \RuntimeException(new FormattableMarkup('File %filepath does not exist.', [
        '%filepath' => $this->filePath,
      ]));
    }
    if (!is_readable($this->filePath)) {
      throw new \RuntimeException(new FormattableMarkup('File %filepath is not readable.', [
        '%filepath' => $this->filePath,
      ]));
    }
  }

  /**
   * Sanitizes the raw content string.
   *
   * Currently supported sanitizations:
   * - Remove BOM header from UTF-8 files.
   *
   * @param string $raw
   *   The raw content string to be sanitized.
   *
   * @return string
   *   The sanitized content as a string.
   */
  protected function sanitizeRaw($raw) {
    if (substr($raw, 0, 3) == pack('CCC', 0xef, 0xbb, 0xbf)) {
      $raw = substr($raw, 3);
    }
    return $raw;
  }

  /**
   * Sanitizes the file in place.
   *
   * Currently supported sanitizations:
   * - Remove BOM header from UTF-8 files.
   *
   * @return string
   *   The file path of the sanitized file.
   *
   * @throws \RuntimeException
   *   Thrown if the file is not writable.
   */
  protected function sanitizeFile() {
    $handle = fopen($this->filePath, 'r');
    $line = fgets($handle);
    fclose($handle);

    // If BOM header is present, read entire contents of file and overwrite the
    // file with corrected contents.
    if (substr($line, 0, 3) !== pack('CCC', 0xef, 0xbb, 0xbf)) {
      return $this->filePath;
    }
    if (!is_writable($this->filePath)) {
      throw new \RuntimeException(new FormattableMarkup('File %filepath is not writable.', [
        '%filepath' => $this->filePath,
      ]));
    }
    $contents = file_get_contents($this->filePath);
    $contents = substr($contents, 3);
    $status = file_put_contents($this->filePath, $contents);
    return $this->filePath;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FetcherResult::$filePath protected property The filepath of the fetched item.
FetcherResult::checkFile protected function Checks that a file exists and is readable.
FetcherResult::getFilePath public function Returns the path to the file containing the file provided by the fetcher. Overrides FetcherResultInterface::getFilePath 1
FetcherResult::getRaw public function Returns the file provided by the fetcher as a string. Overrides FetcherResultInterface::getRaw 1
FetcherResult::sanitizeFile protected function Sanitizes the file in place.
FetcherResult::sanitizeRaw protected function Sanitizes the raw content string.
FetcherResult::__construct public function Constructs a new FetcherResult object. 2