From afe1f1891050071ca8dcacd9d803a68433e25f6c Mon Sep 17 00:00:00 2001 From: Mikkel Jakobsen Date: Sun, 5 Jan 2025 12:27:57 +0100 Subject: [PATCH] Refactor consumer drush cmds Adjust cmds to fit new way of handling consumers and be more specific about what each command actually is doing. --- .../dpl_consumers/dpl_consumers.deploy.php | 2 +- .../custom/dpl_consumers/dpl_consumers.module | 10 +-- .../src/Commands/DplConsumersCommands.php | 64 ++++++++----------- 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/web/modules/custom/dpl_consumers/dpl_consumers.deploy.php b/web/modules/custom/dpl_consumers/dpl_consumers.deploy.php index 9942c3510..d88244628 100644 --- a/web/modules/custom/dpl_consumers/dpl_consumers.deploy.php +++ b/web/modules/custom/dpl_consumers/dpl_consumers.deploy.php @@ -44,7 +44,7 @@ function dpl_consumers_deploy_10002(): void { // Create new consumers (BNF and Go) and their users and roles. /** @var \Drupal\dpl_consumers\Services\ConsumerHandler $consumer_handler */ $consumer_handler = \Drupal::service('dpl_consumers.consumer_handler'); - foreach (dpl_consumers_known_consumers_users_and_roles() as $consumer) { + foreach (dpl_consumers_known_consumers_settings() as $consumer) { $consumer_handler->setComponents($consumer['consumer'], $consumer['user'], $consumer['role'])->create(); } } diff --git a/web/modules/custom/dpl_consumers/dpl_consumers.module b/web/modules/custom/dpl_consumers/dpl_consumers.module index 1483b0d2c..5456178a5 100644 --- a/web/modules/custom/dpl_consumers/dpl_consumers.module +++ b/web/modules/custom/dpl_consumers/dpl_consumers.module @@ -52,7 +52,7 @@ function dpl_consumers_get_consumer_uuid(string $client_id): string { * @return mixed[] * An array of consumers, users and roles. */ -function dpl_consumers_known_consumers_users_and_roles(): array { +function dpl_consumers_known_consumers_settings(): array { // Check if we have the necessary secrets for new consumers and their users. if (!$bnf_consumer_secret = getenv('BNF_GRAPHQL_CONSUMER_SECRET')) { throw new \Exception('BNF_GRAPHQL_CONSUMER_SECRET not found.'); @@ -89,10 +89,10 @@ function dpl_consumers_known_consumers_users_and_roles(): array { */ function dpl_consumers_get_known_consumers(): array { return array_reduce( - dpl_consumers_known_consumers_users_and_roles(), - static function (array $carry, array $consumer): array { - $consumer_client_id = $consumer['consumer']->client_id; - $carry[$consumer_client_id] = $consumer['consumer']; + dpl_consumers_known_consumers_settings(), + static function (array $carry, array $settings): array { + $consumer_client_id = $settings['consumer']->clientId; + $carry[$consumer_client_id] = $settings['consumer']->load(); return $carry; }, diff --git a/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php b/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php index 1c5b17d05..8ef385da0 100644 --- a/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php +++ b/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php @@ -30,64 +30,50 @@ public function __construct( } /** - * Function is used for setting the consumer secret. + * Used for setting the consumer secret and password. * - * The secret is stored as an environment variable. - * This command makes sure it is transferred to the database. + * The secret and password is stored as environment variables. + * This command makes sure they are transferred to the database. * - * @param string $client_id + * @param string $clientId * Client id of the consumer. * - * @command dpl_consumers:set-consumer-secret - * @aliases dpl-scs - * @usage dpl_consumers:set-consumer-secret [client_id of the consumer] + * @command dpl_consumers:set-consumer-env-credentials + * @aliases dpl-scec + * @usage dpl_consumers:set-consumer-env-credentials [client_id of the consumer] * * @throws \Exception */ - public function setConsumerSecret(string $client_id): string { - $consumers = $this->getConsumers($client_id); - try { + public function setConsumerEnvCredentials(string $clientId): string { + $consumers = $this->getConsumers($clientId); - $consumer = $consumers[$client_id]; - if (!$consumer) { - throw new \Exception('Could not find consumer.'); - } - if (!$consumer->secret) { - throw new \Exception('Consumer secret not found.'); - } - if (!$consumer_entity = $consumer->load()) { - throw new \Exception('Could not load consumer entity.'); - } - $consumer_entity->save(); - return 'Consumer secret set successfully.'; - } - catch (\Exception $e) { - throw new \Exception($e->getMessage()); - } + $consumer = $consumers[$clientId]; + $consumer->save(); + return 'Consumer secret set successfully.'; } /** - * Function is used for printing out the consumer credentials to the console. + * Used for printing out the consumer UUID to the console. * - * @param string $client_id + * @param string $clientId * Client id of the consumer. * - * @command dpl_consumers:consumer-credentials - * @aliases dpl-gcc - * @usage dpl_consumers:consumer-credentials [client_id of the consumer] + * @command dpl_consumers:get-consumer-uuid + * @aliases dpl-gcu + * @usage dpl_consumers:get-consumer-uuid [client_id of the consumer] * * @throws \Exception */ - public function getConsumerCredentials(string $client_id): void { - $consumers = $this->getConsumers($client_id); - $consumer = $consumers[$client_id]; - $this->output()->writeln(sprintf('Consumer UUID: %s', $consumer->uuid)); + public function getConsumerUuid(string $clientId): void { + $consumers = $this->getConsumers($clientId); + $consumer = $consumers[$clientId]; + $this->output()->writeln(sprintf('Consumer UUID: %s', $consumer->uuid->value)); } /** * Get consumers. * - * @param string $client_id + * @param string $clientId * Client id of the consumer. * * @return mixed[] @@ -95,10 +81,10 @@ public function getConsumerCredentials(string $client_id): void { * * @throws \Exception */ - protected function getConsumers(string $client_id): array { + protected function getConsumers(string $clientId): array { $consumers = dpl_consumers_get_known_consumers(); - if (!in_array($client_id, array_keys($consumers), TRUE)) { - throw new \Exception(sprintf('GraphQL consumer %s is not known.', $client_id)); + if (!in_array($clientId, array_keys($consumers), TRUE)) { + throw new \Exception(sprintf('GraphQL consumer %s is not known.', $clientId)); } return $consumers;