From aa1b40567785d45e21354534f5a3c4e3effb8fc3 Mon Sep 17 00:00:00 2001 From: Andreas Nielsen Date: Tue, 5 Nov 2024 15:51:55 +0100 Subject: [PATCH 1/3] Added a new task for fetching the UUID of the graphql_consumer. This UUID is used while calling the GraphQL API. --- Taskfile.yml | 5 +++ .../custom/dpl_consumers/dpl_consumers.module | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 web/modules/custom/dpl_consumers/dpl_consumers.module diff --git a/Taskfile.yml b/Taskfile.yml index 401ab912e..792a69e00 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -382,6 +382,11 @@ tasks: cmds: - cmd: task dev:cli -- drush default-content:export-module dpl_example_content + dev:dpl-go:get-graphql-credentials: + desc: Get the GraphQL credentials from the site + cmds: + - cmd: task dev:cli -- drush php-eval "dpl_consumers_print_consumer_credentials()" + ci:reset: desc: Create CI setup in a clean state cmds: diff --git a/web/modules/custom/dpl_consumers/dpl_consumers.module b/web/modules/custom/dpl_consumers/dpl_consumers.module new file mode 100644 index 000000000..660dc7466 --- /dev/null +++ b/web/modules/custom/dpl_consumers/dpl_consumers.module @@ -0,0 +1,44 @@ +getStorage('consumer') + ->loadByProperties(['client_id' => $client_id]); + + if (!empty($consumer)) { + $consumer = reset($consumer); + $uuid = $consumer->uuid(); + return $uuid ?? 'Consumer UUID not found.'; + } + else { + return 'Consumer not found.'; + } + } + catch (\Exception $e) { + \Drupal::logger('dpl_consumers')->error($e->getMessage()); + return 'Error fetching consumer.'; + } +} From 7c7966b7564c253f2288d0eea00a5a253a7635ff Mon Sep 17 00:00:00 2001 From: Andreas Nielsen Date: Mon, 11 Nov 2024 15:18:26 +0100 Subject: [PATCH 2/3] Created a new DplConsumersCommands class for dpl_consumer and moved the logic for fetching consumer UUID from the .module file and into the new Commands class. Updated the Taskfile.yml to use the new drush command instead of calling a function using php-eval. --- Taskfile.yml | 6 ++--- .../custom/dpl_consumers/dpl_consumers.module | 11 -------- .../custom/dpl_consumers/drush.services.yml | 5 ++++ .../src/Commands/DplConsumersCommands.php | 26 +++++++++++++++++++ 4 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 web/modules/custom/dpl_consumers/drush.services.yml create mode 100644 web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php diff --git a/Taskfile.yml b/Taskfile.yml index 792a69e00..9452b6c71 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -382,10 +382,10 @@ tasks: cmds: - cmd: task dev:cli -- drush default-content:export-module dpl_example_content - dev:dpl-go:get-graphql-credentials: - desc: Get the GraphQL credentials from the site + dev:go:graphql-credentials: + desc: Get the GraphQL consumer credentials cmds: - - cmd: task dev:cli -- drush php-eval "dpl_consumers_print_consumer_credentials()" + - cmd: task dev:cli -- drush dpl_consumers:consumer-credentials ci:reset: desc: Create CI setup in a clean state diff --git a/web/modules/custom/dpl_consumers/dpl_consumers.module b/web/modules/custom/dpl_consumers/dpl_consumers.module index 660dc7466..034201aaa 100644 --- a/web/modules/custom/dpl_consumers/dpl_consumers.module +++ b/web/modules/custom/dpl_consumers/dpl_consumers.module @@ -5,17 +5,6 @@ * DPL consumers module. */ -use Drupal\dpl_consumers\DplGraphqlConsumersConstants; - -/** - * Function is used for printing out the consumer credentials to the console. - */ -function dpl_consumers_print_consumer_credentials(): void { - $graphql_consumer_client_id = DplGraphqlConsumersConstants::GRAPHQL_CONSUMER_CLIENT_ID; - $consumer_uuid = dpl_consumers_get_consumer_uuid($graphql_consumer_client_id); - print_r('Consumer UUID: ' . $consumer_uuid); -} - /** * Get consumer by Client ID. * diff --git a/web/modules/custom/dpl_consumers/drush.services.yml b/web/modules/custom/dpl_consumers/drush.services.yml new file mode 100644 index 000000000..350f43c43 --- /dev/null +++ b/web/modules/custom/dpl_consumers/drush.services.yml @@ -0,0 +1,5 @@ +services: + dpl_consumer.commands: + class: \Drupal\dpl_consumers\Commands\DplConsumersCommands + tags: + - { name: drush.command } diff --git a/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php b/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php new file mode 100644 index 000000000..ad9496eb1 --- /dev/null +++ b/web/modules/custom/dpl_consumers/src/Commands/DplConsumersCommands.php @@ -0,0 +1,26 @@ +output()->writeln('Consumer UUID: ' . $consumer_uuid); + } + +} From 3be45a6559686d602d76d192cb7b871216b7c5a4 Mon Sep 17 00:00:00 2001 From: Andreas Nielsen Date: Wed, 20 Nov 2024 16:02:35 +0100 Subject: [PATCH 3/3] Added comment explaining why we expect the first consumer returned to be the correct one. Also now throws exception whenever we do not get the expected results returned. --- .../custom/dpl_consumers/dpl_consumers.module | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/web/modules/custom/dpl_consumers/dpl_consumers.module b/web/modules/custom/dpl_consumers/dpl_consumers.module index 034201aaa..0eb0820fb 100644 --- a/web/modules/custom/dpl_consumers/dpl_consumers.module +++ b/web/modules/custom/dpl_consumers/dpl_consumers.module @@ -10,6 +10,8 @@ * * @return string * The consumer UUID. + * + * @throws \Exception */ function dpl_consumers_get_consumer_uuid(string $client_id): string { try { @@ -17,17 +19,18 @@ function dpl_consumers_get_consumer_uuid(string $client_id): string { ->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); - $uuid = $consumer->uuid(); - return $uuid ?? 'Consumer UUID not found.'; + + return $consumer->uuid() ?? throw new \Exception('UUID not found.'); } else { - return 'Consumer not found.'; + throw new \Exception('Consumer not found.'); } } catch (\Exception $e) { - \Drupal::logger('dpl_consumers')->error($e->getMessage()); - return 'Error fetching consumer.'; + throw new \Exception($e->getMessage()); } }