You are here

class FileMetadata in File metadata manager 8

Same name in this branch
  1. 8 src/FileMetadata.php \Drupal\file_mdm\FileMetadata
  2. 8 src/Plugin/Annotation/FileMetadata.php \Drupal\file_mdm\Plugin\Annotation\FileMetadata
Same name and namespace in other branches
  1. 8.2 src/FileMetadata.php \Drupal\file_mdm\FileMetadata

A file metadata object.

Hierarchy

Expanded class hierarchy of FileMetadata

File

src/FileMetadata.php, line 13

Namespace

Drupal\file_mdm
View source
class FileMetadata implements FileMetadataInterface {

  /**
   * The FileMetadata plugin manager.
   *
   * @var \Drupal\file_mdm\Plugin\FileMetadataPluginManager
   */
  protected $pluginManager;

  /**
   * The file_mdm logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The URI of the file.
   *
   * @var string
   */
  protected $uri = '';

  /**
   * The hash used to reference the URI.
   *
   * @var string
   */
  protected $hash;

  /**
   * The local filesystem path to the file.
   *
   * This is used to allow accessing local copies of files stored remotely, to
   * minimise remote calls and allow functions that cannot access remote stream
   * wrappers to operate locally.
   *
   * @var string
   */
  protected $localTempPath;

  /**
   * The array of FileMetadata plugins for this URI.
   *
   * @var \Drupal\file_mdm\Plugin\FileMetadataPluginInterface[]
   */
  protected $plugins = [];

  /**
   * Constructs a FileMetadata object.
   *
   * @param \Drupal\file_mdm\Plugin\FileMetadataPluginManager $plugin_manager
   *   The file metadata plugin manager.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system service.
   * @param string $uri
   *   The URI of the file.
   * @param string $hash
   *   The hash used to reference the URI by file_mdm.
   */
  public function __construct(FileMetadataPluginManager $plugin_manager, LoggerInterface $logger, FileSystemInterface $file_system, $uri, $hash) {
    $this->pluginManager = $plugin_manager;
    $this->logger = $logger;
    $this->fileSystem = $file_system;
    $this->uri = $uri;
    $this->hash = $hash;
  }

  /**
   * {@inheritdoc}
   */
  public function getUri() {
    return $this->uri;
  }

  /**
   * {@inheritdoc}
   */
  public function getLocalTempPath() {
    return $this->localTempPath;
  }

  /**
   * {@inheritdoc}
   */
  public function setLocalTempPath($temp_uri) {
    $this->localTempPath = $temp_uri;
    foreach ($this->plugins as $plugin) {
      $plugin
        ->setLocalTempPath($this->localTempPath);
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function copyUriToTemp($temp_uri = NULL) {
    if ($temp_uri === NULL) {
      $temp_uri = $this->fileSystem
        ->tempnam('temporary://', 'file_mdm_');
      $this->fileSystem
        ->unlink($temp_uri);
      $temp_uri .= '.' . pathinfo($this
        ->getUri(), PATHINFO_EXTENSION);
    }
    if ($temp_path = file_unmanaged_copy($this
      ->getUri(), $this->fileSystem
      ->realpath($temp_uri), FILE_EXISTS_REPLACE)) {
      $this
        ->setLocalTempPath($temp_path);
    }
    return (bool) $temp_path;
  }

  /**
   * {@inheritdoc}
   */
  public function copyTempToUri() {
    if (($temp_path = $this
      ->getLocalTempPath()) === NULL) {
      return FALSE;
    }
    return (bool) file_unmanaged_copy($temp_path, $this
      ->getUri(), FILE_EXISTS_REPLACE);
  }

  /**
   * {@inheritdoc}
   */
  public function getFileMetadataPlugin($metadata_id) {
    if (!isset($this->plugins[$metadata_id])) {
      try {
        $this->plugins[$metadata_id] = $this->pluginManager
          ->createInstance($metadata_id);
        $this->plugins[$metadata_id]
          ->setUri($this->uri);
        $this->plugins[$metadata_id]
          ->setLocalTempPath($this->localTempPath ?: $this->uri);
        $this->plugins[$metadata_id]
          ->setHash($this->hash);
      } catch (PluginNotFoundException $e) {
        return NULL;
      }
    }
    return $this->plugins[$metadata_id];
  }

  /**
   * {@inheritdoc}
   */
  public function getSupportedKeys($metadata_id, $options = NULL) {
    try {
      if ($plugin = $this
        ->getFileMetadataPlugin($metadata_id)) {
        $keys = $plugin
          ->getSupportedKeys($options);
      }
      else {
        $keys = NULL;
      }
    } catch (\Exception $e) {
      $this->logger
        ->error($e
        ->getMessage());
      $keys = NULL;
    }
    return $keys;
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadata($metadata_id, $key = NULL) {
    try {
      if ($plugin = $this
        ->getFileMetadataPlugin($metadata_id)) {
        $metadata = $plugin
          ->getMetadata($key);
      }
      else {
        $metadata = NULL;
      }
    } catch (\Exception $e) {
      $this->logger
        ->error($e
        ->getMessage());
      $metadata = NULL;
    }
    return $metadata;
  }

  /**
   * {@inheritdoc}
   */
  public function removeMetadata($metadata_id, $key) {
    try {
      if ($plugin = $this
        ->getFileMetadataPlugin($metadata_id)) {
        return $plugin
          ->removeMetadata($key);
      }
    } catch (\Exception $e) {
      $this->logger
        ->error($e
        ->getMessage());
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setMetadata($metadata_id, $key, $value) {
    try {
      if ($plugin = $this
        ->getFileMetadataPlugin($metadata_id)) {
        return $plugin
          ->setMetadata($key, $value);
      }
    } catch (\Exception $e) {
      $this->logger
        ->error($e
        ->getMessage());
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isMetadataLoaded($metadata_id) {
    if ($plugin = $this
      ->getFileMetadataPlugin($metadata_id)) {
      return $plugin
        ->isMetadataLoaded();
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function loadMetadata($metadata_id, $metadata) {
    if ($plugin = $this
      ->getFileMetadataPlugin($metadata_id)) {
      return $plugin
        ->loadMetadata($metadata);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function loadMetadataFromCache($metadata_id) {
    if ($plugin = $this
      ->getFileMetadataPlugin($metadata_id)) {
      return $plugin
        ->loadMetadataFromCache();
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function saveMetadataToCache($metadata_id, array $tags = []) {
    if ($plugin = $this
      ->getFileMetadataPlugin($metadata_id)) {
      return $plugin
        ->saveMetadataToCache($tags);
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function saveMetadataToFile($metadata_id) {
    if ($plugin = $this
      ->getFileMetadataPlugin($metadata_id)) {
      return $plugin
        ->saveMetadataToFile();
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileMetadata::$fileSystem protected property The file system service.
FileMetadata::$hash protected property The hash used to reference the URI.
FileMetadata::$localTempPath protected property The local filesystem path to the file.
FileMetadata::$logger protected property The file_mdm logger.
FileMetadata::$pluginManager protected property The FileMetadata plugin manager.
FileMetadata::$plugins protected property The array of FileMetadata plugins for this URI.
FileMetadata::$uri protected property The URI of the file.
FileMetadata::copyTempToUri public function Copies the local temporary file to the destination URI. Overrides FileMetadataInterface::copyTempToUri
FileMetadata::copyUriToTemp public function Copies the file at URI to a local temporary file. Overrides FileMetadataInterface::copyUriToTemp
FileMetadata::getFileMetadataPlugin public function Gets a FileMetadata plugin instance. Overrides FileMetadataInterface::getFileMetadataPlugin
FileMetadata::getLocalTempPath public function Gets the local filesystem URI to the temporary file. Overrides FileMetadataInterface::getLocalTempPath
FileMetadata::getMetadata public function Gets a metadata element. Overrides FileMetadataInterface::getMetadata
FileMetadata::getSupportedKeys public function Returns a list of supported metadata keys. Overrides FileMetadataInterface::getSupportedKeys
FileMetadata::getUri public function Gets the URI of the file. Overrides FileMetadataInterface::getUri
FileMetadata::isMetadataLoaded public function Checks if file metadata has been already loaded. Overrides FileMetadataInterface::isMetadataLoaded
FileMetadata::loadMetadata public function Loads file metadata. Overrides FileMetadataInterface::loadMetadata
FileMetadata::loadMetadataFromCache public function Loads file metadata from a cache entry. Overrides FileMetadataInterface::loadMetadataFromCache
FileMetadata::removeMetadata public function Removes a metadata element. Overrides FileMetadataInterface::removeMetadata
FileMetadata::saveMetadataToCache public function Caches metadata for file at URI. Overrides FileMetadataInterface::saveMetadataToCache
FileMetadata::saveMetadataToFile public function Saves metadata to file at URI. Overrides FileMetadataInterface::saveMetadataToFile
FileMetadata::setLocalTempPath public function Sets the local filesystem URI to the temporary file. Overrides FileMetadataInterface::setLocalTempPath
FileMetadata::setMetadata public function Sets a metadata element. Overrides FileMetadataInterface::setMetadata
FileMetadata::__construct public function Constructs a FileMetadata object.
FileMetadataInterface::LOADED_BY_CODE constant Metadata loaded by code.
FileMetadataInterface::LOADED_FROM_CACHE constant Metadata loaded from cache.
FileMetadataInterface::LOADED_FROM_FILE constant Metadata loaded from file.
FileMetadataInterface::NOT_LOADED constant Metadata not loaded.