AcquiaContentHubSettingsCommands.php in Acquia Content Hub 8.2
File
src/Commands/AcquiaContentHubSettingsCommands.php
View source
<?php
namespace Drupal\acquia_contenthub\Commands;
use Acquia\ContentHubClient\Settings;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub\Event\AcquiaContentHubSettingsEvent;
use Drupal\acquia_contenthub\EventSubscriber\GetSettings\GetSettingsFromCoreConfig;
use Drupal\acquia_contenthub\EventSubscriber\GetSettings\GetSettingsFromCoreSettings;
use Drupal\acquia_contenthub\EventSubscriber\GetSettings\GetSettingsFromEnvVar;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Helper\Table;
class AcquiaContentHubSettingsCommands extends DrushCommands {
protected $clientFactory;
protected $configFactory;
protected $loggerChannel;
protected $messenger;
public function __construct(ClientFactory $client_factory, LoggerChannelInterface $logger_channel, MessengerInterface $messenger, ConfigFactoryInterface $configFactory) {
$this->clientFactory = $client_factory;
$this->configFactory = $configFactory;
$this->loggerChannel = $logger_channel;
$this->messenger = $messenger;
}
public function getContentHubSettings() : void {
$this->output
->writeln('Content Hub Credentials:');
$in_use_provider = $this->clientFactory
->getProvider();
$config_settings = $this
->getSettingsFromCoreConfig();
$core_settings = $this
->getSettingsFromCoreSettings();
$env_var_settings = $this
->getSettingsFromEnvVar();
$content = [];
foreach ([
$config_settings,
$core_settings,
$env_var_settings,
] as $setting_obj) {
$settings = $setting_obj['settings'];
$provider = $setting_obj['provider'];
if ($settings instanceof Settings && !empty($provider)) {
$content[] = [
$settings
->getUrl() ?? '',
$settings
->getName() ?? '',
$settings
->getApiKey() ?? '',
$settings
->getSecretKey() ?? '',
$settings
->getUuid() ?? '',
$settings
->getSharedSecret() ?? '',
$provider ?? '',
$in_use_provider === $provider ? 'Yes' : 'No',
];
}
}
(new Table($this->output))
->setHeaders([
'Hostname',
'Name',
'API Key',
'Secret Key',
'Origin UUID',
'Shared Secret',
'Settings Provider',
'In Use',
])
->setRows($content)
->render();
}
protected function getSettingsFromCoreConfig() : array {
$event = new AcquiaContentHubSettingsEvent();
$config_setter = new GetSettingsFromCoreConfig($this->configFactory);
$config_setter
->onGetSettings($event);
return $this
->returnSettings($event);
}
protected function getSettingsFromCoreSettings() : array {
$event = new AcquiaContentHubSettingsEvent();
$core_settings_setter = new GetSettingsFromCoreSettings();
$core_settings_setter
->onGetSettings($event);
return $this
->returnSettings($event);
}
protected function getSettingsFromEnvVar() : array {
$event = new AcquiaContentHubSettingsEvent();
$env_var_setter = new GetSettingsFromEnvVar($this->loggerChannel, $this->messenger);
$env_var_setter
->onGetSettings($event);
return $this
->returnSettings($event);
}
protected function returnSettings(AcquiaContentHubSettingsEvent $event) {
return [
'settings' => $event
->getSettings() ?? NULL,
'provider' => $event
->getProvider() ?? '',
];
}
}