You are here

public function ConfigurationCheck::run in Varnish purger 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Purge/DiagnosticCheck/ConfigurationCheck.php \Drupal\varnish_purger\Plugin\Purge\DiagnosticCheck\ConfigurationCheck::run()

Perform the check and determine the severity level.

Diagnostic checks determine whether something you are checking for is in shape, for instance CMI settings on which your plugin depends. Any check reporting self::SEVERITY_ERROR in their run() methods, will cause purge to stop working. Any other severity level will let the purgers proceed operating but you may report any warning through getRecommendation() to be shown on Drupal's status report, purge_ui or any other diagnostic listing.


public function run() {
  if (...check..) {
    return self::SEVERITY_OK;
  }
  return self::SEVERITY_WARNING;
}

@warning As diagnostic checks can be expensive, this method is called as rarely as possible. Checks derived from DiagnosticCheckBase will only see the check getting executed when any of the getter methods are called.

Return value

int Integer, matching either of the following constants:

Throws

\Drupal\purge\Plugin\Purge\DiagnosticCheck\Exception\CheckNotImplementedCorrectly Thrown when the return value is incorrect.

Overrides DiagnosticCheckInterface::run

File

src/Plugin/Purge/DiagnosticCheck/ConfigurationCheck.php, line 61

Class

ConfigurationCheck
Verifies that only fully configured Varnish purgers load.

Namespace

Drupal\varnish_purger\Plugin\Purge\DiagnosticCheck

Code

public function run() {

  // Load configuration objects for all enabled HTTP purgers.
  $plugins = [];
  foreach ($this->purgePurgers
    ->getPluginsEnabled() as $id => $plugin_id) {
    if (in_array($plugin_id, [
      'varnish',
      'varnishbundled',
    ])) {
      $plugins[$id] = VarnishPurgerSettings::load($id);
    }
  }

  // Perform checks against configuration.
  $labels = $this->purgePurgers
    ->getLabels();
  foreach ($plugins as $id => $settings) {
    $t = [
      '@purger' => $labels[$id],
    ];
    foreach ([
      'name',
      'hostname',
      'port',
      'request_method',
      'scheme',
    ] as $f) {
      if (empty($settings->{$f})) {
        $this->recommendation = $this
          ->t("@purger not configured.", $t);
        return SELF::SEVERITY_ERROR;
      }
    }
    if ($settings->scheme === 'https' && $settings->port != 443) {
      $this->recommendation = $this
        ->t("@purger uses https:// but its port is not 443!", $t);
      return SELF::SEVERITY_WARNING;
    }
    if ($settings->scheme === 'http' && $settings->port == 443) {
      $this->recommendation = $this
        ->t("@purger uses http:// but its port is 443!", $t);
      return SELF::SEVERITY_WARNING;
    }
  }
  $this->recommendation = $this
    ->t("All purgers configured.");
  return SELF::SEVERITY_OK;
}