class MongodbFlood in MongoDB 8
Defines the mongodb flood backend.
Hierarchy
- class \Drupal\mongodb\MongodbFlood implements FloodInterface
Expanded class hierarchy of MongodbFlood
1 string reference to 'MongodbFlood'
1 service uses MongodbFlood
File
- src/
MongodbFlood.php, line 16 - Definition of Drupal\mongodb\MongodbFlood.
Namespace
Drupal\mongodbView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MongodbFlood:: |
protected | property | The mongodb factory registered as a service. | |
MongodbFlood:: |
protected | property | ||
MongodbFlood:: |
public | function |
Implements Drupal\Core\Flood\FloodInterface::clear(). Overrides FloodInterface:: |
|
MongodbFlood:: |
public | function |
Implements Drupal\Core\Flood\FloodInterface::garbageCollection(). Overrides FloodInterface:: |
|
MongodbFlood:: |
public | function |
Implements Drupal\Core\Flood\FloodInterface::isAllowed(). Overrides FloodInterface:: |
|
MongodbFlood:: |
public | function |
Implements Drupal\Core\Flood\FloodInterface::register(). Overrides FloodInterface:: |
|
MongodbFlood:: |
protected | function | ||
MongodbFlood:: |
public | function | Construct the DatabaseFileUsageBackend. |