You are here

function acquia_lift_get_sync_operations_for_agent in Acquia Lift Connector 7.2

Returns all operations required to sync the testing components of an agent.

Parameters

$agent: The agent to sync testing components for.

Return value

array An array of operations representing DELETE and PUT requests to be made to Lift.

Throws

\AcquiaLiftCredsException

\AcquiaLiftException

5 calls to acquia_lift_get_sync_operations_for_agent()
AcquiaLiftWebTestFundamentals::testBatchSyncOperations in tests/acquia_lift.test
Tests the function that gathers operations for syncing of agents and their components to Lift.
AcquiaLiftWebTestFundamentals::testSiteNamePrefixAttach in tests/acquia_lift.test
acquia_lift_batch_sync_all in ./acquia_lift.batch.inc
Batch syncs the nested tests for all agents.
acquia_lift_batch_sync_tests_for_agent in ./acquia_lift.batch.inc
Batch syncs the nested tests for the specified agent.
drush_acquia_lift_campaign_sync in ./acquia_lift.drush.inc
Syncs the Acquia Lift Campaigns.

File

./acquia_lift.batch.inc, line 218
acquia_lift.batch.inc

Code

function acquia_lift_get_sync_operations_for_agent($agent) {
  $operations = array();

  // First see if there are any tests that need to be deleted from Lift.
  if (isset($agent->tests_to_delete)) {
    foreach ($agent->tests_to_delete as $old_test_agent) {
      $operations[] = array(
        'method' => 'deleteAgent',
        'args' => array(
          $old_test_agent,
        ),
      );
    }
  }
  $nested = acquia_lift_get_nested_tests($agent);

  // If there are no nested tests then there is nothing further to sync.
  if (empty($nested)) {
    return $operations;
  }
  $nested_tests = personalize_agent_load_multiple($nested);
  $goals = personalize_goal_load_by_conditions(array(
    'agent' => $agent->machine_name,
  ));
  if (empty($goals)) {
    throw new AcquiaLiftException('No goals have been set up for personalization ' . $agent->machine_name);
  }
  $account_info = acquia_lift_get_account_info();
  $lift_api = AcquiaLiftAPI::getInstance($account_info);
  foreach ($nested_tests as $agent_name => $test) {
    if (!acquia_lift_is_testing_agent($test)) {
      continue;
    }
    if ($agent_instance = personalize_agent_load_agent($agent_name, TRUE)) {
      if (!$agent_instance instanceof AcquiaLiftLearningAgentInterface) {
        continue;
      }

      // If we're dealing with an agent that already exists in Lift
      // then we may need to delete some of its components.
      try {
        $existing_agent = $lift_api
          ->getAgent($agent_name);
      } catch (AcquiaLiftException $e) {
        if ($e instanceof AcquiaLiftNotFoundException) {
          $existing_agent = FALSE;
        }
        else {

          // Any other exception means we can't communicate with Lift so throw
          // an exception to indicate batch sync cannot complete.
          throw new AcquiaLiftException('There is a problem communicating with Lift, syncing cannot complete at this time.');
        }
      }

      // Now go through all option sets and sync those.
      $option_sets = personalize_option_set_load_by_agent($agent_name, TRUE);
      if (empty($option_sets)) {
        throw new AcquiaLiftException('Missing variation set for test ' . $agent_name);
      }
      $first_os = reset($option_sets);
      if (count($first_os->options) < 2) {
        throw new AcquiaLiftException('Test ' . $agent_name . ' has fewer than two variations.');
      }
      $new_goals = $old_goals = array();
      foreach ($goals as $goal) {
        $new_goals[$goal->action] = $goal->value;
      }
      if ($existing_agent && ($existing_goals = $lift_api
        ->getGoalsForAgent($agent_name))) {

        // If it's an existing agent there may be goals that need
        // to be deleted.
        foreach ($existing_goals as $goal) {

          // The array of goals has goal names as keys and goal values
          // as values, but the values are not used so we can pass
          // any value for each old goal.
          $old_goals[$goal] = 1;
        }
      }

      // Get all the operations required to sync this agent and its components.
      $operations = array_merge($operations, $agent_instance
        ->getAgentSyncOperations($option_sets, $new_goals, $old_goals));
    }
  }
  return $operations;
}