You are here

abstract class SiteAuditCheckBase in Site Audit 8.3

Base class for Site Audit Check plugins.

Hierarchy

Expanded class hierarchy of SiteAuditCheckBase

56 files declare their use of SiteAuditCheckBase
BestPracticesFast404.php in src/Plugin/SiteAuditCheck/BestPracticesFast404.php
BestPracticesFolderStructure.php in src/Plugin/SiteAuditCheck/BestPracticesFolderStructure.php
BestPracticesMultisite.php in src/Plugin/SiteAuditCheck/BestPracticesMultisite.php
BestPracticesServices.php in src/Plugin/SiteAuditCheck/BestPracticesServices.php
BestPracticesSettings.php in src/Plugin/SiteAuditCheck/BestPracticesSettings.php

... See full list

File

src/Plugin/SiteAuditCheckBase.php, line 11

Namespace

Drupal\site_audit\Plugin
View source
abstract class SiteAuditCheckBase extends PluginBase implements SiteAuditCheckInterface {
  use StringTranslationTrait;
  const AUDIT_CHECK_SCORE_INFO = 3;
  const AUDIT_CHECK_SCORE_PASS = 2;
  const AUDIT_CHECK_SCORE_WARN = 1;
  const AUDIT_CHECK_SCORE_FAIL = 0;

  /**
   * Quantifiable number associated with result on a scale of 0 to 2.
   *
   * @var int
   */
  protected $score;

  /**
   * Names of checks that should not run as a result of this check.
   *
   * @var array
   */
  protected $abort = [];

  /**
   * User has opted out of this check in configuration.
   *
   * @var bool
   */
  protected $optOut = FALSE;

  /**
   * If set, will override the Report's percentage.
   *
   * @var int
   */
  protected $percentOverride;

  /**
   * Use for passing data between checks within a report.
   *
   * @var array
   */
  protected $registry;

  /**
   * Are we in a static context.
   *
   * @var bool
   */
  protected $static = TRUE;

  /**
   * Options passed in for reports and checks.
   *
   * @var array
   */
  protected $options = [];

  /**
   * Constructor.
   *
   * @param array $registry
   *   Aggregates data from each individual check.
   * @param bool $opt_out
   *   If set, will not perform checks.
   */
  public function __construct($configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    if (isset($configuration['options'])) {
      $this->options = $configuration['options'];
    }
    $this->registry = $configuration['registry'];
    if (isset($configuration['opt_out']) && !empty($configuration['opt_out'])) {
      $this->optOut = TRUE;
      $this->score = SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO;
    }
    $static = FALSE;
  }

  /**
   * Determine the result message based on the score.
   *
   * @return string
   *   Human readable message for a given status.
   */
  public function getResult() {
    if ($this->optOut) {
      return $this
        ->t('Opted-out in site configuration.');
    }
    switch ($this->score) {
      case SiteAuditCheckBase::AUDIT_CHECK_SCORE_PASS:
        return $this
          ->getResultPass();
      case SiteAuditCheckBase::AUDIT_CHECK_SCORE_WARN:
        return $this
          ->getResultWarn();
      case SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO:
        return $this
          ->getResultInfo();
      default:
        return $this
          ->getResultFail();
    }
  }

  /**
   * Get a human readable label for a score.
   *
   * @return string
   *   Pass, Recommendation and so forth.
   */
  public function getScoreLabel() {
    switch ($this->score) {
      case SiteAuditCheckBase::AUDIT_CHECK_SCORE_PASS:
        return $this
          ->t('Pass');
      case SiteAuditCheckBase::AUDIT_CHECK_SCORE_WARN:
        return $this
          ->t('Warning');
      case SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO:
        return $this
          ->t('Information');
      default:
        return $this
          ->t('Blocker');
    }
  }

  /**
   * Get the ID or machine name for the check.
   *
   * @return string
   *   The ID or machine name for the check.
   */
  public function getId() {
    return $this
      ->getPluginDefinition()['id'];
  }

  /**
   * Get the label for the check that describes, high level what is happening.
   *
   * @return string
   *   Get the label for the check that describes, high level what is happening.
   */
  public function getLabel() {
    return $this
      ->getPluginDefinition()['name'];
  }

  /**
   * Get a more verbose description of what is being checked.
   *
   * @return string
   *   A sentence describing the check; shown in detail mode.
   */
  public function getDescription() {
    return $this
      ->getPluginDefinition()['description'];
  }

  /**
   * Get the description of what happened in a failed check.
   *
   * @return string
   *   Something is explicitly wrong and requires action.
   */
  public abstract function getResultFail();

  /**
   * Get the result of a purely informational check.
   *
   * @return string
   *   Purely informational response.
   */
  public abstract function getResultInfo();

  /**
   * Get a description of what happened in a passed check.
   *
   * @return string
   *   Success; good job.
   */
  public abstract function getResultPass();

  /**
   * Get a description of what happened in a warning check.
   *
   * @return string
   *   Something is wrong, but not horribly so.
   */
  public abstract function getResultWarn();

  /**
   * Get action items for a user to perform if the check did not pass.
   *
   * @return string
   *   Actionable tasks to perform.
   */
  public abstract function getAction();

  /**
   * Display action items for a user to perform.
   *
   * @return string
   *   Actionable tasks to perform, or nothing if check is opted-out.
   */
  public function renderAction() {
    if ($this->optOut) {
      return '';
    }
    return $this
      ->getAction();
  }

  /**
   * Calculate the score.
   *
   * @return int
   *   Constants indicating pass, fail and so forth.
   */
  public abstract function calculateScore();

  /**
   * Get a quantifiable number representing a check result; lazy initialization.
   *
   * @return int
   *   Constants indicating pass, fail and so forth.
   */
  public function getScore() {
    if (!isset($this->score)) {
      $this->score = $this
        ->calculateScore();
    }
    return $this->score;
  }

  /**
   * Get the check registry.
   *
   * @return array
   *   Contains values calculated from this check and any prior checks.
   */
  public function getRegistry() {
    return $this->registry;
  }

  /**
   * Determine whether the check failed so badly that the report must stop.
   *
   * @return bool
   *   Whether to stop the abort after this check.
   */
  public function shouldAbort() {
    return $this->abort;
  }

  /**
   * Get the report percent override, if any.
   *
   * @return int
   *   The overridden percentage.
   */
  public function getPercentOverride() {
    return $this->percentOverride;
  }

  /**
   * Invoke another check's calculateScore() method if it is needed.
   */
  protected function checkInvokeCalculateScore($plugin_id) {
    $checkManager = \Drupal::service('plugin.manager.site_audit_check');
    if (isset($opt_out)) {
      $check = $checkManager
        ->createInstance($plugin_id, [
        'registry' => $this->registry,
        'opt_out' => $opt_out,
      ]);
      $check
        ->calculateScore();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SiteAuditCheckBase::$abort protected property Names of checks that should not run as a result of this check.
SiteAuditCheckBase::$options protected property Options passed in for reports and checks.
SiteAuditCheckBase::$optOut protected property User has opted out of this check in configuration.
SiteAuditCheckBase::$percentOverride protected property If set, will override the Report's percentage.
SiteAuditCheckBase::$registry protected property Use for passing data between checks within a report.
SiteAuditCheckBase::$score protected property Quantifiable number associated with result on a scale of 0 to 2.
SiteAuditCheckBase::$static protected property Are we in a static context.
SiteAuditCheckBase::AUDIT_CHECK_SCORE_FAIL constant
SiteAuditCheckBase::AUDIT_CHECK_SCORE_INFO constant
SiteAuditCheckBase::AUDIT_CHECK_SCORE_PASS constant
SiteAuditCheckBase::AUDIT_CHECK_SCORE_WARN constant
SiteAuditCheckBase::calculateScore abstract public function Calculate the score. 54
SiteAuditCheckBase::checkInvokeCalculateScore protected function Invoke another check's calculateScore() method if it is needed.
SiteAuditCheckBase::getAction abstract public function Get action items for a user to perform if the check did not pass. 54
SiteAuditCheckBase::getDescription public function Get a more verbose description of what is being checked.
SiteAuditCheckBase::getId public function Get the ID or machine name for the check.
SiteAuditCheckBase::getLabel public function Get the label for the check that describes, high level what is happening.
SiteAuditCheckBase::getPercentOverride public function Get the report percent override, if any.
SiteAuditCheckBase::getRegistry public function Get the check registry.
SiteAuditCheckBase::getResult public function Determine the result message based on the score.
SiteAuditCheckBase::getResultFail abstract public function Get the description of what happened in a failed check. 54
SiteAuditCheckBase::getResultInfo abstract public function Get the result of a purely informational check. 54
SiteAuditCheckBase::getResultPass abstract public function Get a description of what happened in a passed check. 54
SiteAuditCheckBase::getResultWarn abstract public function Get a description of what happened in a warning check. 54
SiteAuditCheckBase::getScore public function Get a quantifiable number representing a check result; lazy initialization.
SiteAuditCheckBase::getScoreLabel public function Get a human readable label for a score.
SiteAuditCheckBase::renderAction public function Display action items for a user to perform.
SiteAuditCheckBase::shouldAbort public function Determine whether the check failed so badly that the report must stop.
SiteAuditCheckBase::__construct public function Constructor. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.