You are here

protected function DummyAcquiaLiftHttpClient::getDataForURI in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 tests/acquia_lift.test_classes.inc \DummyAcquiaLiftHttpClient::getDataForURI()

Returns the expected data array for a given uri.

Parameters

string $uri: An absolute url for an API endpoint, e.g. http://example.com/owner-code/list-agents

Return value

array An array of data to be returned in the response.

1 call to DummyAcquiaLiftHttpClient::getDataForURI()
DummyAcquiaLiftHttpClient::get in tests/acquia_lift.test_classes.inc
Implements AcquiaLiftDrupalHttpClientInterface::get().

File

tests/acquia_lift.test_classes.inc, line 127
Provides test classes for Acquia Lift

Class

DummyAcquiaLiftHttpClient
Classes used for testing.

Code

protected function getDataForURI($uri, $headers = array()) {
  $parsed = parse_url($uri);
  $path_parts = explode('/', $parsed['path']);

  // The first element of the $path_parts array will be an empty string and the second
  // will be the owner code, which we don't need here.
  $path_parts = array_slice($path_parts, 2);
  switch ($path_parts[0]) {
    case 'list-agents':
      return array(
        'data' => array(
          'agents' => isset($this->data['agents']) ? $this->data['agents'] : array(),
        ),
      );
    case 'transforms-options':
      return array(
        'data' => array(
          'options' => isset($this->data['options']) ? $this->data['options'] : array(),
        ),
      );
    case '-':

      // For some reason the endpoint for getting targeting values is '/-/potential-targeting'
      if ($path_parts[1] != 'potential-targeting') {
        return array();
      }
      if (isset($this->data['features'])) {
        return array(
          'data' => array(
            'potential' => array(
              'features' => $this->data['features'],
            ),
          ),
        );
      }
      if (isset($this->data['reports'])) {

        // Grab the agent name from the querystring.
        $query_params = explode('&', $parsed['query']);
        $first_param = explode('=', reset($query_params));
        if ($first_param[0] !== 'agent') {
          return array();
        }
        $agent_name = $first_param[1];
        if (isset($this->data['reports'][$agent_name]['context-filters'])) {
          return $this->data['reports'][$agent_name]['context-filters'];
        }
      }
      return array(
        'data' => array(
          'potential' => array(
            'features' => array(),
          ),
        ),
      );
    case 'agent-api':
      $agent_name = $path_parts[1];
      if (count($path_parts) == 2) {
        return array(
          'machine-name' => $agent_name,
          'status' => PERSONALIZE_STATUS_NOT_STARTED,
        );
      }
      switch ($path_parts[2]) {
        case 'points':

          // This request must be for the list of points for an agent, or the list of decisions
          // for a point, or the list of choices for a decision. Use a regex to figure out which.
          $matches = array();
          $pattern = '/agent\\-api\\/[a-zA-Z0-9-_]+(\\/points\\/[a-zA-Z0-9-_]+\\/decisions(\\/[a-zA-Z0-9-_]+\\/choices)?)?/';
          if (!preg_match($pattern, $parsed['path'], $matches)) {
            return array();
          }
          switch (count($matches)) {
            case 1:

              // This is a request for points for an agent.
              return isset($this->data['points'][$agent_name]) ? $this->data['points'][$agent_name] : array();
            case 2:

              // This is a request for decisions for a point.
              $point_name = $path_parts[3];
              return isset($this->data['decisions'][$agent_name][$point_name]) ? $this->data['decisions'][$agent_name][$point_name] : array();
            case 3:

              // This is a request for choices for a decision.
              $point_name = $path_parts[3];
              $decision_name = $path_parts[5];
              return isset($this->data['choices'][$agent_name][$point_name][$decision_name]) ? $this->data['choices'][$agent_name][$point_name][$decision_name] : array();
          }
          return array();
        case 'goals':
          return isset($this->data['goals'][$agent_name]) ? $this->data['goals'][$agent_name] : array();
      }
      break;
  }

  // Reports (unfortunately not all of which have the same url pattern)
  if (strpos($parsed['path'], 'report/status') !== FALSE) {
    $query_params = explode('&', $parsed['query']);
    $first = explode('=', reset($query_params));
    if ($first[0] !== 'codes') {
      return array();
    }
    $agent_names = explode(',', $first[1]);
    $agent_name = reset($agent_names);
    return isset($this->data['reports'][$agent_name]['agent-status']) ? $this->data['reports'][$agent_name]['agent-status'] : array();
  }
  if (isset($path_parts[1]) && $path_parts[1] == 'report') {
    $matches = array();
    $pattern = '/([a-zA-Z0-9-_]+)\\/report\\/(confidence|targeting\\-features|learning)/';
    if (!preg_match($pattern, $parsed['path'], $matches)) {
      return array();
    }
    $agent_name = $matches[1];
    $report_type = $matches[2];
    if ($report_type == 'confidence' && isset($headers['x-mpath-point'])) {

      // We're getting a confidence report for a particular decision point.
      if (isset($this->data['reports'][$agent_name]['confidence'])) {
        $confidence_report = $this->data['reports'][$agent_name]['confidence'];
        foreach ($confidence_report['data']['items'] as $i => $item) {
          if ($item['point'] !== $headers['x-mpath-point']) {
            unset($confidence_report['data']['items'][$i]);
          }
        }
        $confidence_report['data']['items'] = array_values($confidence_report['data']['items']);
        return $confidence_report;
      }
    }
    return isset($this->data['reports'][$agent_name][$report_type]) ? $this->data['reports'][$agent_name][$report_type] : array();
  }
  return array();
}