Skip to content

Commit 3d6cfae

Browse files
committed
Adding Notifications
1 parent bec8af8 commit 3d6cfae

File tree

9 files changed

+300
-1
lines changed

9 files changed

+300
-1
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"symfony/console": "^6.4",
6060
"symfony/dependency-injection": "*",
6161
"symfony/dotenv": "^4.3 || 5.4",
62+
"symfony/notifier": "*",
6263
"symfony/yaml": "~6.4.3",
6364
"thomaspark/bootswatch": "^5.3",
6465
"twbs/bootstrap": "^5.3",

composer.lock

Lines changed: 129 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Services\Core\Notifications\Channels;
4+
5+
abstract class Hm_NotificationChannel
6+
{
7+
abstract public function send($notifiable, string $message): void;
8+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Services\Core\Notifications\Channels;
4+
5+
use Symfony\Component\Notifier\Notification\Notification;
6+
use Symfony\Component\Notifier\Bridge\Slack\SlackTransport;
7+
8+
class Hm_SlackChannel extends Hm_NotificationChannel
9+
{
10+
private SlackTransport $transport;
11+
12+
public function __construct(SlackTransport $transport)
13+
{
14+
$this->transport = $transport;
15+
}
16+
17+
public function send($notifiable, string $message): void
18+
{
19+
// Assuming $notifiable has a method to get the Slack channel/user ID
20+
$notification = (new Notification('Slack Notification'))
21+
->content($message);
22+
23+
$this->transport->send($notification);
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Services\Core\Notifications\Channels;
4+
5+
use Symfony\Component\Notifier\Notification\Notification;
6+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramTransport;
7+
8+
class Hm_TelegramChannel extends Hm_NotificationChannel
9+
{
10+
private TelegramTransport $transport;
11+
12+
public function __construct(TelegramTransport $transport)
13+
{
14+
$this->transport = $transport;
15+
}
16+
17+
public function send($notifiable, string $message): void
18+
{
19+
$notification = (new Notification('Telegram Notification'))
20+
->content($message);
21+
22+
$this->transport->send($notification);
23+
}
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Services\Core\Notifications\Channels;
4+
5+
use Symfony\Component\Notifier\Notification\Notification;
6+
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransport;
7+
8+
class Hm_TwilioChannel extends Hm_NotificationChannel
9+
{
10+
private TwilioTransport $transport;
11+
12+
public function __construct(TwilioTransport $transport)
13+
{
14+
$this->transport = $transport;
15+
}
16+
17+
public function send($notifiable, string $message): void
18+
{
19+
$notification = (new Notification('SMS Notification'))
20+
->content($message);
21+
22+
$this->transport->send($notification);
23+
}
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Services\Core\Notifications;
4+
5+
use Symfony\Component\Notifier\Notifier;
6+
use Services\Core\Notifications\Channels\Hm_SlackChannel;
7+
use Services\Core\Notifications\Channels\Hm_TwilioChannel;
8+
use Services\Core\Notifications\Channels\Hm_TelegramChannel;
9+
10+
class Hm_Notification
11+
{
12+
public function __construct(private array $config = [])
13+
{
14+
$this->config = $config; // Set configuration in the constructor
15+
}
16+
17+
public function via(): array
18+
{
19+
return $this->config['channels'] ?? ['slack', 'telegram'];
20+
}
21+
22+
public function send($notifiable, string $title, string $content): void
23+
{
24+
$channels = $this->via();
25+
foreach ($channels as $channel) {
26+
$this->sendThroughChannel($channel, $notifiable, $content);
27+
}
28+
}
29+
30+
private function sendThroughChannel(string $channel, $notifiable, string $message): void
31+
{
32+
switch ($channel) {
33+
case 'slack':
34+
(new Hm_SlackChannel(new SlackTransport()))->send($notifiable, $message);
35+
break;
36+
case 'telegram':
37+
(new Hm_TelegramChannel(new TelegramTransport()))->send($notifiable, $message);
38+
break;
39+
case 'twilio':
40+
(new Hm_TwilioChannel(new TwilioTransport()))->send($notifiable, $message);
41+
break;
42+
// Add more channels as necessary
43+
}
44+
}
45+
}
46+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Services\Notifications;
4+
5+
use Services\Core\Notifications\Hm_Notification;
6+
7+
class Hm_NewMailNotification extends Hm_Notification
8+
{
9+
public function __construct(array $config = [])
10+
{
11+
parent::__construct($config);
12+
}
13+
14+
public function via(): array
15+
{
16+
// Specify which channels this notification should use
17+
return ['slack', 'telegram'];
18+
}
19+
20+
public function sendNotification($notifiable, string $message): void
21+
{
22+
// You can define a title and content as needed
23+
$title = "New Notification";
24+
$this->send($notifiable, $title, $message);
25+
}
26+
}

services/readme.rd

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,21 @@ $scheduler->command('backup:database')->dailyAt('02:00');
4141
```
4242
// Dispatch the event
4343
(new NewEmailProcessedEvent)->dispatch('user@example.com');
44+
```
45+
```
46+
// Notification Example usage
47+
48+
use Services\Notifications\UserNotification;
49+
50+
// Configure the notification channels
51+
$config = [
52+
'channels' => ['slack', 'telegram'], // User-defined channels
53+
];
54+
55+
// Create an instance of UserNotification with the specified channels
56+
$notification = new UserNotification($config);
57+
58+
// Send a message through the specified channels
59+
$message = "Hello! You have a new alert.";
60+
$notification->sendNotification($message);
4461
```

0 commit comments

Comments
 (0)