Skip to content

Commit

Permalink
Merge pull request #1726 from danskernesdigitalebibliotek/DDFBRA-138-…
Browse files Browse the repository at this point in the history
…create-task-for-fetching-graphql-credentials

DDFBRA-138 - Create task for fetching UUID of the graphql_consumer
  • Loading branch information
Dresse authored Nov 20, 2024
2 parents eee407d + 3be45a6 commit fb7001a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,11 @@ tasks:
cmds:
- cmd: task dev:cli -- drush default-content:export-module dpl_example_content

dev:go:graphql-credentials:
desc: Get the GraphQL consumer credentials
cmds:
- cmd: task dev:cli -- drush dpl_consumers:consumer-credentials

ci:reset:
desc: Create CI setup in a clean state
cmds:
Expand Down
36 changes: 36 additions & 0 deletions web/modules/custom/dpl_consumers/dpl_consumers.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* @file
* DPL consumers module.
*/

/**
* Get consumer by Client ID.
*
* @return string
* The consumer UUID.
*
* @throws \Exception
*/
function dpl_consumers_get_consumer_uuid(string $client_id): string {
try {
$consumer = \Drupal::entityTypeManager()
->getStorage('consumer')
->loadByProperties(['client_id' => $client_id]);

// We assume that there is only one consumer with the given client ID
// as it is used an as unique identifier (machine name).
if (!empty($consumer)) {
$consumer = reset($consumer);

return $consumer->uuid() ?? throw new \Exception('UUID not found.');
}
else {
throw new \Exception('Consumer not found.');
}
}
catch (\Exception $e) {
throw new \Exception($e->getMessage());
}
}
5 changes: 5 additions & 0 deletions web/modules/custom/dpl_consumers/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
dpl_consumer.commands:
class: \Drupal\dpl_consumers\Commands\DplConsumersCommands
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Drupal\dpl_consumers\Commands;

use Drupal\dpl_consumers\DplGraphqlConsumersConstants;
use Drush\Commands\DrushCommands;

/**
* Drush commands for DPL consumers.
*/
final class DplConsumersCommands extends DrushCommands {

/**
* Function is used for printing out the consumer credentials to the console.
*
* @command dpl_consumers:consumer-credentials
*/
public function getConsumerCredentials(): void {
$graphql_consumer_client_id = DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID;
$consumer_uuid = dpl_consumers_get_consumer_uuid($graphql_consumer_client_id);
$this->output()->writeln('Consumer UUID: ' . $consumer_uuid);
}

}

0 comments on commit fb7001a

Please sign in to comment.