-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasDomainEvents.php
More file actions
48 lines (42 loc) · 1.02 KB
/
HasDomainEvents.php
File metadata and controls
48 lines (42 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
namespace ComplexHeart\Domain\Model\Traits;
use ComplexHeart\Domain\Contracts\Events\Event;
use ComplexHeart\Domain\Contracts\Events\EventBus;
/**
* Trait HasDomainEvents
*
* @see https://martinfowler.com/eaaDev/DomainEvent.html
*
* @author Unay Santisteban <usantisteban@othercode.io>
* @package ComplexHeart\Domain\Model\Traits
*/
trait HasDomainEvents
{
/**
* List of registered domain events.
*
* @var Event[]
*/
private array $_domainEvents = [];
/**
* Publish the registered Domain Events.
*
* @param EventBus $eventBus
* @return void
*/
final public function publishDomainEvents(EventBus $eventBus): void
{
$eventBus->publish(...$this->_domainEvents);
$this->_domainEvents = [];
}
/**
* Register a new DomainEvent.
*
* @param Event $domainEvent
*/
final protected function registerDomainEvent(Event $domainEvent): void
{
$this->_domainEvents[] = $domainEvent;
}
}