You are here

class ImageReplaceDatabaseStorage in Image Replace 8

Defines a class for image_replace database storage operations.

Hierarchy

Expanded class hierarchy of ImageReplaceDatabaseStorage

1 string reference to 'ImageReplaceDatabaseStorage'
image_replace.services.yml in ./image_replace.services.yml
image_replace.services.yml
1 service uses ImageReplaceDatabaseStorage
image_replace.storage in ./image_replace.services.yml
Drupal\image_replace\ImageReplaceDatabaseStorage

File

src/ImageReplaceDatabaseStorage.php, line 10

Namespace

Drupal\image_replace
View source
class ImageReplaceDatabaseStorage implements ImageReplaceStorageInterface {

  /**
   * Active database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Constructs a ImageReplaceDatabaseStorage object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection to be used.
   */
  public function __construct(Connection $database) {
    $this->database = $database;
  }

  /**
   * {@inheritdoc}
   */
  public function get($target_style, $target_uri) {
    $target_uri_hash = hash('sha256', $target_uri);
    return $this->database
      ->select('image_replace')
      ->fields('image_replace', [
      'replacement_uri',
    ])
      ->condition('target_style', $target_style)
      ->condition('target_uri_hash', $target_uri_hash)
      ->execute()
      ->fetchField();
  }

  /**
   * {@inheritdoc}
   */
  public function add($target_style, $target_uri, $replacement_uri) {
    $target_uri_hash = hash('sha256', $target_uri);
    return $this->database
      ->insert('image_replace')
      ->fields([
      'target_style' => $target_style,
      'target_uri_hash' => $target_uri_hash,
      'replacement_uri' => $replacement_uri,
    ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function remove($target_style, $target_uri) {
    $target_uri_hash = hash('sha256', $target_uri);
    return $this->database
      ->delete('image_replace')
      ->condition('target_style', $target_style)
      ->condition('target_uri_hash', $target_uri_hash)
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageReplaceDatabaseStorage::$database protected property Active database connection.
ImageReplaceDatabaseStorage::add public function Add an image replacement mapping. Overrides ImageReplaceStorageInterface::add
ImageReplaceDatabaseStorage::get public function Determine replacement image uri for the given original filename. Overrides ImageReplaceStorageInterface::get
ImageReplaceDatabaseStorage::remove public function Remove the given image replacement mapping if it exists. Overrides ImageReplaceStorageInterface::remove
ImageReplaceDatabaseStorage::__construct public function Constructs a ImageReplaceDatabaseStorage object.