sbooker/domain-events

Domain events

2.0.3 2021-06-22 09:10 UTC

This package is auto-updated.

Last update: 2024-11-06 21:21:03 UTC


README

Latest Version Software License PHP Version Total Downloads Build Status codecov

Installation

Install via Composer:

composer require sbooker/domain-events

Usage

Publish events

<?php

namespace Domain;

use Sbooker\DomainEvents\DomainEntity;
use Sbooker\DomainEvents\DomainEvent;
use Sbooker\DomainEvents\DomainEventCollector;

class SomethingOccurred extends  DomainEvent {}

class SomeAggregateRoot implements DomainEntity
{
    use DomainEventCollector;
    
    public function doSomethingOfDomainLogic()
    {
        // do
        $this->publish(new SomethingOccurred());
    }
}

Dispatch events

For dispatching events use Sbooker\Domain\Events\Publihser implementation

class SomeAggregateRoot implements DomainEntity
{
    use DomainEventCollector;
    
    ...
}

$publisher = new class implements Publihser { ... }

$aggregateRoot = new SomeAggregateRoot();
$aggregateRoot->dispatchEvents($publisher)

In more complex cases where Aggregate Root has one or more Domain Entities you must override DomainEventCollector::dispatchEvents as shown bellow:

class Entity implements DomainEntity { ... }

class SomeAggregateRoot implements DomainEntity
{
    use DomainEventCollector { dispatchEvents as doDispatchEvents; }
    
    private Entity $entity;
   
    ...
    
    public function dispatchEvents(Publisher $publisher): void
    {
        $this->doDispatchEvents($publisher);
        $this->entity->dispatchEvents($publisher);
    }
}

License

See LICENSE file.