digitalstate/platform-notification-bundle
## Getting Started
### Minimal Setup
1. **Installation**
Add the bundle to your `composer.json`:
```bash
composer require digitalstate/platform-notification-bundle
Register the bundle in config/bundles.php (if not auto-discovered):
return [
// ...
DigitalState\PlatformNotificationBundle\DigitalStatePlatformNotificationBundle::class => ['all' => true],
];
Database Migrations
Run migrations to create the notification and subscription tables:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case: Creating a Notification
Inject the NotificationManager service and create a notification:
use DigitalState\PlatformNotificationBundle\Entity\Notification;
use DigitalState\PlatformNotificationBundle\Manager\NotificationManager;
public function createNotification(NotificationManager $notificationManager)
{
$notification = new Notification();
$notification->setTitle('Urgent Update');
$notification->setMessage('This is a critical system update.');
$notification->setChannel('email'); // 'email', 'sms', 'letter_mail', etc.
$notificationManager->save($notification);
}
First Use Case: Subscribing a User
Subscribe a user (e.g., User entity) to a notification topic:
use DigitalState\PlatformNotificationBundle\Entity\Subscription;
use DigitalState\PlatformNotificationBundle\Manager\SubscriptionManager;
public function subscribeUser(SubscriptionManager $subscriptionManager, User $user, Notification $notification)
{
$subscription = new Subscription();
$subscription->setUser($user);
$subscription->setNotification($notification);
$subscription->setChannel('email');
$subscription->setIsActive(true);
$subscriptionManager->save($subscription);
}
Where to Look First
src/Entity/Notification.php and src/Entity/Subscription.php for field definitions and relationships.src/Manager/NotificationManager.php and src/Manager/SubscriptionManager.php for CRUD operations.src/Service/NotificationService.php for business logic (e.g., sending notifications).src/Form/ for pre-built forms (if available) to manage notifications/subscriptions via admin panels.Notification entity with metadata (title, message, channel, priority, etc.).NotificationManager to persist or update the notification.$notification = (new Notification())
->setTitle('New Policy Announcement')
->setMessage('Details: [link]')
->setChannel('email')
->setPriority(Notification::PRIORITY_HIGH)
->setIsUrgent(true);
$notificationManager->save($notification);
SubscriptionManager.SubscriptionRepository to query subscriptions (e.g., find all active email subscriptions for a user).$subscription = (new Subscription())
->setUser($user)
->setNotification($notification)
->setChannel('sms')
->setIsActive(true);
$subscriptionManager->save($subscription);
NotificationService to trigger notifications for subscribed users.$notificationService->send($notification, ['email', 'sms']);
# config/oro_platform_notification.yml
oro_platform_notification:
grid:
notification:
columns:
title: ~
channel: ~
priority: ~
filters:
channel: ~
UserRegisteredEvent) and trigger notifications.// src/EventListener/NotificationListener.php
public function onUserRegistered(UserRegisteredEvent $event)
{
$notification = $this->createWelcomeNotification($event->getUser());
$this->notificationService->send($notification, ['email']);
}
EntityConfig system to customize notification/subscription fields (e.g., add custom attributes).Datagrid and DatagridHelper to build admin interfaces for notifications.NotificationService to support custom channels (e.g., push notifications, Slack).ChannelInterface and register it as a service:
class SlackChannel implements ChannelInterface
{
public function send(Notification $notification, User $user): void
{
// Logic to send via Slack
}
}
NotificationManager and SubscriptionManager in unit tests to verify CRUD operations.NotificationService to test sending logic without actual deliveries.letter_mail) may be supported out-of-the-box.SubscriptionManager:
$existing = $subscriptionRepository->findOneBy([
'user' => $user->getId(),
'notification' => $notification->getId(),
'channel' => $channel,
]);
if ($existing) { /* Handle duplicate */ }
user_id, notification_id, and channel columns. Use pagination in admin grids.services:
App\EventListener\NotificationListener:
tags:
- { name: kernel.event_listener, event: user.registered, method: onUserRegistered }
$notification->setMessage($this->translator->trans('notification.welcome.message'));
php bin/console doctrine:schema:validate
// In a listener
error_log('Event triggered: ' . $event->getName());
isModified() or isNew() to debug entity changes:
if ($notification->isModified()) {
error_log('Modified fields: ' . implode(', ', $notification->getModifiedFields()));
}
try {
$this->smsGateway->send($message);
} catch (\Exception $e) {
error_log('SMS failed: ' . $e->getMessage());
// Implement retry logic
}
Notification entity or use inheritance to add domain-specific fields:
class Policy
How can I help you explore Laravel packages today?