You are here

class MongodbFlood in MongoDB 8

Defines the mongodb flood backend.

Hierarchy

Expanded class hierarchy of MongodbFlood

1 string reference to 'MongodbFlood'
mongodb.services.yml in ./mongodb.services.yml
mongodb.services.yml
1 service uses MongodbFlood
mongodb.flood in ./mongodb.services.yml
Drupal\mongodb\MongodbFlood

File

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

Namespace

Drupal\mongodb
View source
class MongodbFlood implements FloodInterface {

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

  /**
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $requestStack;

  /**
   * Construct the DatabaseFileUsageBackend.
   *
   * @param \Drupal\mongodb\MongoCollectionFactory $mongo
   *   The database connection which will be used to store the flood
   *   information.
   */
  public function __construct(MongoCollectionFactory $mongo, RequestStack $request_stack) {
    $this->mongo = $mongo;
    $this->requestStack = $request_stack;
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::register().
   */
  public function register($name, $window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this
        ->request()
        ->getClientIp();
    }
    $data = array(
      'event' => $name,
      'identifier' => $identifier,
      'timestamp' => REQUEST_TIME,
      'expiration' => REQUEST_TIME + $window,
    );
    $this->mongo
      ->get('flood')
      ->insert($data);
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::clear().
   */
  public function clear($name, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this
        ->request()
        ->getClientIp();
    }
    $key = array(
      'event' => $name,
      'identifier' => $identifier,
    );
    $this->mongo
      ->get('flood')
      ->remove($key);
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::isAllowed().
   */
  public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this
        ->request()
        ->getClientIp();
    }
    $key = array(
      'event' => $name,
      'identifier' => $identifier,
      'timestamp' => array(
        '$gt' => REQUEST_TIME - $window,
      ),
      'expiration' => array(
        '$gt' => REQUEST_TIME,
      ),
    );
    return $this->mongo
      ->get('flood')
      ->count($key) < $threshold;
  }

  /**
   * Implements Drupal\Core\Flood\FloodInterface::garbageCollection().
   */
  public function garbageCollection() {

    // Since we use TTL collections do nothing here.
  }

  /**
   * @return \Symfony\Component\HttpFoundation\Request
   */
  protected function request() {
    return $this->requestStack
      ->getCurrentRequest();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MongodbFlood::$mongo protected property The mongodb factory registered as a service.
MongodbFlood::$requestStack protected property
MongodbFlood::clear public function Implements Drupal\Core\Flood\FloodInterface::clear(). Overrides FloodInterface::clear
MongodbFlood::garbageCollection public function Implements Drupal\Core\Flood\FloodInterface::garbageCollection(). Overrides FloodInterface::garbageCollection
MongodbFlood::isAllowed public function Implements Drupal\Core\Flood\FloodInterface::isAllowed(). Overrides FloodInterface::isAllowed
MongodbFlood::register public function Implements Drupal\Core\Flood\FloodInterface::register(). Overrides FloodInterface::register
MongodbFlood::request protected function
MongodbFlood::__construct public function Construct the DatabaseFileUsageBackend.