sbooker/domain-events-persistence

2.3.1 2021-10-31 10:24 UTC

This package is auto-updated.

Last update: 2021-12-31 00:36:53 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,
    new \Sbooker\PersistentPointer\Repository(/* ... */),
    $eventHandler,
    'subscriber.name'
);

// Or all with factory

$factory = new \Sbooker\DomainEvents\Persistence\ConsumerFactory(
        $eventStorage, 
        $transactionManager,
        new \Sbooker\PersistentPointer\Repository(/* ... */),
        $nameGiver,
        $denormalizer 
    );

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

License

See LICENSE file.