antismok/domain-events-publisher

Publisher of domain events implemented on symfony dispatcher component

1.1 2018-11-08 02:34 UTC

This package is auto-updated.

Last update: 2024-09-08 15:56:35 UTC


README

Build Status

Publisher of immutable domain events implemented on symfony dispatcher component.

See https://symfony.com/doc/current/components/event_dispatcher.html

Usage

//....

use Antismok\DomainEventPublisher\DomainEvent;

class UserRegistered implements DomainEvent
{
    private $occurredOn;
    
    /**
     * @var string $user
     */
    private $userName;
    
    /**
     * @param string $userName
     */
    function __construct(string $userName)
    {
        $this->useName    = $userName;   
        $this->occurredOn = new DateTime();
    }
    
    public function username(): string
    {
        return $this->username;
    }

    public function occurredOn(): DateTime
    {
        return $this->occurredOn;
    }
}
//....
class UserRegisteredHandler
{
    public function handle(UserCreated $event)
    {
        //Some operation
    }
}
//....
//Some config place
DomainEventPublisher::getInstance()->addListener(UserRegistered::class, [new UserRegisteredHandler, 'handle']);

//Some domain place
DomainEventPublisher::getInstance()->publish(new UserRegistered('Roman'));