adachsoft / notification-contracts
Notification contracts: technology-agnostic notification model, channel SPI, channel preferences and dispatcher interface; channels (e-mail, SMS, push) are separate adapter packages.
Package info
gitlab.com/a.adach/notification-contracts
pkg:composer/adachsoft/notification-contracts
Requires
- php: ^8.3
- adachsoft/reference-contracts: ^0.1
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
- phpunit/phpunit: Required to run the reusable channel contract test from testing/
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/notification-contracts defines a technology-agnostic notification model and the contracts used by notification channels, preference providers, and dispatchers. The core identifies who should receive a notification and what it contains; it does not know recipient addresses or delivery transports.
Each channel is an adapter in its own package. An e-mail, SMS, WhatsApp, or Web Push adapter resolves the address for a leaf principal and sends through its own transport. Adding a channel therefore does not require changes to this package. Notification bodies are plain text; HTML rendering and templating belong in a separate package.
Requirements
- PHP 8.3 or later.
adachsoft/reference-contracts:^0.1, installed automatically as a runtime dependency.
Installation
composer require adachsoft/notification-contracts
Notification model
Create an immutable NotificationDto with a PrincipalReference, a trimmed subject, and a plain-text body. The category is a lowercase dotted identifier such as reminder or list.shared; an optional SubjectReference describes what caused the notification, and optional metadata must be JSON-serializable.
A channel produces a DeliveryResultDto for a leaf recipient. Its status is one of DeliveryStatusEnum::SENT, SKIPPED, or FAILED. DeliveryResultCollection keeps results in order and offers status, recipient, and channel filters.
Invalid model values throw InvalidNotificationException. Transport failures are represented by NotificationDeliveryException.
Usage
Create a NotificationDto with its principal reference, subject, plain-text body, and optional category, source, and metadata. Pass it to a channel for a single leaf recipient; use NotificationDispatcherInterface when the application should resolve recipients, apply preferences, and coordinate multiple channel attempts. The in-memory channel can exercise the same flow in tests. See docs/examples/basic-usage.md for a complete runnable example.
Contracts
NotificationChannelInterfacesends to one already-resolved leaf recipient. It returnsSENTafter delivery orSKIPPEDwhen the recipient cannot be reached; transport failures are thrown asNotificationDeliveryException.ChannelPreferenceProviderInterfacereturns the ordered channel names to use for a leaf recipient and notification. An empty list means no delivery should be attempted.NotificationDispatcherInterfacecoordinates recipient resolution, preferences, and channel sends. Its implementation is provided by the separateadachsoft/notificationpackage, not by this contracts package. Delivery failures are collected asFAILEDresults and do not stop other attempts.
Channel packages own address resolution and transport details. The core does not depend on an authentication system, a mailer, a clock abstraction, or any specific delivery provider.
Implementing a channel
Implement NotificationChannelInterface in the channel package. Return a stable name matching /^[a-z0-9_]+$/, handle one leaf recipient per send() call, and throw NotificationDeliveryException::forChannel() when the transport fails.
Channel packages can reuse AdachSoft\NotificationContracts\Testing\AbstractNotificationChannelContractTest. Add PHPUnit as a development dependency, extend the abstract test, and implement its hooks for reachable/unreachable recipients, simulated transport failure, and checking actual delivery. PHPUnit is an optional development tool for this package and is listed under suggest.
In-memory channel
InMemoryNotificationChannel is a stateful test channel. It records successful sends, can be configured to skip or fail for selected recipients, and accepts an optional fixed result time. It is intended for application and dispatcher tests, not production delivery.
See docs/examples/basic-usage.md for a runnable notification creation and in-memory send example.