-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, the application layer is responsible for sending events that are actually more related to the domain. For example, the `UpsertArticleHandler` publishes an event when an article is created. In this commit we move the publishing of domain events to the entity itself. By adding a doctrine listener we make sure the events are only emitted when the entity is committed to the database.
- Loading branch information
Showing
12 changed files
with
124 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Common\Domain; | ||
|
||
interface EventPublishingAggregateRoot | ||
{ | ||
public function recordEvent(object $event): void; | ||
|
||
public function shiftDomainEvent(): object; | ||
|
||
public function hasDomainEvents(): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App\Common\Domain; | ||
|
||
trait EventPublishingAggregateRootTrait | ||
{ | ||
/** | ||
* @var list<object> | ||
*/ | ||
private array $domainEvents = []; | ||
|
||
public function recordEvent(object $event): void | ||
{ | ||
$this->domainEvents[] = $event; | ||
} | ||
|
||
public function shiftDomainEvent(): object | ||
{ | ||
return array_shift($this->domainEvents) ?? throw new \LogicException('There are no domain events (left).'); | ||
} | ||
|
||
public function hasDomainEvents(): bool | ||
{ | ||
return $this->domainEvents !== []; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Common/Infrastructure/Persistence/Doctrine/EmitDomainEventsOnFlushListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Common\Infrastructure\Persistence\Doctrine; | ||
|
||
use App\Common\Domain\EventPublishingAggregateRoot; | ||
use App\Common\Infrastructure\Messenger\EventBus\EventBus; | ||
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; | ||
use Doctrine\ORM\Event\OnFlushEventArgs; | ||
use Doctrine\ORM\Events; | ||
|
||
#[AsEntityListener(event: Events::onFlush, method: 'onFlush')] | ||
final readonly class EmitDomainEventsOnFlushListener | ||
{ | ||
public function __construct(private EventBus $eventBus) | ||
{ | ||
} | ||
|
||
public function onFlush(OnFlushEventArgs $event): void | ||
{ | ||
$uow = $event->getObjectManager()->getUnitOfWork(); | ||
|
||
foreach ($uow->getScheduledEntityInsertions() as $entity) { | ||
$this->emitRecordedEvents($entity); | ||
} | ||
|
||
foreach ($uow->getScheduledEntityUpdates() as $entity) { | ||
$this->emitRecordedEvents($entity); | ||
} | ||
|
||
foreach ($uow->getScheduledEntityDeletions() as $entity) { | ||
$this->emitRecordedEvents($entity); | ||
} | ||
|
||
foreach ($uow->getScheduledCollectionDeletions() as $collection) { | ||
foreach ($collection as $entity) { | ||
$this->emitRecordedEvents($entity); | ||
} | ||
} | ||
|
||
foreach ($uow->getScheduledCollectionUpdates() as $collection) { | ||
foreach ($collection as $entity) { | ||
$this->emitRecordedEvents($entity); | ||
} | ||
} | ||
} | ||
|
||
public function emitRecordedEvents(object $entity): void | ||
{ | ||
if ($entity instanceof EventPublishingAggregateRoot === false) { | ||
return; | ||
} | ||
|
||
while ($entity->hasDomainEvents()) { | ||
$this->eventBus->dispatch($entity->shiftDomainEvent()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ation/Event/Article/ArticleAddedEvent.php → ...ticle/Event/Article/ArticleAddedEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ion/Event/Article/ArticleUpdatedEvent.php → ...cle/Event/Article/ArticleUpdatedEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters