You are here

class MongodbFileUsage in MongoDB 8

Defines the mongodb file usage backend.

Hierarchy

Expanded class hierarchy of MongodbFileUsage

1 string reference to 'MongodbFileUsage'
mongodb.services.yml in ./mongodb.services.yml
mongodb.services.yml
1 service uses MongodbFileUsage
mongodb.file.usage in ./mongodb.services.yml
Drupal\mongodb\MongodbFileUsage

File

src/MongodbFileUsage.php, line 16
Definition of Drupal\mongodb\MongodbFileUsage.

Namespace

Drupal\mongodb
View source
class MongodbFileUsage extends FileUsageBase {

  /**
   * The mongodb factory registered as a service.
   *
   * @var \Drupal\mongodb\MongoCollectionFactory
   */
  protected $database;

  /**
   * The name of the mongodb collection used to store file usage information.
   *
   * @var string
   */
  protected $collection;

  /**
   * Construct the DatabaseFileUsageBackend.
   *
   * @param \Drupal\mongodb\MongoCollectionFactory $database
   *   The database connection which will be used to store the file usage
   *   information.
   * @param string $collection
   *   (optional) The collection to store file usage info. Defaults to 'file_usage'.
   */
  public function __construct(MongoCollectionFactory $database, $collection = 'file_usage') {
    $this->database = $database;
    $this->collection = $collection;
  }

  /**
   * {@inheritdoc}
   */
  public function add(FileInterface $file, $module, $type, $id, $count = 1) {
    $key = array(
      'fid' => (int) $file
        ->id(),
      'module' => $module,
      'type' => $type,
      'id' => (int) $id,
    );
    $this->database
      ->get($this->collection)
      ->update($key, array(
      '$inc' => array(
        'count' => $count,
      ),
    ), array(
      'upsert' => TRUE,
      'safe' => TRUE,
    ));
    parent::add($file, $module, $type, $id, $count);
  }

  /**
   * {@inheritdoc}
   */
  public function delete(FileInterface $file, $module, $type = NULL, $id = NULL, $count = 1) {
    $key = array(
      'fid' => (int) $file
        ->id(),
      'module' => $module,
    );
    if ($type && $id) {
      $key['type'] = $type;
      $key['id'] = (int) $id;
    }
    if ($count) {
      $key['count']['$lte'] = $count;
    }

    // Delete entries that have a exact or less value to prevent empty rows.
    $record = $this->database
      ->get($this->collection)
      ->remove($key, array(
      'safe' => TRUE,
    ));
    if (empty($record['n']) && $count > 0) {
      unset($key['count']);

      // Assume that we do not want to update if item is not in the collection.
      try {
        $this->database
          ->get($this->collection)
          ->update($key, array(
          '$inc' => array(
            'count' => -1 * $count,
          ),
        ), array(
          'safe' => TRUE,
        ));
      } catch (Exception $e) {
      }
    }
    parent::delete($file, $module, $type, $id, $count);
  }

  /**
   * {@inheritdoc}
   */
  public function listUsage(FileInterface $file) {
    $key = array(
      'fid' => (int) $file
        ->id(),
      'count' => array(
        '$gt' => 0,
      ),
    );
    $results = $this->database
      ->get($this->collection)
      ->find($key);
    $references = array();
    foreach ($results as $usage) {
      $references[$usage['module']][$usage['type']][$usage['id']] = $usage['count'];
    }
    return $references;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileUsageBase::$configFactory protected property The config factory.
MongodbFileUsage::$collection protected property The name of the mongodb collection used to store file usage information.
MongodbFileUsage::$database protected property The mongodb factory registered as a service.
MongodbFileUsage::add public function Records that a module is using a file. Overrides FileUsageBase::add
MongodbFileUsage::delete public function Removes a record to indicate that a module is no longer using a file. Overrides FileUsageBase::delete
MongodbFileUsage::listUsage public function Determines where a file is used. Overrides FileUsageInterface::listUsage
MongodbFileUsage::__construct public function Construct the DatabaseFileUsageBackend. Overrides FileUsageBase::__construct