You are here

public function SiteAuditCheckInsightsAnalyze::calculateScore in Site Audit 7

Implements \SiteAudit\Check\Abstract\calculateScore().

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/Insights/Analyze.php, line 243
Contains \SiteAudit\Check\Insights\Analyze.

Class

SiteAuditCheckInsightsAnalyze
Class SiteAuditCheckInsightsAnalyze.

Code

public function calculateScore() {
  $pso_url = 'https://www.googleapis.com/pagespeedonline/v1/runPagespeed';
  $pso_url .= '?url=' . $this->registry['url'];
  $pso_url .= '&key=' . $this->registry['key'];
  $request = drupal_http_request($pso_url);
  $result = $request->data;
  $this->registry['json_result'] = json_decode($result);

  // Failure.
  if (isset($this->registry['json_result']->error)) {
    $this->abort = TRUE;
    $this->registry['errors'] = array();
    foreach ($this->registry['json_result']->error->errors as $error) {
      $this->registry['errors'][] = dt('@message (@domain - @reason)', array(
        '@message' => $error->message,
        '@domain' => $error->domain,
        '@reason' => $error->reason,
      ));
    }
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }

  // Access problem.
  if (isset($this->registry['json_result']->responseCode) && $this->registry['json_result']->responseCode != 200) {
    $this->abort = TRUE;
    $this->registry['errors'] = array();
    $this->registry['errors'][] = dt('@id is not accessible by PageSpeed Insights - response code (@responsecode)', array(
      '@id' => $this->registry['json_result']->id,
      '@responsecode' => $this->registry['json_result']->responseCode,
    ));
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }

  // Overview.
  if ($this->registry['json_result']->score > 80) {
    $this->percentOverride = $this->registry['json_result']->score;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
  }
  elseif ($this->registry['json_result']->score > 60) {
    $this->percentOverride = $this->registry['json_result']->score;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
  }
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}