RateLimitDecoratedResource.php in RESTful 7.2
File
src/Plugin/resource/Decorators/RateLimitDecoratedResource.php
View source
<?php
namespace Drupal\restful\Plugin\resource\Decorators;
use Drupal\restful\Plugin\resource\ResourceInterface;
use Drupal\restful\RateLimit\RateLimitManager;
class RateLimitDecoratedResource extends ResourceDecoratorBase implements ResourceDecoratorInterface {
protected $rateLimitManager;
public function __construct(ResourceInterface $subject, RateLimitManager $rate_limit_manager = NULL) {
$this->subject = $subject;
$plugin_definition = $subject
->getPluginDefinition();
$rate_limit_info = empty($plugin_definition['rateLimit']) ? array() : $plugin_definition['rateLimit'];
if ($limit = variable_get('restful_global_rate_limit', 0)) {
$rate_limit_info['global'] = array(
'period' => variable_get('restful_global_rate_period', 'P1D'),
'limits' => array(),
);
foreach (user_roles() as $role_name) {
$rate_limit_info['global']['limits'][$role_name] = $limit;
}
}
$this->rateLimitManager = $rate_limit_manager ? $rate_limit_manager : new RateLimitManager($this, $rate_limit_info);
}
protected function setRateLimitManager(RateLimitManager $rate_limit_manager) {
$this->rateLimitManager = $rate_limit_manager;
}
protected function getRateLimitManager() {
return $this->rateLimitManager;
}
public function process() {
$this
->getRateLimitManager()
->checkRateLimit($this
->getRequest());
return $this->subject
->process();
}
public function setAccount($account) {
$this->subject
->setAccount($account);
$this->rateLimitManager
->setAccount($account);
}
}