adachsoft / notification
Notification dispatcher for adachsoft/notification-contracts: resolves recipients (groups to accounts) via principal-contracts, applies channel preferences and sends through pluggable channels.
Requires
- php: ^8.3
- adachsoft/notification-contracts: ^0.1
- adachsoft/principal-contracts: ^0.2
- adachsoft/reference-contracts: ^0.1
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/notification dispatches notifications defined by adachsoft/notification-contracts. It resolves a notification recipient to leaf principals, applies ordered channel preferences, and collects each delivery outcome. Channels are independent adapters; this package contains no transport or persistence implementation.
Requirements and installation
- PHP
^8.3 adachsoft/notification-contractsadachsoft/principal-contractsadachsoft/reference-contractspsr/clock
Install the package with Composer:
composer require adachsoft/notification
Usage
Quick start
The factory defaults to all registered channel names, in registration order, and a UTC system clock. InMemoryNotificationChannel is useful for examples and tests; use a production channel adapter to deliver real notifications.
<?php
declare(strict_types=1);
use AdachSoft\Notification\NotificationDispatcherFactory;
use AdachSoft\NotificationContracts\InMemory\InMemoryNotificationChannel;
use AdachSoft\NotificationContracts\Model\NotificationDto;
use AdachSoft\PrincipalContracts\CompositePrincipalResolver;
use AdachSoft\PrincipalContracts\LeafPrincipalExpander;
use AdachSoft\ReferenceContracts\PrincipalReference;
require __DIR__ . '/vendor/autoload.php';
$principals = new CompositePrincipalResolver([
new LeafPrincipalExpander(['account']),
]);
$channel = new InMemoryNotificationChannel('email');
$dispatcher = NotificationDispatcherFactory::create($principals, [$channel]);
$notification = new NotificationDto(
PrincipalReference::fromString('account:42'),
'A reminder',
'Your scheduled task is due.',
);
$results = $dispatcher->dispatch($notification);
printf("Sent: %d; failed: %s\n", count($results->sent()), $results->hasFailures() ? 'yes' : 'no');
For a complete runnable script, see docs/examples/basic-usage.md.
Recipient resolution
The dispatcher calls PrincipalResolverInterface::resolve() once for the notification recipient. With LeafPrincipalExpander(['account']), an account resolves to itself. Applications that support groups can compose a group expander so a group resolves to its member accounts, including nested groups.
For example, the optional adachsoft/principal-group integration exposes GroupServiceFactory::createPrincipalResolver($groupRepository). That integration is not a dependency of this package.
Channel preferences
Use StaticChannelPreferenceProvider to return the same ordered channel list for every recipient and notification:
use AdachSoft\Notification\Preference\StaticChannelPreferenceProvider;
$preferences = new StaticChannelPreferenceProvider(['email', 'web_push']);
Use CategoryChannelPreferenceProvider when channel choices depend on the notification category. An explicit empty list mutes that category; categories without an entry use the default list:
use AdachSoft\Notification\Preference\CategoryChannelPreferenceProvider;
$preferences = new CategoryChannelPreferenceProvider(
[
'reminder' => ['email'],
'list.shared' => ['email', 'web_push'],
'digest' => [],
],
['email'],
);
Channel names use lowercase letters, digits, and underscores. Category names use lowercase dotted identifiers.
Delivery failures
A channel failure, including a thrown NotificationDeliveryException or other Throwable, becomes a FAILED result and does not prevent later channel or recipient attempts. A requested channel that is not registered also produces a FAILED result. Channel SENT and SKIPPED results are preserved as returned.
Inspect outcomes with DeliveryResultCollection::failed() and DeliveryResultCollection::hasFailures(). A principal-resolution failure occurs before delivery begins and is thrown as NotificationException with the original exception as its previous exception.
Adding a channel
Implement NotificationChannelInterface in a separate adapter package and register it with the dispatcher. For example, adachsoft/notification-channel-email can own e-mail address resolution and its transport. Future SMS and WhatsApp channels can be added in the same way without changing this dispatcher.
Retries and queues can be added later as decorators of NotificationDispatcherInterface; delivery transports and persistence remain outside this package.