Search by

adachsoft / notification

Arkadiusz Adach

Notification dispatcher for adachsoft/notification-contracts: resolves recipients (groups to accounts) via principal-contracts, applies channel preferences and sends through pluggable channels.

Package info

gitlab.com/a.adach/notification

Issues

pkg:composer/adachsoft/notification

Statistics

Installs: 2

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.0 2026-11-23 13:04 UTC

This package is auto-updated.

Last update: 2026-11-23 12:05:09 UTC


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-contracts
  • adachsoft/principal-contracts
  • adachsoft/reference-contracts
  • psr/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.