Skip to content

Commit

Permalink
Add support for defining message decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
simensen committed Jul 8, 2023
1 parent 3e91ad2 commit 2026f24
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
Expand Up @@ -4,11 +4,13 @@

namespace Dflydev\EventSauce\SupportForLaravel\Container;

use Dflydev\EventSauce\Support\MessagePreparation\DefaultMessagePreparation;
use Dflydev\EventSauce\Support\Transaction\Transaction;
use Dflydev\EventSauce\SupportForLaravel\AggregateRoot\EloquentAggregateRoot;
use Dflydev\EventSauce\SupportForLaravel\AggregateRoot\EloquentAggregateRootRepository;
use Dflydev\EventSauce\SupportForLaravel\EventSauceConfiguration;
use EventSauce\EventSourcing\AggregateRootRepository;
use EventSauce\EventSourcing\MessageDecorator;
use EventSauce\EventSourcing\MessageDispatcher;
use EventSauce\EventSourcing\MessageRepository;
use EventSauce\EventSourcing\SynchronousMessageDispatcher;
Expand All @@ -27,6 +29,11 @@ final class EloquentAggregateRootRepositoryRegistrationBuilder
*/
private OutboxRepository|string|null $outboxRepository;

/**
* @var MessageDecorator|class-string<MessageDecorator>|null
*/
private MessageDecorator|string|null $messageDecorator;

/**
* @var MessageDispatcher|class-string<MessageDispatcher>|null
*/
Expand All @@ -40,6 +47,7 @@ final class EloquentAggregateRootRepositoryRegistrationBuilder
private bool $withoutOutboxRepository = false;
private bool $withoutTransactionalMessageDispatcher = false;
private bool $withoutSynchronousMessageDispatcher = false;
private bool $withoutMessageDecorator = false;

private function __construct()
{
Expand Down Expand Up @@ -72,6 +80,17 @@ public function withOutboxRepository(null|string|OutboxRepository $outboxReposit
return $instance;
}

/**
* @param class-string<MessageDecorator>|MessageDecorator|null $messageDecorator
*/
public function withMessageDecorator(null|string|MessageDecorator $messageDecorator = null): self
{
$instance = clone $this;
$instance->messageDecorator = $messageDecorator ?? MessageDecorator::class;

return $instance;
}

/**
* @param class-string<MessageDispatcher>|MessageDispatcher|null $transactionalMessageDispatcher
*/
Expand Down Expand Up @@ -110,6 +129,14 @@ public function withoutOutboxRepository(): self
return $instance;
}

public function withoutMessageDecorator(): self
{
$instance = clone $this;
$instance->withoutMessageDecorator = true;

return $instance;
}

public function withoutTransactionalMessageDispatcher(): self
{
$instance = clone $this;
Expand Down Expand Up @@ -168,7 +195,7 @@ public function build(
if (isset($this->outboxRepository)) {
$args['outboxRepository'] = is_object($this->outboxRepository) ? $this->outboxRepository : $app->get($this->outboxRepository);
} elseif ($app->bound(OutboxRepository::class)) {
$args['transactionalMessageDispatcher'] = $app->get(OutboxRepository::class);
$args['outboxRepository'] = $app->get(OutboxRepository::class);
}
}

Expand All @@ -188,6 +215,21 @@ public function build(
}
}

/** @var array{'messageDecorator'?: MessageDecorator|null} $messagePreparationArgs */
$messagePreparationArgs = [];

if (!$this->withoutMessageDecorator) {
if (isset($this->messageDecorator)) {
$messagePreparationArgs['messageDecorator'] = is_object($this->messageDecorator) ? $this->messageDecorator : $app->get($this->messageDecorator);
} elseif ($app->bound(MessageDecorator::class)) {
$messagePreparationArgs['messageDecorator'] = $app->get(MessageDecorator::class);
}
}

if (count($messagePreparationArgs)) {
$args['messagePreparation'] = new DefaultMessagePreparation(...$messagePreparationArgs);
}

return new EloquentAggregateRootRepository(...$args);
});
}
Expand Down
Expand Up @@ -6,9 +6,11 @@

use Dflydev\EventSauce\Support\AggregateRoot\EventSourcedAggregateRoot;
use Dflydev\EventSauce\Support\AggregateRoot\EventSourcedAggregateRootRepository;
use Dflydev\EventSauce\Support\MessagePreparation\DefaultMessagePreparation;
use Dflydev\EventSauce\Support\Transaction\Transaction;
use Dflydev\EventSauce\SupportForLaravel\EventSauceConfiguration;
use EventSauce\EventSourcing\AggregateRootRepository;
use EventSauce\EventSourcing\MessageDecorator;
use EventSauce\EventSourcing\MessageDispatcher;
use EventSauce\EventSourcing\MessageRepository;
use EventSauce\EventSourcing\SynchronousMessageDispatcher;
Expand All @@ -27,6 +29,11 @@ final class EventSourcedAggregateRootRepositoryRegistrationBuilder
*/
private OutboxRepository|string|null $outboxRepository;

/**
* @var MessageDecorator|class-string<MessageDecorator>|null
*/
private MessageDecorator|string|null $messageDecorator;

/**
* @var MessageDispatcher|class-string<MessageDispatcher>|null
*/
Expand All @@ -39,6 +46,7 @@ final class EventSourcedAggregateRootRepositoryRegistrationBuilder
private bool $withoutOutboxRepository = false;
private bool $withoutTransactionalMessageDispatcher = false;
private bool $withoutSynchronousMessageDispatcher = false;
private bool $withoutMessageDecorator = false;

private function __construct()
{
Expand Down Expand Up @@ -71,6 +79,17 @@ public function withOutboxRepository(null|string|OutboxRepository $outboxReposit
return $instance;
}

/**
* @param class-string<MessageDecorator>|MessageDecorator|null $messageDecorator
*/
public function withMessageDecorator(null|string|MessageDecorator $messageDecorator = null): self
{
$instance = clone $this;
$instance->messageDecorator = $messageDecorator ?? MessageDecorator::class;

return $instance;
}

/**
* @param class-string<MessageDispatcher>|MessageDispatcher|null $transactionalMessageDispatcher
*/
Expand Down Expand Up @@ -101,6 +120,14 @@ public function withoutOutboxRepository(): self
return $instance;
}

public function withoutMessageDecorator(): self
{
$instance = clone $this;
$instance->withoutMessageDecorator = true;

return $instance;
}

public function withoutTransactionalMessageDispatcher(): self
{
$instance = clone $this;
Expand Down Expand Up @@ -162,7 +189,7 @@ public function build(
if (isset($this->outboxRepository)) {
$args['outboxRepository'] = is_object($this->outboxRepository) ? $this->outboxRepository : $app->get($this->outboxRepository);
} elseif ($app->bound(OutboxRepository::class)) {
$args['transactionalMessageDispatcher'] = $app->get(OutboxRepository::class);
$args['outboxRepository'] = $app->get(OutboxRepository::class);
}
}

Expand All @@ -182,6 +209,21 @@ public function build(
}
}

/** @var array{'messageDecorator'?: MessageDecorator|null} $messagePreparationArgs */
$messagePreparationArgs = [];

if (!$this->withoutMessageDecorator) {
if (isset($this->messageDecorator)) {
$messagePreparationArgs['messageDecorator'] = is_object($this->messageDecorator) ? $this->messageDecorator : $app->get($this->messageDecorator);
} elseif ($app->bound(MessageDecorator::class)) {
$messagePreparationArgs['messageDecorator'] = $app->get(MessageDecorator::class);
}
}

if (count($messagePreparationArgs)) {
$args['messagePreparation'] = new DefaultMessagePreparation(...$messagePreparationArgs);
}

return new EventSourcedAggregateRootRepository(...$args);
});
}
Expand Down
14 changes: 13 additions & 1 deletion src/EventSauceConfiguration.php
Expand Up @@ -36,6 +36,18 @@

final class EventSauceConfiguration
{
public static function registerMessageDecorator(string|array $messageConsumerClassName, ?Application $application = null): void
{
$application = $application ?? app();

$messageConsumerClassNames = is_array($messageConsumerClassName) ? $messageConsumerClassName : [$messageConsumerClassName];

foreach ($messageConsumerClassNames as $messageConsumerClassName) {
$application->singleton($messageConsumerClassName);
$application->tag($messageConsumerClassName, MessageDecorator::class);
}
}

/**
* @param class-string<AggregateRoot> $aggregateRootClassName
*/
Expand Down Expand Up @@ -333,7 +345,7 @@ public static function registerPayloadSerializer(?string $class = null, ?Applica
$application->singleton(PayloadSerializer::class, $class);
}

public static function registerMessageDecorator(?Application $application = null): void
public static function registerDefaultMessageDecorator(?Application $application = null): void
{
$application = $application ?? app();

Expand Down

0 comments on commit 2026f24

Please sign in to comment.