You are here

public function AcquiaLiftAgent::errors in Acquia Lift Connector 7

Implements PersonalizeAgentInterface::errors().

File

plugins/agent_types/AcquiaLiftAgent.inc, line 641
Provides an agent type for Acquia Lift

Class

AcquiaLiftAgent

Code

public function errors() {
  $errors = array();
  try {
    $acquia_lift_agent = $this->liftAPI
      ->getAgent($this->machineName);
  } catch (AcquiaLiftException $e) {
    return $this
      ->convertAgentExceptionToErrors($e, $errors);
  }
  if ($acquia_lift_agent['status'] === AcquiaLiftAPI::PROVISIONAL_STATUS) {
    $errors[] = t('The status of the Acquia Lift agent is @status', array(
      '@status' => $acquia_lift_agent['status'],
    ));
  }

  // Make sure Acquia Lift knows about the agent's goals.
  $goals = personalize_goal_load_by_conditions(array(
    'agent' => $this->machineName,
  ));
  $discrepancies = FALSE;
  if (empty($goals)) {

    // Acquia Lift agents need goals.
    $errors[] = t('No goals have been set up for this agent');
  }
  try {
    $acquia_lift_goals = $this->liftAPI
      ->getGoalsForAgent($this->machineName);
  } catch (AcquiaLiftException $e) {
    return $this
      ->convertAgentExceptionToErrors($e, $errors);
  }
  foreach ($goals as $goal) {
    if (!in_array($goal->action, $acquia_lift_goals)) {
      $errors[] = t('Goal @goal has not been sync\'d to the Acquia Lift agent.', array(
        '@goal' => $goal->action,
      ));
      $discrepancies = TRUE;
    }
  }

  // Make sure all decision points are known by Acquia Lift.
  $option_sets = personalize_option_set_load_by_agent($this->machineName);
  if (empty($option_sets)) {

    // Acquia Lift agents need option sets.
    $errors[] = t('No variation sets have been set up for this agent');
  }
  $decision_tree = self::convertOptionSetsToDecisions($option_sets);
  try {
    $acquia_lift_points = $this->liftAPI
      ->getPointsForAgent($this->machineName);
  } catch (AcquiaLiftException $e) {
    return $this
      ->convertAgentExceptionToErrors($e, $errors);
  }
  foreach ($decision_tree as $point => $decisions) {
    if (!in_array($point, $acquia_lift_points)) {
      $errors[] = t('Point @point has not been sync\'d to the Acquia Lift agent.', array(
        '@point' => $point,
      ));
      $discrepancies = TRUE;
      continue;
    }
    try {
      $acquia_lift_decisions = $this->liftAPI
        ->getDecisionsForPoint($this->machineName, $point);
    } catch (AcquiaLiftException $e) {
      return $this
        ->convertAgentExceptionToErrors($e, $errors);
    }
    foreach ($decisions as $decision_name => $options) {
      if (!in_array($decision_name, $acquia_lift_decisions)) {
        $errors[] = t('Decision @decision has not been sync\'d to the Acquia Lift agent.', array(
          '@decision' => $decision_name,
        ));
        $discrepancies = TRUE;
      }
      try {
        $acquia_lift_choices = $this->liftAPI
          ->getChoicesForDecision($this->machineName, $point, $decision_name);
      } catch (AcquiaLiftException $e) {
        return $this
          ->convertAgentExceptionToErrors($e, $errors);
      }
      foreach ($options as $option) {
        if (!in_array($option, $acquia_lift_choices)) {
          $errors[] = t('Option @choice has not been sync\'d to the Acquia Lift agent.', array(
            '@choice' => $option,
          ));
          $discrepancies = TRUE;
        }
      }
    }

    // @todo Check the fixed targeting for each decision point.
  }
  if ($discrepancies) {

    // Add a general message about how to resolve discrepancies.
    $message = t('To resolve the discrepancies between your agent configuration here and what has been sync\'d to the Acquia Lift service, try saving your campaign and its variation sets and goals again.');
    if (user_access('administer site configuration')) {
      $message .= t(' If that still does not resolve it, try <a href="@cron">running cron</a>.', array(
        '@cron' => url('admin/reports/status/run-cron'),
      ));
    }
    $errors[] = $message;
  }
  return $errors;
}