You are here

public function SiteAuditReportAbstract::__construct in Site Audit 8.2

Same name and namespace in other branches
  1. 7 Report/Abstract.php \SiteAuditReportAbstract::__construct()

Constructor; loads and executes checks based on the name of this report.

File

Report/Abstract.php, line 66
Contains \SiteAudit\Report\Abstract.

Class

SiteAuditReportAbstract
Class SiteAuditReportAbstract.

Code

public function __construct() {
  $base_class_name = 'SiteAuditCheck' . $this
    ->getReportName();
  $percent_override = NULL;
  $checks_to_skip = array();
  if (drush_get_option('skip')) {
    $checks_to_skip = explode(',', drush_get_option('skip'));
  }
  $checks_to_perform = $this
    ->getCheckNames();
  foreach ($checks_to_perform as $key => $check_name) {
    if (in_array($this
      ->getReportName() . $check_name, $checks_to_skip)) {
      unset($checks_to_perform[$key]);
    }
  }
  if (empty($checks_to_perform)) {

    // No message for audit_all.
    $command = drush_parse_command();
    if ($command['command'] == 'audit_all') {
      return FALSE;
    }
    return drush_set_error('SITE_AUDIT_NO_CHECKS', dt('No checks are available!'));
  }
  $config = \Drupal::config('site_audit');
  foreach ($checks_to_perform as $check_name) {
    $class_name = $base_class_name . $check_name;
    $opt_out = $config
      ->get('opt_out.' . $this
      ->getReportName() . $check_name) != NULL;
    $check = new $class_name($this->registry, $opt_out);

    // Calculate score.
    if ($check
      ->getScore() != SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO) {

      // Mark if there's a major failure.
      if ($check
        ->getScore() == SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL) {
        $this->hasFail = TRUE;
      }

      // Total.
      $this->scoreTotal += $check
        ->getScore();

      // Maximum.
      $this->scoreMax += SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
    }

    // Allow Report percentage to be overridden.
    if ($check
      ->getPercentOverride()) {
      $percent_override = $check
        ->getPercentOverride();
    }

    // Combine registry.
    $this->registry = array_merge($this->registry, $check
      ->getRegistry());

    // Store all checks.
    $this->checks[$class_name] = $check;

    // Abort the loop if the check says to bail.
    if ($check
      ->shouldAbort()) {
      break;
    }
  }
  if ($percent_override) {
    $this->percent = $percent_override;
  }
  else {
    if ($this->scoreMax != 0) {
      $this->percent = round($this->scoreTotal / $this->scoreMax * 100);
    }
    else {
      $this->percent = SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_INFO;
    }
  }
}