symfony/mattermost-notifier
Symfony Notifier integration for Mattermost. Configure via DSN (access token, host/path, default channel) and send ChatMessage notifications, optionally overriding the recipient channel with MattermostOptions.
Illuminate\Events\Dispatcher), enabling seamless integration for asynchronous notifications like job failures, system alerts, or user actions. Its PSR-15 middleware support complements Laravel’s middleware stack (e.g., Illuminate\Pipeline), facilitating request/response handling.Notifier, Transport) allows for easy abstraction, enabling Laravel to mock or swap providers (e.g., Mattermost ↔ Slack) without refactoring business logic. This is particularly useful for Laravel’s dependency injection system.HttpClient is not native to Laravel, the package’s core functionality (DSN-based configuration, ChatMessage objects) can be adapted using Laravel’s Http client or Guzzle. The PSR-15 middleware can be replaced with Laravel’s middleware, ensuring compatibility.Http client or Guzzle can handle POST requests to Mattermost’s endpoint with minimal boilerplate.mattermost://TOKEN@HOST/PATH?channel=ID) can be mapped to Laravel’s .env or config/mattermost.php for centralized management. Example:
MATTERMOST_DSN=mattermost://${MATTERMOST_TOKEN}@${MATTERMOST_HOST}/api/v4/posts?channel=${DEFAULT_CHANNEL}
Event facade can trigger notifications via the package’s Notifier interface. For example:
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Message\ChatMessage;
// In a Laravel event listener:
public function handle(JobFailed $event) {
$notifier = new Notifier(new MattermostTransport($this->mattermostDsn));
$notifier->send(new ChatMessage('Job failed: ' . $event->job->name));
}
MattermostOptions class allows per-message channel overrides, which can be extended in Laravel via dynamic properties or service providers.Notifier namespace may conflict with Laravel’s autoloading. Mitigation: Use composer.json aliases or PSR-4 prefixes (e.g., Symfony\Component\Notifier\Bridge\Mattermost).ContainerInterface with Laravel’s Illuminate\Contracts\Container\Container).Illuminate\Queue) should wrap notifications to handle retries/failures. Example:
$notifier->send($message)->then(function () {
// Success
})->otherwise(function ($e) {
Log::error('Mattermost notification failed', ['error' => $e]);
});
$message = new ChatMessage(Blade::render('notifications.job-failed', ['job' => $event->job]));
dispatchSync() or queue the notification.Notifiable trait) or augment them? If augmenting, how will conflicts (e.g., duplicate alerts) be resolved?.env, Laravel Vault, AWS Secrets Manager)?FailedJob table, dead-letter queues)?Http client mocking, PestPHP assertions)?Event facade or dispatch() helper.queue:work for reliability (e.g., Illuminate\Bus\Queueable).Notifier interface to a Laravel-specific implementation in AppServiceProvider:
public function register() {
$this->app->bind(\Symfony\Component\Notifier\Notifier::class, function ($app) {
return new \Symfony\Component\Notifier\Notifier(
new \Symfony\Component\Notifier\Bridge\Mattermost\MattermostTransport(
config('mattermost.dsn')
)
);
});
}
HttpClient with Laravel’s Http client or Guzzle for consistency.composer.json:
{
"require": {
"symfony/notifier": "^6.4",
"symfony/http-client": "^6.4",
"guzzlehttp/guzzle": "^7.0" // Optional: For Laravel Http client compatibility
},
"replace": {
"symfony/http-client": "guzzlehttp/guzzle" // Optional: Swap for Laravel's Http client
}
}
Http client directly with Mattermost’s API (avoids Symfony dependency).spatie/laravel-notification-channels-mattermost (Laravel-specific).Phase 1: Proof of Concept (1–2 weeks)
composer.json..env:
MATTERMOST_DSN=mattermost://${MATTERMOST_TOKEN}@${MATTERMOST_HOST}/api/v4/posts?channel=${DEFAULT_CHANNEL}
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Message\ChatMessage;
class SendMattermostAlert
{
public function __construct(private Notifier $notifier) {}
public function handle(JobFailed $event) {
$this->notifier->send(new ChatMessage('Job failed: ' . $event->job->name));
}
}
EventServiceProvider:
protected $listen = [
JobFailed::class => [SendMattermostAlert::class],
];
event(new JobFailed((new Job)->name('test-job')));
Phase 2: Full Integration (2–4 weeks)
Notification classes (extend Illuminate\Notifications\Notification).ShouldQueue).namespace App\Notifications\Channels;
use Symfony\Component\Notifier\Notifier;
use Symfony\Component\Notifier\Message\ChatMessage;
use Illuminate\Notifications\Notification;
class MattermostChannel
{
public function __construct(private Notifier $notifier) {}
public function send($notifiable, Notification $notification) {
$message = $notification->toMattermost($notifiable);
$this->notifier->send($message);
}
}
Notification class to support Mattermost:
namespace App\Notifications;
use Illuminate\Notifications\Notification;
use App\Notifications
How can I help you explore Laravel packages today?