You are here

class GetSettingsFromEnvVar in Acquia Content Hub 8.2

Gets the ContentHub Server settings from environment variable.

Hierarchy

Expanded class hierarchy of GetSettingsFromEnvVar

1 file declares its use of GetSettingsFromEnvVar
AcquiaContentHubSettingsCommands.php in src/Commands/AcquiaContentHubSettingsCommands.php
1 string reference to 'GetSettingsFromEnvVar'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses GetSettingsFromEnvVar
acquia_contenthub.settings.envvar in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\GetSettings\GetSettingsFromEnvVar

File

src/EventSubscriber/GetSettings/GetSettingsFromEnvVar.php, line 17

Namespace

Drupal\acquia_contenthub\EventSubscriber\GetSettings
View source
class GetSettingsFromEnvVar implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * Array containing the necessary environment variable keys.
   */
  const ENVIRONMENT_VARIABLES = [
    'acquia_contenthub_api_secret',
    'acquia_contenthub_api_key',
    'acquia_contenthub_hostname',
    'acquia_contenthub_client_name',
    'acquia_contenthub_origin',
    'acquia_contenthub_shared_secret',
    'acquia_contenthub_webhook_url',
    'acquia_contenthub_webhook_uuid',
    'acquia_contenthub_settings_url',
  ];

  /**
   * Acquia ContentHub logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Drupal messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * GetSettingsFromEnvVarTest constructor.
   *
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
   *   Acquia ContentHub logger channel.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   Drupal messenger interface.
   */
  public function __construct(LoggerChannelInterface $logger, MessengerInterface $messenger) {
    $this->logger = $logger;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::GET_SETTINGS][] = [
      'onGetSettings',
      100,
    ];
    return $events;
  }

  /**
   * Extract settings from environment and create a Settings object.
   *
   * @param \Drupal\acquia_contenthub\Event\AcquiaContentHubSettingsEvent $event
   *   The dispatched event.
   *
   * @see \Acquia\ContentHubClient\Settings
   */
  public function onGetSettings(AcquiaContentHubSettingsEvent $event) {
    $credentials = [];
    foreach (self::ENVIRONMENT_VARIABLES as $env_variable) {
      $credential = getenv($env_variable);
      if ($credential) {
        $credentials[$env_variable] = $credential;
      }
    }
    if (count($credentials) === 0) {

      // If there are no environment variables set, then we should keep going
      // and not log all the errors in isValidCredential(). We assume, the user
      // do not want to use this way of registration.
      return;
    }
    $success = $this
      ->isValidCredential($credentials);
    if (!$success) {
      return;
    }
    $settings = new Settings($credentials['acquia_contenthub_client_name'], $credentials['acquia_contenthub_origin'], $credentials['acquia_contenthub_api_key'], $credentials['acquia_contenthub_api_secret'], $credentials['acquia_contenthub_hostname'], $credentials['acquia_contenthub_shared_secret'], [
      'url' => $credentials['acquia_contenthub_webhook_url'],
      'uuid' => $credentials['acquia_contenthub_webhook_uuid'],
      'settings_url' => $credentials['acquia_contenthub_settings_url'],
    ]);
    $event
      ->setProvider('environment_variable');
    $event
      ->setSettings($settings);
    $event
      ->stopPropagation();
  }

  /**
   * Checks credentials are all set and valid.
   *
   * @param array $credentials
   *   Credentials for registering ACH.
   *
   * @return bool
   *   TRUE if there is no error at all.
   */
  protected function isValidCredential(array $credentials) : bool {
    $errors = [];
    if (count(self::ENVIRONMENT_VARIABLES) !== count($credentials)) {
      $errors[] = $this
        ->t('Some of the credentials missing from the environment variables.');
    }
    foreach (self::ENVIRONMENT_VARIABLES as $variable) {
      if (!isset($credentials[$variable])) {
        $errors[] = $this
          ->t('Credential missing from environment variables: @var', [
          '@var' => $variable,
        ]);
      }
    }
    foreach ([
      'acquia_contenthub_hostname',
      'acquia_contenthub_webhook_url',
    ] as $url) {
      if (isset($credentials[$url]) && !UrlHelper::isValid($credentials[$url], TRUE)) {
        $errors[] = $this
          ->t('@url is not a valid url. Please insert another one.', [
          '@url' => $url,
        ]);
      }
    }
    foreach ($errors as $error) {
      $this->messenger
        ->addWarning($this
        ->t('Environment variables set for registering ACH, but something went wrong. Error: @error', [
        '@error' => $error,
      ]));
      $this->logger
        ->error($this
        ->t('Environment variables set for registering ACH, but something went wrong. Error: @error', [
        '@error' => $error,
      ]));
    }
    return empty($errors);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GetSettingsFromEnvVar::$logger protected property Acquia ContentHub logger channel.
GetSettingsFromEnvVar::$messenger protected property Drupal messenger.
GetSettingsFromEnvVar::ENVIRONMENT_VARIABLES constant Array containing the necessary environment variable keys.
GetSettingsFromEnvVar::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
GetSettingsFromEnvVar::isValidCredential protected function Checks credentials are all set and valid.
GetSettingsFromEnvVar::onGetSettings public function Extract settings from environment and create a Settings object.
GetSettingsFromEnvVar::__construct public function GetSettingsFromEnvVarTest constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.