Ejemplo n.º 1
0
 /**
  * Returns the last stored result of the check or null if no results have been
  * stored yet.
  *
  * @return \Drupal\security_review\CheckResult|null
  *   The last stored result (or null).
  */
 public function lastResult()
 {
     $statePrefix = $this->statePrefix . 'last_result.';
     $result = Drupal::state()->get($statePrefix . 'result');
     $findings = Drupal::state()->get($statePrefix . 'findings');
     $time = Drupal::state()->get($statePrefix . 'time');
     $validResult = is_int($result) && $result >= CheckResult::SUCCESS && $result <= CheckResult::INFO;
     $validFindings = is_array($findings);
     $validTime = is_int($time) && $time > 0;
     if (!$validResult || !$validFindings || !$validTime) {
         return NULL;
     }
     $lastResult = new CheckResult($this, $result, $findings, $time);
     if ($this->storesFindings()) {
         return $lastResult;
     } else {
         // Run the check to get the findings.
         $freshResult = $this->run();
         // If it malfunctioned return the last known good result.
         if (!$freshResult instanceof CheckResult) {
             return $lastResult;
         }
         if ($freshResult->result() != $lastResult->result()) {
             // If the result is not the same store the new result and return it.
             $this->storeResult($freshResult);
             SecurityReview::logCheckResult($freshResult);
             return $freshResult;
         } else {
             // Else return the old result with the fresh one's findings.
             return CheckResult::combine($lastResult, $this->run());
         }
     }
 }