HttpPurgerBase.php in Generic HTTP Purger 8
File
src/Plugin/Purge/Purger/HttpPurgerBase.php
View source
<?php
namespace Drupal\purge_purger_http\Plugin\Purge\Purger;
use Drupal\Core\Utility\Token;
use Drupal\purge\Plugin\Purge\Purger\PurgerBase;
use Drupal\purge\Plugin\Purge\Purger\PurgerInterface;
use Drupal\purge_purger_http\Entity\HttpPurgerSettings;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class HttpPurgerBase extends PurgerBase implements PurgerInterface {
protected $client;
protected $settings;
protected $token;
public final function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $http_client, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->settings = HttpPurgerSettings::load($this
->getId());
$this->client = $http_client;
$this->token = $token;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('http_client'), $container
->get('token'));
}
public function delete() {
HttpPurgerSettings::load($this
->getId())
->delete();
}
public function getCooldownTime() {
return $this->settings->cooldown_time;
}
public function getIdealConditionsLimit() {
return $this->settings->max_requests;
}
protected function getHeaders(array $token_data) {
$headers = [];
$headers['user-agent'] = 'purge_purger_http module for Drupal 8.';
if (strlen($this->settings->body)) {
$headers['content-type'] = $this->settings->body_content_type;
}
foreach ($this->settings->headers as $header) {
$headers[strtolower($header['field'])] = $this->token
->replace($header['value'], $token_data);
}
return $headers;
}
public function getLabel() {
if ($this->settings->name) {
return $this->settings->name;
}
else {
return parent::getLabel();
}
}
protected function getOptions(array $token_data) {
$opt = [
'http_errors' => $this->settings->http_errors,
'connect_timeout' => $this->settings->connect_timeout,
'timeout' => $this->settings->timeout,
'headers' => $this
->getHeaders($token_data),
];
if (strlen($this->settings->body)) {
$opt['body'] = $this->token
->replace($this->settings->body, $token_data);
}
if ($this->settings->scheme === 'https') {
$opt['verify'] = $this->settings->verify;
}
return $opt;
}
public function getTimeHint() {
if ($this->settings->runtime_measurement) {
return parent::getTimeHint();
}
return $this->settings->connect_timeout + $this->settings->timeout;
}
public function getTypes() {
return [
$this->settings->invalidationtype,
];
}
protected function getUri(array $token_data) {
return sprintf('%s://%s:%s%s', $this->settings->scheme, $this->settings->hostname, $this->settings->port, $this->token
->replace($this->settings->path, $token_data));
}
public function hasRuntimeMeasurement() {
return (bool) $this->settings->runtime_measurement;
}
}