From dd0b6e72eb82cb20209205ee53801afdefb47c39 Mon Sep 17 00:00:00 2001
From: Andreas Fernandez <a.fernandez@scripting-base.de>
Date: Tue, 25 Apr 2023 16:22:48 +0200
Subject: [PATCH] [TASK] Show configured transport per webhook
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The "Webhooks" view in the "Configuration" module now shows the
Symfony Messenger transport configured per webhook.

Resolves: #100752
Releases: main
Change-Id: I809cb376a24591249697c828b9454e4168e7bbd2
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/78841
Reviewed-by: Jörg Bösche <typo3@joergboesche.de>
Tested-by: Benni Mack <benni@typo3.org>
Tested-by: core-ci <typo3@b13.com>
Tested-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Oliver Bartsch <bo@cedev.de>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../WebhookTypesProvider.php                  | 27 ++++++++++++++++++-
 .../webhooks/Configuration/Services.yaml      |  2 ++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/webhooks/Classes/ConfigurationModuleProvider/WebhookTypesProvider.php b/typo3/sysext/webhooks/Classes/ConfigurationModuleProvider/WebhookTypesProvider.php
index d9f75cb286eb..050f7fcfe1ff 100644
--- a/typo3/sysext/webhooks/Classes/ConfigurationModuleProvider/WebhookTypesProvider.php
+++ b/typo3/sysext/webhooks/Classes/ConfigurationModuleProvider/WebhookTypesProvider.php
@@ -17,7 +17,10 @@ declare(strict_types=1);
 
 namespace TYPO3\CMS\Webhooks\ConfigurationModuleProvider;
 
+use Symfony\Component\DependencyInjection\ServiceLocator;
+use TYPO3\CMS\Core\Messaging\WebhookMessageInterface;
 use TYPO3\CMS\Lowlevel\ConfigurationModuleProvider\AbstractProvider;
+use TYPO3\CMS\Webhooks\Model\WebhookType;
 use TYPO3\CMS\Webhooks\WebhookTypesRegistry;
 
 /**
@@ -28,7 +31,8 @@ use TYPO3\CMS\Webhooks\WebhookTypesRegistry;
 class WebhookTypesProvider extends AbstractProvider
 {
     public function __construct(
-        private readonly WebhookTypesRegistry $webhookTypesRegistry
+        private readonly WebhookTypesRegistry $webhookTypesRegistry,
+        private readonly ServiceLocator $sendersLocator,
     ) {
     }
 
@@ -40,8 +44,29 @@ class WebhookTypesProvider extends AbstractProvider
                 'messageName' => $webhookType->getServiceName(),
                 'description' => $webhookType->getDescription(),
                 'connectedEvent' => $webhookType->getConnectedEvent() ?? 'none',
+                'transport' => $this->determineTransportForWebhook($webhookType),
             ];
         }
         return $configuration;
     }
+
+    /**
+     * @param WebhookType $webhookType
+     * @return array{identifier: non-empty-string, serviceName: non-empty-string}
+     */
+    private function determineTransportForWebhook(WebhookType $webhookType): array
+    {
+        $transportName =
+            $GLOBALS['TYPO3_CONF_VARS']['SYS']['messenger']['routing'][$webhookType->getServiceName()]
+            ?? $GLOBALS['TYPO3_CONF_VARS']['SYS']['messenger']['routing'][WebhookMessageInterface::class]
+            ?? $GLOBALS['TYPO3_CONF_VARS']['SYS']['messenger']['routing']['*']
+            ?? 'undefined';
+
+        return [
+            'identifier' => $transportName,
+            'serviceName' => $this->sendersLocator->has($transportName)
+                ? get_class($this->sendersLocator->get($transportName))
+                : 'undefined',
+        ];
+    }
 }
diff --git a/typo3/sysext/webhooks/Configuration/Services.yaml b/typo3/sysext/webhooks/Configuration/Services.yaml
index 15f3772aa4ec..eb5ce35fc0c7 100644
--- a/typo3/sysext/webhooks/Configuration/Services.yaml
+++ b/typo3/sysext/webhooks/Configuration/Services.yaml
@@ -31,6 +31,8 @@ services:
 
   lowlevel.configuration.module.provider.webhooks:
     class: 'TYPO3\CMS\Webhooks\ConfigurationModuleProvider\WebhookTypesProvider'
+    arguments:
+      $sendersLocator: !tagged_locator { tag: 'messenger.sender', index_by: 'identifier' }
     tags:
       - name: 'lowlevel.configuration.module.provider'
         identifier: 'webhooks'
-- 
GitLab