Skip to content

Commit

Permalink
Refactor consumer drush cmds
Browse files Browse the repository at this point in the history
Adjust cmds to fit new way of handling consumers and be more specific
about what each command actually is doing.
  • Loading branch information
spaceo committed Jan 5, 2025
1 parent d4c0ca4 commit afe1f18
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
2 changes: 1 addition & 1 deletion web/modules/custom/dpl_consumers/dpl_consumers.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 5 additions & 5 deletions web/modules/custom/dpl_consumers/dpl_consumers.module
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down Expand Up @@ -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;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,61 @@ 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[]
* An array of consumer keyed by client id.
*
* @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;
Expand Down

0 comments on commit afe1f18

Please sign in to comment.