Skip to content

Commit

Permalink
Adding Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Oct 29, 2024
1 parent bec8af8 commit 3d6cfae
Show file tree
Hide file tree
Showing 9 changed files with 300 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
130 changes: 129 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Services\Core\Notifications\Channels;

abstract class Hm_NotificationChannel
{
abstract public function send($notifiable, string $message): void;
}
25 changes: 25 additions & 0 deletions services/Core/Notifications/Channels/Hm_SlackChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Services\Core\Notifications\Channels;

use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;

class Hm_SlackChannel extends Hm_NotificationChannel
{
private SlackTransport $transport;

public function __construct(SlackTransport $transport)
{
$this->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);
}
}
24 changes: 24 additions & 0 deletions services/Core/Notifications/Channels/Hm_TelegramChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Services\Core\Notifications\Channels;

use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransport;

class Hm_TelegramChannel extends Hm_NotificationChannel
{
private TelegramTransport $transport;

public function __construct(TelegramTransport $transport)
{
$this->transport = $transport;
}

public function send($notifiable, string $message): void
{
$notification = (new Notification('Telegram Notification'))
->content($message);

$this->transport->send($notification);
}
}
24 changes: 24 additions & 0 deletions services/Core/Notifications/Channels/Hm_TwilioChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Services\Core\Notifications\Channels;

use Symfony\Component\Notifier\Notification\Notification;
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransport;

class Hm_TwilioChannel extends Hm_NotificationChannel
{
private TwilioTransport $transport;

public function __construct(TwilioTransport $transport)
{
$this->transport = $transport;
}

public function send($notifiable, string $message): void
{
$notification = (new Notification('SMS Notification'))
->content($message);

$this->transport->send($notification);
}
}
46 changes: 46 additions & 0 deletions services/Core/Notifications/Hm_Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Services\Core\Notifications;

use Symfony\Component\Notifier\Notifier;
use Services\Core\Notifications\Channels\Hm_SlackChannel;
use Services\Core\Notifications\Channels\Hm_TwilioChannel;
use Services\Core\Notifications\Channels\Hm_TelegramChannel;

class Hm_Notification
{
public function __construct(private array $config = [])
{
$this->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
}
}
}

26 changes: 26 additions & 0 deletions services/Notifications/Hm_NewMailNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Services\Notifications;

use Services\Core\Notifications\Hm_Notification;

class Hm_NewMailNotification extends Hm_Notification
{
public function __construct(array $config = [])
{
parent::__construct($config);
}

public function via(): array
{
// Specify which channels this notification should use
return ['slack', 'telegram'];
}

public function sendNotification($notifiable, string $message): void
{
// You can define a title and content as needed
$title = "New Notification";
$this->send($notifiable, $title, $message);
}
}
17 changes: 17 additions & 0 deletions services/readme.rd
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

0 comments on commit 3d6cfae

Please sign in to comment.