You are here

class AcsfVariableStorage in Acquia Cloud Site Factory Connector 8

Same name and namespace in other branches
  1. 8.2 src/AcsfVariableStorage.php \Drupal\acsf\AcsfVariableStorage

Encapsulates functionality to interact with ACSF variables.

Hierarchy

Expanded class hierarchy of AcsfVariableStorage

1 string reference to 'AcsfVariableStorage'
acsf.services.yml in ./acsf.services.yml
acsf.services.yml
1 service uses AcsfVariableStorage
acsf.variable_storage in ./acsf.services.yml
Drupal\acsf\AcsfVariableStorage

File

src/AcsfVariableStorage.php, line 10

Namespace

Drupal\acsf
View source
class AcsfVariableStorage {

  /**
   * The current database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The active database connection.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * Sets a named variable with an optional group.
   *
   * @param string $name
   *   The name of the variable.
   * @param mixed $value
   *   The value of the variable.
   * @param string $group
   *   The group name of the variable. Optional.
   *
   * @return int
   *   1 if an INSERT query was executed, 2 for UPDATE.
   */
  public function set($name, $value, $group = NULL) {
    return $this->connection
      ->merge('acsf_variables')
      ->keys([
      'name' => $name,
    ])
      ->fields([
      'group_name' => $group,
      'value' => serialize($value),
    ])
      ->execute();
  }

  /**
   * Retrieves a named variable.
   *
   * @param string $name
   *   The name of the variable.
   * @param mixed $default
   *   The default value of the variable.
   *
   * @return mixed
   *   The value of the variable.
   */
  public function get($name, $default = NULL) {
    $record = $this->connection
      ->select('acsf_variables', 'v')
      ->fields('v', [
      'value',
    ])
      ->condition('name', $name, '=')
      ->execute()
      ->fetchAssoc();
    if ($record) {
      return unserialize($record['value']);
    }
    else {
      return $default;
    }
  }

  /**
   * Retrieves variables whose names match a substring.
   *
   * @param string $match
   *   A substring that must occur in the variable name.
   *
   * @return array
   *   An associative array holding the values of the variables, keyed by the
   *   variable names.
   */
  public function getMatch($match) {
    $return = [];
    $result = $this->connection
      ->select('acsf_variables', 'v')
      ->fields('v', [
      'name',
      'value',
    ])
      ->condition('name', '%' . $match . '%', 'LIKE')
      ->execute();
    while ($record = $result
      ->fetchAssoc()) {
      $return[$record['name']] = unserialize($record['value']);
    }
    return $return;
  }

  /**
   * Retrieves a group of variables.
   *
   * @param string $group
   *   The group name of the variables.
   * @param mixed $default
   *   The default value of the group.
   *
   * @return array
   *   An associative array holding the values of the group of variables, keyed
   *   by the variable names.
   */
  public function getGroup($group, $default = []) {
    $return = [];
    $result = $this->connection
      ->select('acsf_variables', 'v')
      ->fields('v', [
      'name',
      'value',
    ])
      ->condition('group_name', $group, '=')
      ->execute();
    while ($record = $result
      ->fetchAssoc()) {
      $return[$record['name']] = unserialize($record['value']);
    }
    if (empty($return)) {
      return $default;
    }
    else {
      return $return;
    }
  }

  /**
   * Deletes a named variable.
   *
   * @param string $name
   *   The name of the variable.
   *
   * @return int
   *   The number of deleted rows.
   */
  public function delete($name) {
    $result = $this->connection
      ->delete('acsf_variables')
      ->condition('name', $name)
      ->execute();
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcsfVariableStorage::$connection protected property The current database connection.
AcsfVariableStorage::delete public function Deletes a named variable.
AcsfVariableStorage::get public function Retrieves a named variable.
AcsfVariableStorage::getGroup public function Retrieves a group of variables.
AcsfVariableStorage::getMatch public function Retrieves variables whose names match a substring.
AcsfVariableStorage::set public function Sets a named variable with an optional group.
AcsfVariableStorage::__construct public function Constructor.