gbprod/domain-event-bundle

This package is abandoned and no longer maintained. No replacement package was suggested.

Integrates domain event library to a Symfony application

v0.1.1 2016-03-12 08:08 UTC

This package is auto-updated.

Last update: 2020-06-07 07:56:55 UTC


README

stability-deprecated

Build Status Scrutinizer Code Quality Code Coverage

Latest Stable Version Total Downloads Latest Unstable Version License

Integrates domain event library to a Symfony application

Installation

With composer :

composer require gbprod/domain-event-bundle

Update your app/AppKernel.php file:

public function registerBundles()
{
    $bundles = array(
        new GBProd\DomainEventBundle\DomainEventBundle(),
    );
}

Setup your entity

Use domain event library to raise events :

<?php

namespace GBProd\Acme\Entity;

use GBProd\DomainEvent\EventProvider;
use GBProd\DomainEvent\EventProviderTrait;

final class MyEntity implements EventProvider
{
    use EventProviderTrait;

    public function doSomething()
    {
        $this->raise(
            new SomethingHappenedEvent($this->id)
        );
    }
}

Dispatch events from your repository

Example with Doctrine repository :

<?php

namespace GBProd\AcmeBundle\Repository;

use GBProd\DomainEvent\EventProvider;
use GBProd\DomainEvent\Dispatcher;

class MyEntityRepository
{
    public function __construct(EntityManager $em, DomainEventDispatcher $dispatcher)
    {
        $this->em         = $em;
        $this->dispatcher = $dispatcher;
    }
    
    public function save(MyEntity $entity)
    {
        $this->em->persist($entity);
        $this->em->flush();
        
        $this->dispatcher->dispatch($entity);
    }
}
# src/GBProd/AcmeBundle/Resourses/config/services.yml
services:
    gbprod_acme.my_entity_repository:
        class: GBProd\AcmeBundle\Repository\MyEntityRepository
        arguments:
            - "@doctrine.entity_manager"
            - "@gbprod.domain_event_dispatcher"

This will dispatch events using Symfony Event dispatcher. The name of the event will be the classname of the aggregate and the event (MyEntity.SomethingHappenedEvent in this example).

Create your listener

<?php

namespace GBProd\AcmeBundle\Listener;

use Symfony\Component\EventDispatcher\Event;

class MyListener
{
    public function onSomethingHappened(Event $event) 
    {
        $domainEvent = $event->getDomainEvent();
        
        // Use it now
    }
}

Register your listener

gbprod_acme.event_listener.my_listener:
    class: GBProd\AcmeBundle\Listener\MyListener
    tags:
        - { name: kernel.event_listener, event: MyEntity.SomethingHappenedEvent, method: 'onSomethingHappened' }