coka/notifier-client-bundle
composer require coka/notifier-client-bundle
config/bundles.php:
return [
// ...
CedrickOka\NotifierClientBundle\CokaNotifierClientBundle::class => ['all' => true],
];
config/packages/coka_notifier_client.yaml (default config provided).NotifierClient service and send a notification:
use CedrickOka\NotifierClientBundle\Notifier\NotifierClient;
class MyController extends AbstractController
{
public function __construct(private NotifierClient $notifier)
{
}
public function sendNotification()
{
$this->notifier->send(
'email', // channel
'user@example.com', // recipient
'Welcome!', // subject
'Hello, this is your notification.' // message
);
}
}
Channel-Based Notifications:
Use predefined channels (e.g., email, sms, push) or extend with custom channels.
$this->notifier->send('custom_channel', $recipient, $subject, $message);
Template-Based Messages: Replace static messages with Twig templates for dynamic content:
# config/packages/coka_notifier_client.yaml
templates:
email:
welcome: 'emails/welcome.html.twig'
$this->notifier->send('email', 'user@example.com', 'Welcome', [
'name' => 'John'
], 'welcome'); // Uses 'welcome' template
Batch Processing: Queue notifications for async delivery (if configured):
$this->notifier->queue('email', 'user@example.com', 'Subject', 'Message');
Event-Driven Triggers:
Dispatch notifications from Symfony events (e.g., KernelEvents::TERMINATE):
use Symfony\Component\HttpKernel\Event\TerminateEvent;
use Symfony\Component\HttpKernel\KernelEvents;
$eventDispatcher->addListener(KernelEvents::TERMINATE, function (TerminateEvent $event) {
$this->notifier->send('email', 'admin@example.com', 'System Alert', 'Check logs!');
});
HttpKernel bridge to integrate with Laravel’s service container:
$container->register('notifier', NotifierClient::class)
->addArgument($container->getParameter('coka_notifier_client.config'));
# config/packages/coka_notifier_client.yaml
logging: true
NotifierClient in PHPUnit:
$mockNotifier = $this->createMock(NotifierClient::class);
$mockNotifier->expects($this->once())->method('send');
$this->container->set('notifier', $mockNotifier);
Channel Validation:
InvalidArgumentException. Always validate channels exist in config:
if (!$this->notifier->supportsChannel('custom_channel')) {
throw new \RuntimeException('Channel not configured.');
}
ChannelInterface to add new channels dynamically.Template Resolution:
templates/ or a configured path. Missing templates cause silent failures.debug: true in config to log unresolved templates.Async Queueing:
coka_notifier_client.queue_connection is configured (e.g., doctrine or symfony).php bin/console coka:notifier:consume
Recipient Sanitization:
if (!filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
throw new \InvalidArgumentException('Invalid email.');
}
var/log/dev.log for notification failures (enable logging: true).dump($this->notifier->getChannel('email')->getConfig());
NotifierEvents::FAILED to handle errors:
$eventDispatcher->addListener(NotifierEvents::FAILED, function (NotificationFailedEvent $event) {
// Handle failure (e.g., retry or alert)
});
Custom Channels:
Implement CedrickOka\NotifierClientBundle\Notifier\ChannelInterface:
class SlackChannel implements ChannelInterface
{
public function send($recipient, $message): bool
{
// Custom logic (e.g., HTTP call to Slack API)
return true;
}
}
Register in config:
channels:
slack:
class: App\Notifier\SlackChannel
webhook_url: '%env(SLACK_WEBHOOK_URL)%'
Middleware:
Add pre/post-send logic via NotifierEvents::BEFORE_SEND/NotifierEvents::AFTER_SEND:
$eventDispatcher->addListener(NotifierEvents::BEFORE_SEND, function (NotificationEvent $event) {
$event->setMessage(strtoupper($event->getMessage()));
});
Configuration Overrides:
Override default config in config/packages/overrides/coka_notifier_client.yaml:
default_channel: 'sms'
channels:
email:
from: 'noreply@yourdomain.com'
How can I help you explore Laravel packages today?