From 3d6cfae662e8f38a611c60fb99b4dae6c123792f Mon Sep 17 00:00:00 2001 From: Steven Ngesera Date: Tue, 29 Oct 2024 23:43:23 +0300 Subject: [PATCH] Adding Notifications --- composer.json | 1 + composer.lock | 130 +++++++++++++++++- .../Channels/Hm_NotificationChannel.php | 8 ++ .../Channels/Hm_SlackChannel.php | 25 ++++ .../Channels/Hm_TelegramChannel.php | 24 ++++ .../Channels/Hm_TwilioChannel.php | 24 ++++ .../Core/Notifications/Hm_Notification.php | 46 +++++++ .../Notifications/Hm_NewMailNotification.php | 26 ++++ services/readme.rd | 17 +++ 9 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 services/Core/Notifications/Channels/Hm_NotificationChannel.php create mode 100644 services/Core/Notifications/Channels/Hm_SlackChannel.php create mode 100644 services/Core/Notifications/Channels/Hm_TelegramChannel.php create mode 100644 services/Core/Notifications/Channels/Hm_TwilioChannel.php create mode 100644 services/Core/Notifications/Hm_Notification.php create mode 100644 services/Notifications/Hm_NewMailNotification.php diff --git a/composer.json b/composer.json index a178a85d3..0d53f4066 100644 --- a/composer.json +++ b/composer.json @@ -59,6 +59,7 @@ "symfony/console": "^6.4", "symfony/dependency-injection": "*", "symfony/dotenv": "^4.3 || 5.4", + "symfony/notifier": "*", "symfony/yaml": "~6.4.3", "thomaspark/bootswatch": "^5.3", "twbs/bootstrap": "^5.3", diff --git a/composer.lock b/composer.lock index 14593bc26..0c9188c87 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "d0b61ae7feb455c0bda982d900d7357a", + "content-hash": "f563ecfadd9972063701823cf07c172b", "packages": [ { "name": "aws/aws-crt-php", @@ -1623,6 +1623,56 @@ }, "time": "2023-04-04T09:54:51+00:00" }, + { + "name": "psr/log", + "version": "3.0.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "reference": "f16e1d5863e37f8d8c2a01719f5b34baa2b714d3", + "shasum": "" + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "support": { + "source": "https://github.com/php-fig/log/tree/3.0.2" + }, + "time": "2024-09-11T13:17:53+00:00" + }, { "name": "ralouphie/getallheaders", "version": "3.0.3", @@ -1980,6 +2030,84 @@ ], "time": "2021-11-23T10:19:22+00:00" }, + { + "name": "symfony/notifier", + "version": "v6.4.13", + "source": { + "type": "git", + "url": "https://github.com/symfony/notifier.git", + "reference": "c46321b53391088861bf627cd9e24873d216cf00" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/notifier/zipball/c46321b53391088861bf627cd9e24873d216cf00", + "reference": "c46321b53391088861bf627cd9e24873d216cf00", + "shasum": "" + }, + "require": { + "php": ">=8.1", + "psr/log": "^1|^2|^3" + }, + "conflict": { + "symfony/event-dispatcher": "<5.4", + "symfony/event-dispatcher-contracts": "<2.5", + "symfony/http-client-contracts": "<2.5", + "symfony/http-kernel": "<5.4" + }, + "require-dev": { + "symfony/event-dispatcher-contracts": "^2.5|^3", + "symfony/http-client-contracts": "^2.5|^3", + "symfony/http-foundation": "^5.4|^6.0|^7.0", + "symfony/messenger": "^5.4|^6.0|^7.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Notifier\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Sends notifications via one or more channels (email, SMS, ...)", + "homepage": "https://symfony.com", + "keywords": [ + "notification", + "notifier" + ], + "support": { + "source": "https://github.com/symfony/notifier/tree/v6.4.13" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2024-09-25T14:18:03+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.31.0", diff --git a/services/Core/Notifications/Channels/Hm_NotificationChannel.php b/services/Core/Notifications/Channels/Hm_NotificationChannel.php new file mode 100644 index 000000000..8abe0bd1d --- /dev/null +++ b/services/Core/Notifications/Channels/Hm_NotificationChannel.php @@ -0,0 +1,8 @@ +transport = $transport; + } + + public function send($notifiable, string $message): void + { + // Assuming $notifiable has a method to get the Slack channel/user ID + $notification = (new Notification('Slack Notification')) + ->content($message); + + $this->transport->send($notification); + } +} diff --git a/services/Core/Notifications/Channels/Hm_TelegramChannel.php b/services/Core/Notifications/Channels/Hm_TelegramChannel.php new file mode 100644 index 000000000..ea8a8af63 --- /dev/null +++ b/services/Core/Notifications/Channels/Hm_TelegramChannel.php @@ -0,0 +1,24 @@ +transport = $transport; + } + + public function send($notifiable, string $message): void + { + $notification = (new Notification('Telegram Notification')) + ->content($message); + + $this->transport->send($notification); + } +} diff --git a/services/Core/Notifications/Channels/Hm_TwilioChannel.php b/services/Core/Notifications/Channels/Hm_TwilioChannel.php new file mode 100644 index 000000000..08360f2bf --- /dev/null +++ b/services/Core/Notifications/Channels/Hm_TwilioChannel.php @@ -0,0 +1,24 @@ +transport = $transport; + } + + public function send($notifiable, string $message): void + { + $notification = (new Notification('SMS Notification')) + ->content($message); + + $this->transport->send($notification); + } +} diff --git a/services/Core/Notifications/Hm_Notification.php b/services/Core/Notifications/Hm_Notification.php new file mode 100644 index 000000000..9bc49f5f8 --- /dev/null +++ b/services/Core/Notifications/Hm_Notification.php @@ -0,0 +1,46 @@ +config = $config; // Set configuration in the constructor + } + + public function via(): array + { + return $this->config['channels'] ?? ['slack', 'telegram']; + } + + public function send($notifiable, string $title, string $content): void + { + $channels = $this->via(); + foreach ($channels as $channel) { + $this->sendThroughChannel($channel, $notifiable, $content); + } + } + + private function sendThroughChannel(string $channel, $notifiable, string $message): void + { + switch ($channel) { + case 'slack': + (new Hm_SlackChannel(new SlackTransport()))->send($notifiable, $message); + break; + case 'telegram': + (new Hm_TelegramChannel(new TelegramTransport()))->send($notifiable, $message); + break; + case 'twilio': + (new Hm_TwilioChannel(new TwilioTransport()))->send($notifiable, $message); + break; + // Add more channels as necessary + } + } +} + diff --git a/services/Notifications/Hm_NewMailNotification.php b/services/Notifications/Hm_NewMailNotification.php new file mode 100644 index 000000000..35805854f --- /dev/null +++ b/services/Notifications/Hm_NewMailNotification.php @@ -0,0 +1,26 @@ +send($notifiable, $title, $message); + } +} diff --git a/services/readme.rd b/services/readme.rd index 45373442d..76ac25e8f 100644 --- a/services/readme.rd +++ b/services/readme.rd @@ -41,4 +41,21 @@ $scheduler->command('backup:database')->dailyAt('02:00'); ``` // Dispatch the event (new NewEmailProcessedEvent)->dispatch('user@example.com'); +``` +``` +// Notification Example usage + +use Services\Notifications\UserNotification; + +// Configure the notification channels +$config = [ + 'channels' => ['slack', 'telegram'], // User-defined channels +]; + +// Create an instance of UserNotification with the specified channels +$notification = new UserNotification($config); + +// Send a message through the specified channels +$message = "Hello! You have a new alert."; +$notification->sendNotification($message); ``` \ No newline at end of file