You are here

public function VariablesController::setVariables in Acquia Connector 8

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

Set variables from NSPI response.

Parameters

array|bool $set_variables: Variables to be set.

File

src/Controller/VariablesController.php, line 205

Class

VariablesController
Class MappingController.

Namespace

Drupal\acquia_connector\Controller

Code

public function setVariables($set_variables) {
  $this
    ->getLogger('acquia spi')
    ->notice('SPI set variables: @messages', [
    '@messages' => implode(', ', $set_variables),
  ]);
  if (empty($set_variables)) {
    return;
  }
  $saved = [];
  $ignored = $this
    ->config('acquia_connector.settings')
    ->get('spi.ignored_set_variables');
  if (!$this
    ->config('acquia_connector.settings')
    ->get('spi.set_variables_override')) {
    $ignored[] = 'acquia_spi_set_variables_automatic';
  }

  // Some variables can never be set.
  $ignored = array_merge($ignored, [
    'drupal_private_key',
    'site_mail',
    'site_name',
    'maintenance_mode',
    'user_register',
  ]);

  // Variables that can be automatically set.
  $whitelist = $this
    ->config('acquia_connector.settings')
    ->get('spi.set_variables_automatic');
  foreach ($set_variables as $key => $value) {

    // Approved variables get set immediately unless ignored.
    if (in_array($key, $whitelist) && !in_array($key, $ignored)) {
      if (!empty($this->mapping[$key])) {

        // State.
        if ($this->mapping[$key][0] == 'state' and !empty($this->mapping[$key][1])) {
          \Drupal::state()
            ->set($this->mapping[$key][1], $value);
          $saved[] = $key;
        }
        elseif ($this->mapping[$key][0] == 'settings') {

          // No setter for Settings.
        }
        else {
          $mapping_row_copy = $this->mapping[$key];
          $config_name = array_shift($mapping_row_copy);
          $variable_name = implode('.', $mapping_row_copy);
          \Drupal::configFactory()
            ->getEditable($config_name)
            ->set($variable_name, $value);
          \Drupal::configFactory()
            ->getEditable($config_name)
            ->save();
          $saved[] = $key;
        }
      }
      elseif (preg_match('/^([^\\s]+):([^\\s]+)$/ui', $key, $regs)) {
        $config_name = $regs[1];
        $variable_name = $regs[2];
        \Drupal::configFactory()
          ->getEditable($config_name)
          ->set($variable_name, $value);
        \Drupal::configFactory()
          ->getEditable($config_name)
          ->save();
        $saved[] = $key;
      }
      else {
        $this
          ->getLogger('acquia spi')
          ->notice('Variable is not implemented: ' . $key);
      }
    }
  }
  if (!empty($saved)) {
    \Drupal::configFactory()
      ->getEditable('acquia_connector.settings')
      ->set('spi.saved_variables', [
      'variables' => $saved,
      'time' => time(),
    ]);
    \Drupal::configFactory()
      ->getEditable('acquia_connector.settings')
      ->save();
    $this
      ->getLogger('acquia spi')
      ->notice('Saved variables from the Acquia: @variables', [
      '@variables' => implode(', ', $saved),
    ]);
  }
  else {
    $this
      ->getLogger('acquia spi')
      ->notice('Did not save any variables from Acquia.');
  }
}