You are here

private function SpiController::updateDefinition in Acquia Connector 8

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

Checks if NSPI server has an updated SPI data definition.

If it does, then this function updates local copy of SPI definition data.

Return value

bool True if SPI definition data has been updated.

1 call to SpiController::updateDefinition()
SpiController::handleServerResponse in src/Controller/SpiController.php
Act on specific elements of SPI update server response.

File

src/Controller/SpiController.php, line 1174

Class

SpiController
SPI Controller class.

Namespace

Drupal\acquia_connector\Controller

Code

private function updateDefinition() {
  $core_version = substr(\Drupal::VERSION, 0, 1);
  $spi_def_end_point = '/spi_def/get/' . $core_version;
  $response = $this->client
    ->getDefinition($spi_def_end_point);
  if (!$response) {
    $this
      ->getLogger('acquia spi')
      ->error('Failed to obtain latest SPI data definition.');
    return FALSE;
  }
  else {
    $response_data = $response;
    $expected_data_types = [
      'drupal_version' => 'string',
      'timestamp' => 'string',
      'acquia_spi_variables' => 'array',
    ];

    // Make sure that $response_data contains everything expected.
    foreach ($expected_data_types as $key => $values) {
      if (!array_key_exists($key, $response_data) || gettype($response_data[$key]) != $expected_data_types[$key]) {
        $this
          ->getLogger('acquia spi')
          ->error('Received SPI data definition does not match expected pattern while checking "@key". Received and expected data: @data', [
          '@key' => $key,
          '@data' => var_export(array_merge([
            'expected_data' => $expected_data_types,
          ], [
            'response_data' => $response_data,
          ]), TRUE),
        ]);
        return FALSE;
      }
    }
    if ($response_data['drupal_version'] != $core_version) {
      $this
        ->getLogger('acquia spi')
        ->notice('Received SPI data definition does not match with current version of your Drupal installation. Data received for Drupal @version', [
        '@version' => $response_data['drupal_version'],
      ]);
      return FALSE;
    }
  }

  // NSPI response is in expected format.
  if ((int) $response_data['timestamp'] > (int) $this
    ->state()
    ->get('acquia_spi_data.def_timestamp', 0)) {

    // Compare stored variable names to incoming and report on update.
    $old_vars = $this
      ->state()
      ->get('acquia_spi_data.def_vars', []);
    $new_vars = $response_data['acquia_spi_variables'];
    $new_optional_vars = 0;
    foreach ($new_vars as $new_var_name => $new_var) {

      // Count if received from NSPI optional variable is not present in old
      // local SPI definition or if it already was in old SPI definition, but
      // was not optional.
      if ($new_var['optional'] && !array_key_exists($new_var_name, $old_vars) || $new_var['optional'] && isset($old_vars[$new_var_name]) && !$old_vars[$new_var_name]['optional']) {
        $new_optional_vars++;
      }
    }

    // Clean up waived vars that are not exposed by NSPI anymore.
    $waived_spi_def_vars = $this
      ->state()
      ->get('acquia_spi_data.def_waived_vars', []);
    $changed_bool = FALSE;
    foreach ($waived_spi_def_vars as $key => $waived_var) {
      if (!in_array($waived_var, $new_vars)) {
        unset($waived_spi_def_vars[$key]);
        $changed_bool = TRUE;
      }
    }
    if ($changed_bool) {
      $this
        ->state()
        ->set('acquia_spi_data.def_waived_vars', $waived_spi_def_vars);
    }

    // Finally, save SPI definition data.
    if ($new_optional_vars > 0) {
      $this
        ->state()
        ->set('acquia_spi_data.new_optional_data', 1);
    }
    $this
      ->state()
      ->set('acquia_spi_data.def_timestamp', (int) $response_data['timestamp']);
    $this
      ->state()
      ->set('acquia_spi_data.def_vars', $response_data['acquia_spi_variables']);
    return TRUE;
  }
  return FALSE;
}