Skip to content

Commit

Permalink
Merge pull request #29 from biig-io/feature/events-in-domainevents
Browse files Browse the repository at this point in the history
Support for embedded events in DomainEvents
  • Loading branch information
Awkan authored Mar 27, 2018
2 parents 90fd696 + 82aac3e commit fbcc893
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added

- Support for embbeded events in the `DomainEvent`

## [1.3.0] - 2018-03-14
### Added
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Domain Events:
* [Domain event dispatcher](docs/domain_event_dispatcher.md)
* [Injection of the dispatcher in Doctrine entities](docs/injection_in_doctrine_entities.md)
* [Symfony serializer integration](docs/symfony_serializer_integration.md)
* [Learn how do more with our cookbooks](docs/cookbooks.md)


Installation
Expand Down
8 changes: 8 additions & 0 deletions Tests/Event/DomainEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Biig\Component\Domain\Event\DomainEvent;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\GenericEvent;

class DomainEventTest extends TestCase
{
Expand All @@ -13,4 +14,11 @@ public function testItIsInstanceOfSymfonyEvent()
$event = new DomainEvent();
$this->assertInstanceOf(Event::class, $event);
}

public function testItAcceptAnEventAsParameter()
{
$event = new DomainEvent(null, [], new GenericEvent());

$this->assertInstanceOf(GenericEvent::class, $event->getOriginalEvent());
}
}
32 changes: 32 additions & 0 deletions docs/cookbooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Cookbooks
=========

This section is about some things you can do while using this component.

1. [Use events from another component in the domain dispatcher](#support_other_events)


Support other events
--------------------

In some special cases you may have to handle some other events. For example
the workflow of Symfony dispatch events specific to this composant. You can
transform these events by redefining the dispatcher and transform the event:

```php
<?php

class WorkflowDomainEventDispatcher extends DomainEventDispatcher
{
public function dispatch($eventName, Event $event = null)
{
if ($event instanceof \Symfony\Component\Workflow\Event\Event) {
$event = new DomainEvent($event->getSubject(), [], $event);
}

return parent::dispatch($eventName, $event);
}
}
```

You can do this because the domain event support embedded events. 👍
19 changes: 19 additions & 0 deletions src/Event/DomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,27 @@

namespace Biig\Component\Domain\Event;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\GenericEvent;

class DomainEvent extends GenericEvent
{
/**
* @var Event
*/
private $originalEvent;

public function __construct($subject = null, $arguments = [], Event $originalEvent = null)
{
parent::__construct($subject, $arguments);
$this->originalEvent = $originalEvent;
}

/**
* @return Event
*/
public function getOriginalEvent(): ?Event
{
return $this->originalEvent;
}
}

0 comments on commit fbcc893

Please sign in to comment.