You are here

public function SiteAuditCheckFrontEndGooglePageSpeed::calculateScore in Site Audit 8.2

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

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/FrontEnd/GooglePageSpeed.php, line 287
Contains \SiteAudit\Check\Insights\Analyze.

Class

SiteAuditCheckFrontEndGooglePageSpeed
Class SiteAuditCheckFrontEndGooglePageSpeed.

Code

public function calculateScore() {
  $this->registry['errors'] = array();
  $key = drush_get_option('gi-key');
  $url = drush_get_option('url');
  if ($key == NULL) {
    $this->registry['errors'][] = dt('No API key provided.');
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }
  if ($url == NULL) {
    $this->registry['errors'][] = dt('No url provided.');
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }
  $pso_url = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed';
  $pso_url .= '?url=' . $url;
  $pso_url .= '&key=' . $key;
  $ch = curl_init($pso_url . '&strategy=desktop');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $result = curl_exec($ch);
  $this->registry['json_desktop_result'] = json_decode($result);
  $ch = curl_init($pso_url . '&strategy=mobile');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $result = curl_exec($ch);
  $this->registry['json_mobile_result'] = json_decode($result);

  // Network connection or any other problem.
  if (is_null($this->registry['json_desktop_result']) || is_null($this->registry['json_mobile_result'])) {
    $this->abort = TRUE;
    $this->registry['errors'][] = dt('www.googleapis.com did not provide valid json; raw result: @message', array(
      '@message' => $result,
    ));
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }

  // Failure.
  if (isset($this->registry['json_desktop_result']->error)) {
    $this->abort = TRUE;
    $this->registry['errors'] = array();
    foreach ($this->registry['json_desktop_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;
  }
  if (isset($this->registry['json_mobile_result']->error)) {
    $this->abort = TRUE;
    $this->registry['errors'] = array();
    foreach ($this->registry['json_mobile_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_desktop_result']->responseCode) && $this->registry['json_desktop_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_desktop_result']->id,
      '@responsecode' => $this->registry['json_desktop_result']->responseCode,
    ));
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }
  if (isset($this->registry['json_mobile_result']->responseCode) && $this->registry['json_desktop_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_desktop_result']->id,
      '@responsecode' => $this->registry['json_desktop_result']->responseCode,
    ));
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }

  // Overview.
  // Average all scores to get the final score.
  $score = $count = 0;
  foreach ($this->registry['json_mobile_result']->ruleGroups as $group) {
    $score += $group->score;
    $count++;
  }
  foreach ($this->registry['json_desktop_result']->ruleGroups as $group) {
    $score += $group->score;
    $count++;
  }
  $score = intval($score / $count);
  if ($score > 80) {
    $this->percentOverride = $score;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
  }
  elseif ($score > 60) {
    $this->percentOverride = $score;
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
  }
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}