VarnishPurgerBase.php in Varnish purger 8.2
File
src/Plugin/Purge/Purger/VarnishPurgerBase.php
View source
<?php
namespace Drupal\varnish_purger\Plugin\Purge\Purger;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Utility\Token;
use Drupal\purge\Plugin\Purge\Purger\PurgerBase;
use Drupal\purge\Plugin\Purge\Purger\PurgerInterface;
use Drupal\varnish_purger\Entity\VarnishPurgerSettings;
abstract class VarnishPurgerBase extends PurgerBase implements PurgerInterface {
protected $client;
protected $settings;
protected $token;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ClientInterface $http_client, Token $token) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->settings = VarnishPurgerSettings::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() {
VarnishPurgerSettings::load($this
->getId())
->delete();
}
public function getCooldownTime() {
return $this->settings->cooldown_time;
}
public function getIdealConditionsLimit() {
return $this->settings->max_requests;
}
protected function getHeaders($token_data) {
$headers = [];
$headers['user-agent'] = 'varnish_purger module for Drupal 8.';
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($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 ($this->settings->scheme === 'https') {
$opt['verify'] = (bool) $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($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;
}
}