From f532334600370390d799fcaab6b18bd17815c16a Mon Sep 17 00:00:00 2001 From: scrummer Date: Wed, 26 Jan 2022 01:10:24 +0100 Subject: [PATCH] #1 reset object per default on send Signed-off-by: scrummer --- .../DiscordWebhookExtension.php | 2 ++ src/DiscordWebhookBundle/DiscordWebhook.php | 20 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/DiscordWebhookBundle/DependencyInjection/DiscordWebhookExtension.php b/src/DiscordWebhookBundle/DependencyInjection/DiscordWebhookExtension.php index 85c8c8d..dd462bf 100644 --- a/src/DiscordWebhookBundle/DependencyInjection/DiscordWebhookExtension.php +++ b/src/DiscordWebhookBundle/DependencyInjection/DiscordWebhookExtension.php @@ -30,6 +30,7 @@ public function load(array $configs, ContainerBuilder $container): void ->register(DiscordWebhook::class, DiscordWebhook::class) ->setPublic(true) ->setArgument('$url', $config['default_url']) + ->setArgument('$serviceName', DiscordWebhook::class) ->addTag('discord_webhook.client', ['key' => DiscordWebhook::class]); // configure the additional named clients @@ -47,6 +48,7 @@ private function configureClients(array $config, ContainerBuilder $container, Fi ->register($name, DiscordWebhook::class) ->setPublic(true) ->setArgument('$url', $clientConfig['webhook_url']) + ->setArgument('$serviceName', $name) ->addMethodCall('setUsername', [$clientConfig['username']]) ->addMethodCall('setAvatar', [$clientConfig['avatar_url']]) ->addTag('discord_webhook.client', ['key' => $name]); diff --git a/src/DiscordWebhookBundle/DiscordWebhook.php b/src/DiscordWebhookBundle/DiscordWebhook.php index 2b6ca4a..995124a 100644 --- a/src/DiscordWebhookBundle/DiscordWebhook.php +++ b/src/DiscordWebhookBundle/DiscordWebhook.php @@ -13,9 +13,27 @@ */ class DiscordWebhook extends Webhook { - public function __construct(array|string $url = []) + private string $serviceName; + + public function __construct(array|string $url = [], string $serviceName = DiscordWebhook::class) { parent::__construct($url); + + $this->serviceName = $serviceName; + } + + public function send(): bool + { + $status = parent::send(false); + $ignoredFields = []; + + if ($this->serviceName !== DiscordWebhook::class) { + $ignoredFields = ['username', 'avatarUrl']; + } + + $this->reset($ignoredFields); + + return $status; } public function setUrl(string $webhook): void