DailyTagPurgeLimitCheck.php in CloudFlare 8
File
modules/cloudflarepurger/src/Plugin/Purge/DiagnosticCheck/DailyTagPurgeLimitCheck.php
View source
<?php
namespace Drupal\cloudflarepurger\Plugin\Purge\DiagnosticCheck;
use Drupal\purge\Plugin\Purge\DiagnosticCheck\DiagnosticCheckBase;
use Drupal\purge\Plugin\Purge\DiagnosticCheck\DiagnosticCheckInterface;
use Drupal\cloudflare\CloudFlareStateInterface;
use CloudFlarePhpSdk\ApiEndpoints\CloudFlareAPI;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DailyTagPurgeLimitCheck extends DiagnosticCheckBase implements DiagnosticCheckInterface {
protected $state;
protected $areCloudFlareComposerDependenciesMet;
public function __construct(array $configuration, $plugin_id, $plugin_definition, CloudFlareStateInterface $state, $composer_dependencies_met) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->state = $state;
$this->areCloudFlareComposerDependenciesMet = $composer_dependencies_met;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('cloudflare.state'), $container
->get('cloudflare.composer_dependency_check')
->check());
}
public function run() {
if (!$this->areCloudFlareComposerDependenciesMet) {
$this->recommendation = $this
->t("Composer dependencies unmet. Unable to assess API rate limits.");
return SELF::SEVERITY_ERROR;
}
$daily_count = $this->state
->getTagDailyCount();
$this->value = $daily_count;
$daily_warning_level = 0.75 * CloudFlareAPI::API_TAG_PURGE_DAILY_RATE_LIMIT;
$message_variables = [
':daily_limit' => CloudFlareAPI::API_TAG_PURGE_DAILY_RATE_LIMIT,
':$daily_count' => $daily_count,
];
if ($daily_count >= CloudFlareAPI::API_TAG_PURGE_DAILY_RATE_LIMIT) {
$this->recommendation = $this
->t('Past Api limit of :daily_count/:daily_limit limit tag purges/day.', $message_variables);
return SELF::SEVERITY_ERROR;
}
elseif ($daily_count >= $daily_warning_level) {
$this->recommendation = $this
->t('Approaching Api limit of :daily_count/:daily_limit limit tag purges/day.', $message_variables);
return SELF::SEVERITY_WARNING;
}
elseif ($daily_count < $daily_warning_level) {
$this->recommendation = $this
->t('Site is safely below the daily limit of :daily_limit tag purges/day.', $message_variables);
return SELF::SEVERITY_OK;
}
}
}