You are here

public function Check::lastResult in Security Review 8

Returns the last stored result of the check.

Returns null if no results have been stored yet.

Parameters

bool $get_findings: Whether to get the findings too.

Return value

\Drupal\security_review\CheckResult|null The last stored result (or null).

2 calls to Check::lastResult()
FailedLogins::run in src/Checks/FailedLogins.php
The actual procedure of carrying out the check.
QueryErrors::run in src/Checks/QueryErrors.php
The actual procedure of carrying out the check.

File

src/Check.php, line 242

Class

Check
Defines a security check.

Namespace

Drupal\security_review

Code

public function lastResult($get_findings = FALSE) {

  // Get stored data from State system.
  $state_prefix = $this->statePrefix . 'last_result.';
  $result = $this->state
    ->get($state_prefix . 'result');
  if ($get_findings) {
    $findings = $this->state
      ->get($state_prefix . 'findings');
  }
  else {
    $findings = [];
  }
  $time = $this->state
    ->get($state_prefix . 'time');

  // Force boolean value.
  $visible = $this->state
    ->get($state_prefix . 'visible') == TRUE;

  // Check validity of stored data.
  $valid_result = is_int($result) && $result >= CheckResult::SUCCESS && $result <= CheckResult::INFO;
  $valid_findings = is_array($findings);
  $valid_time = is_int($time) && $time > 0;

  // If invalid, return NULL.
  if (!$valid_result || !$valid_findings || !$valid_time) {
    return NULL;
  }

  // Construct the CheckResult.
  $last_result = new CheckResult($this, $result, $findings, $visible, $time);

  // Do a check run for acquiring findings if required.
  if ($get_findings && !$this
    ->storesFindings()) {

    // Run the check to get the findings.
    $fresh_result = $this
      ->run();

    // If it malfunctioned return the last known good result.
    if (!$fresh_result instanceof CheckResult) {
      return $last_result;
    }
    if ($fresh_result
      ->result() != $last_result
      ->result()) {

      // If the result is not the same store the new result and return it.
      $this
        ->storeResult($fresh_result);
      $this
        ->securityReview()
        ->logCheckResult($fresh_result);
      return $fresh_result;
    }
    else {

      // Else return the old result with the fresh one's findings.
      return CheckResult::combine($last_result, $fresh_result);
    }
  }
  return $last_result;
}