Search by

adachsoft / notification-contracts

Arkadiusz Adach

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

Issues

pkg:composer/adachsoft/notification-contracts

Statistics

Installs: 14

Dependents: 3

Suggesters: 0

Stars: 0

v0.1.0 2026-11-23 12:16 UTC

This package is auto-updated.

Last update: 2026-11-23 11:16:47 UTC


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

  • NotificationChannelInterface sends to one already-resolved leaf recipient. It returns SENT after delivery or SKIPPED when the recipient cannot be reached; transport failures are thrown as NotificationDeliveryException.
  • ChannelPreferenceProviderInterface returns the ordered channel names to use for a leaf recipient and notification. An empty list means no delivery should be attempted.
  • NotificationDispatcherInterface coordinates recipient resolution, preferences, and channel sends. Its implementation is provided by the separate adachsoft/notification package, not by this contracts package. Delivery failures are collected as FAILED results 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.