sbooker/domain-events-persistence

Domain events storage

2.6.1 2024-06-14 19:04 UTC

This package is auto-updated.

Last update: 2024-11-14 20:21:07 UTC


README

Latest Version Software License PHP Version Total Downloads Build Status codecov

Persistence layer for sbooker/domain-events

Installation

Install via Composer:

composer require sbooker/domain-events-persistence

Usage

Publish event

See sbooker/domain-events README. Also you need configure DomainEventPreCommitProcessor

$transactionManager = new TransactionManager(
    new class implements TransactionHandler { ... },
    new DomainEventPreCommitProcessor(
        new PersistentPublisher(...)
    )
);

Subscribe on events

<?php

use Sbooker\DomainEvents\Persistence\ClassNameNameGiver;
use Sbooker\DomainEvents\Persistence\PersistentEvent;


$eventStorage = new class implements \Sbooker\DomainEvents\Persistence\ConsumeStorage {
    public function getFirstByPosition(array $eventNames,int $position): ?PersistentEvent {
        // ...
    }
};

$nameGiver = new ClassNameNameGiver();

$normalizer = new class implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface { /* ... */ };

use Sbooker\DomainEvents\Persistence\Consumer;

$transactionManager = new \Sbooker\TransactionManager\TransactionManager(
    new class implements \Sbooker\TransactionManager\TransactionHandler { /* ... */ };
); 

$denormalizer = new class implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface { /* ... */ };

// Own custom event handler
$eventHandler = new class implements \Sbooker\DomainEvents\Persistence\PersistentEventHandler {
    public function handle(PersistentEvent $event) : void
    {
        // Handle Event
    }
    public function getHandledEventNames() : array
    {
        return [ /* event names as Name Giver returns */ ];
    }
};

// Or with Subscriber 

$subscriber = new class implements \Sbooker\DomainEvents\DomainEventSubscriber {
        public function getListenedEventClasses() : array {
            return [ \Sbooker\DomainEvents\DomainEvent::class ];
        }
        
        public function handleEvent(\Sbooker\DomainEvents\DomainEvent $event) : void {
            // haddle domain event
        }
    };

$eventHandler = new \Sbooker\DomainEvents\Persistence\ConsumerSubscriberBridge(
    $nameGiver,
    $denormalizer, 
    $subscriber
);

$consumer = new Consumer(
    $eventStorage,
    $transactionManager,
    $eventHandler,
    'subscriber.name'
);

// Or all with factory

$factory = new \Sbooker\DomainEvents\Persistence\ConsumerFactory(
        $eventStorage, 
        $transactionManager,
        $nameGiver,
        $denormalizer 
    );

$consumer = $factory->createBySubscriber('subscriber.name', $subscriber);

License

See LICENSE file.