You are here

private function SpiController::handleServerResponse in Acquia Connector 8.2

Same name and namespace in other branches
  1. 8 src/Controller/SpiController.php \Drupal\acquia_connector\Controller\SpiController::handleServerResponse()
  2. 3.x src/Controller/SpiController.php \Drupal\acquia_connector\Controller\SpiController::handleServerResponse()

Act on specific elements of SPI update server response.

Parameters

array $spi_response: Array response from SpiController->send().

1 call to SpiController::handleServerResponse()
SpiController::sendFullSpi in src/Controller/SpiController.php
Gather full SPI data and send to Acquia.

File

src/Controller/SpiController.php, line 1109

Class

SpiController
SPI Controller class.

Namespace

Drupal\acquia_connector\Controller

Code

private function handleServerResponse(array $spi_response) {
  $config_set = $this->configFactory
    ->getEditable('acquia_connector.settings');
  $changed_action = $this
    ->config('acquia_connector.settings')
    ->get('spi.environment_changed_action');
  $config_set
    ->clear('spi.environment_changed_action')
    ->save();
  $site_uuid = $this
    ->config('acquia_connector.settings')
    ->get('spi.site_uuid');

  // Set site_uuid if it changed or if it hasn't been previously captured.
  if (isset($spi_response['body']['site_uuid']) && (is_null($site_uuid) || $spi_response['body']['site_uuid'] != $site_uuid)) {
    $config_set
      ->set('spi.site_uuid', $spi_response['body']['site_uuid'])
      ->save();
  }

  // Wipe the site_uuid if it is set locally, but NSPI is trying to create a
  // new site.
  if (isset($spi_response['body']['site_uuid']) && empty($spi_response['body']['site_uuid']) && !is_null($site_uuid)) {
    $config_set
      ->clear('spi.site_uuid')
      ->save();
  }
  $spi_environment_changes = isset($spi_response['body']['spi_environment_changes']) ? Json::decode($spi_response['body']['spi_environment_changes']) : [];
  $site_blocked = array_key_exists('blocked', $spi_environment_changes) || !empty($spi_response['site_revoked']);

  // Address any actions taken based on a site environment change.
  if (!empty($changed_action) || $site_blocked) {
    if ($changed_action == 'create' && isset($spi_response['body']['site_uuid'])) {
      $config_set
        ->set('spi.site_uuid', $spi_response['body']['site_uuid'])
        ->save();
    }
    elseif ($changed_action == 'block' && array_key_exists('spi_error', $spi_response['body']) && empty($spi_response['body']['spi_error']) || $site_blocked) {
      $config_set
        ->set('spi.blocked', TRUE)
        ->save();
    }
    elseif ($changed_action == 'unblock' && array_key_exists('spi_error', $spi_response['body']) && empty($spi_response['body']['spi_error'])) {
      $config_set
        ->set('spi.blocked', FALSE)
        ->save();
    }

    // If there were no errors, clear any pending actions.
    if (empty($spi_response['body']['spi_error'])) {
      $config_set
        ->clear('spi.environment_changes')
        ->save();
    }
  }

  // Check result for command to update SPI definition.
  $update = isset($spi_response['body']['update_spi_definition']) ? $spi_response['body']['update_spi_definition'] : FALSE;
  if ($update === TRUE) {
    $this
      ->updateDefinition();
  }

  // Check for set_variables command.
  $set_variables = isset($spi_response['body']['set_variables']) ? $spi_response['body']['set_variables'] : FALSE;
  if ($set_variables !== FALSE) {
    $variablesController = new VariablesController();
    $variablesController
      ->setVariables($set_variables);
  }

  // Log messages.
  $messages = isset($spi_response['body']['nspi_messages']) ? $spi_response['body']['nspi_messages'] : FALSE;
  if ($messages !== FALSE) {
    $this
      ->getLogger('acquia spi')
      ->notice('SPI update server response messages: @messages', [
      '@messages' => implode(', ', $messages),
    ]);
  }
}