adachsoft / event-journal-contracts
Framework-agnostic contracts and value objects for recording and querying operational events.
Package info
gitlab.com/a.adach/event-journal-contracts
pkg:composer/adachsoft/event-journal-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 journal repository contract tests.
Provides
None
Conflicts
None
Replaces
None
README
A framework-agnostic contract library for recording and querying operational events and problems. Applications can use it for worker errors, offline devices, failed actions, HTTP 5xx responses, and other incidents without coupling the journal model to a specific host or persistence engine.
This package defines value objects, DTOs, repository and service ports, and reusable repository contract tests. It does not provide a production persistence adapter or an event journal service implementation.
Installation
composer require adachsoft/event-journal-contracts
The only production dependency is adachsoft/reference-contracts, which supplies PrincipalReference for acknowledgement data.
Requirements
- PHP 8.3 or later.
- Composer for dependency installation.
Example
See Recording an Event for a minimal integration example using EventJournalInterface.
Model
EntryIdis a non-empty identifier of up to 64 ASCII characters using letters, digits, underscores, dots, colons, and hyphens.JournalLevelEnumprovides orderedinfo,notice,warning,error, andcriticalseverities.SubjectReferenceidentifies the optional object an event concerns, such as a device or schedule.JournalEventDtois the input toEventJournalInterface::record(). Its source is non-empty, its code uses lowercase letters, digits, underscores, dots, or hyphens, and its context contains JSON-compatible values.JournalEntryDtois the stored immutable representation. It retains the highest observed level, the latest message and context, the first and last occurrence times, an occurrence count, and optional acknowledgement data.JournalEntryCollectionis an immutable, typed iterable collection of stored entries.JournalQueryDtosupports minimum severity, source and code lists, subject, inclusivelastOccurredAtdate bounds, acknowledgement state, and pagination. Values within a source list or code list match with OR; distinct filters are combined with AND. Repository results are ordered by newestlastOccurredAtfirst.
Ports
JournalEntryRepositoryInterfacedefines persistence, fingerprint lookup, filtering, counting, removal, and pruning.EventJournalInterfacedefines recording and acknowledgement operations.JournalListenerInterfaceobserves recorded and acknowledged entries. Listener failures during recording must be isolated and must not make recording fail.
Deduplication
An event fingerprint is source|code|subject type|subject id; when there is no subject, the final two parts are empty. A journal implementation applies its configured deduplication window to an unacknowledged entry with the same fingerprint. When the prior entry is within that window, the implementation increments occurrences, preserves the first occurrence and identifier, updates the latest message, context, and occurrence time, and retains the more severe level. Otherwise, it creates a new entry. Acknowledged entries are not eligible for deduplication.
The contracts intentionally leave the deduplication window configuration to the service implementation.
Repository contract tests
The package ships AdachSoft\EventJournalContracts\Testing\AbstractJournalEntryRepositoryContractTest and InMemoryJournalEntryRepository through its Composer autoload mapping. PHPUnit is suggested, but is not a production dependency.
Extend the contract test and return the repository implementation being tested:
use AdachSoft\EventJournalContracts\Repository\JournalEntryRepositoryInterface;
use AdachSoft\EventJournalContracts\Testing\AbstractJournalEntryRepositoryContractTest;
final class MyJournalEntryRepositoryContractTest extends AbstractJournalEntryRepositoryContractTest
{
protected function createRepository(): JournalEntryRepositoryInterface
{
return new MyJournalEntryRepository(/* test configuration */);
}
}
Run that test with PHPUnit alongside the implementation's other tests. The shared suite verifies saving and replacing, lookup of the latest unacknowledged fingerprint match, all query filters and pagination, count without pagination, removal, and pruning. InMemoryJournalEntryRepository is a small example implementation for tests, not a production storage recommendation.