You are here

public function SiteAuditCheckFrontEndWebPageTest::calculateScore in Site Audit 8.2

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

Overrides SiteAuditCheckAbstract::calculateScore

File

Check/FrontEnd/WebPageTest.php, line 139
Contains \SiteAudit\Check\FrontEnd\WebPageTest.

Class

SiteAuditCheckFrontEndWebPageTest
Class SiteAuditCheckFrontEndWebPageTest.

Code

public function calculateScore() {
  $this->registry['errors'] = array();
  $key = drush_get_option('wpt-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;
  }

  // Start the test.
  $wpt_url = 'http://www.webpagetest.org/runtest.php';
  $wpt_url .= '?url=' . $url;
  $wpt_url .= '&k=' . $key;
  $wpt_url .= '&f=json';
  $ch = curl_init($wpt_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  $result = curl_exec($ch);
  $result_json = json_decode($result);

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

  // Failure.
  if ($result_json->statusCode != 200) {
    $this->abort = TRUE;
    $this->registry['errors'] = array(
      $result_json->statusText,
    );
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }
  $result_url = $result_json->data->jsonUrl;

  // Keep checking for the results in every 5 seconds.
  $tries = 0;
  $found = FALSE;
  while ($tries < 20) {
    sleep(5);
    $tries++;
    $ch = curl_init($result_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    $result_json = json_decode($result);

    // Network connection or any other problem.
    if (is_null($result_json) || $result_json->statusCode != 200) {
      continue;
    }
    if ($result_json->statusCode == 200) {
      $found = TRUE;
      break;
    }
  }
  if (!$found) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
  }
  $scores = $result_json->data->average->firstView;
  $score = ($scores->{'score_keep-alive'} + $scores->score_gzip + $scores->score_cache + $scores->score_cdn) / 4;
  $this->registry['webpagetest']['data'] = $result_json;
  $this->registry['webpagetest']['images'] = $result_json->data->runs->{'1'}->firstView->images;
  $this->registry['webpagetest']['pages'] = $result_json->data->runs->{'1'}->firstView->pages;
  $this->registry['webpagetest']['scores'] = $scores;
  if ($score > 80) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_PASS;
  }
  elseif ($score > 60) {
    return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_WARN;
  }
  return SiteAuditCheckAbstract::AUDIT_CHECK_SCORE_FAIL;
}