From a3ebadfafddd05a6d93746799f49c99064a498fd Mon Sep 17 00:00:00 2001 From: Christopher Gammie Date: Wed, 22 Nov 2023 19:21:47 +0000 Subject: [PATCH] feat!: improve constructors on dispatchers by allowing pipe container ...instead of having to always provide a pipeline builder factory. --- CHANGELOG.md | 7 +++++++ src/Bus/CommandDispatcher.php | 11 +++++++++-- src/Bus/QueryDispatcher.php | 13 ++++++++++--- src/Bus/Validation/AbstractValidator.php | 2 +- .../DomainEventDispatching/Dispatcher.php | 11 +++++++++-- src/Infrastructure/Queue/Queue.php | 11 +++++++++-- src/Toolkit/Pipeline/PipelineBuilderFactory.php | 12 ++++++------ 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a2d4f8..90fa1bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,12 @@ All notable changes to this project will be documented in this file. This projec ### Added - The command and query validators now allow rules to return `null` to indicate no errors. +- The following dispatchers now accept pipeline builder factories or pipeline containers into their constructor. This + simplifies creating them, as in most cases a pipeline container can be provided from a dependency helper. + - `CommandDispatcher` + - `QueryDispatcher` + - `DomainEventDispatching\Dispatcher` + - `Queue` ### Changed @@ -19,6 +25,7 @@ All notable changes to this project will be documented in this file. This projec from multiple properties of the error object. - BREAKING: The code on an error object is now type-hinted as a `BackedEnum` or `null` - previously it was `mixed`. Error codes should be from a defined list, therefore an enum is the correctly defined type. +- BREAKING: The `PipelineBuilderFactory::cast()` method has been renamed `make()`. ### Removed diff --git a/src/Bus/CommandDispatcher.php b/src/Bus/CommandDispatcher.php index 3f5c4d4..8a7b1a5 100644 --- a/src/Bus/CommandDispatcher.php +++ b/src/Bus/CommandDispatcher.php @@ -21,11 +21,17 @@ use CloudCreativity\Modules\Bus\Results\ResultInterface; use CloudCreativity\Modules\Toolkit\Pipeline\MiddlewareProcessor; +use CloudCreativity\Modules\Toolkit\Pipeline\PipeContainerInterface; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactory; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactoryInterface; class CommandDispatcher implements CommandDispatcherInterface { + /** + * @var PipelineBuilderFactoryInterface + */ + private readonly PipelineBuilderFactoryInterface $pipelineFactory; + /** * @var array */ @@ -35,12 +41,13 @@ class CommandDispatcher implements CommandDispatcherInterface * CommandDispatcher constructor. * * @param CommandHandlerContainerInterface $handlers - * @param PipelineBuilderFactoryInterface $pipelineFactory + * @param PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory */ public function __construct( private readonly CommandHandlerContainerInterface $handlers, - private readonly PipelineBuilderFactoryInterface $pipelineFactory = new PipelineBuilderFactory(), + PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory = null, ) { + $this->pipelineFactory = PipelineBuilderFactory::make($pipelineFactory); } /** diff --git a/src/Bus/QueryDispatcher.php b/src/Bus/QueryDispatcher.php index abfb21e..d4152ba 100644 --- a/src/Bus/QueryDispatcher.php +++ b/src/Bus/QueryDispatcher.php @@ -21,26 +21,33 @@ use CloudCreativity\Modules\Bus\Results\ResultInterface; use CloudCreativity\Modules\Toolkit\Pipeline\MiddlewareProcessor; +use CloudCreativity\Modules\Toolkit\Pipeline\PipeContainerInterface; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactory; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactoryInterface; class QueryDispatcher implements QueryDispatcherInterface { + /** + * @var PipelineBuilderFactoryInterface + */ + private readonly PipelineBuilderFactoryInterface $pipelineFactory; + /** * @var array */ private array $pipes = []; /** - * CommandDispatcher constructor. + * QueryDispatcher constructor. * * @param QueryHandlerContainerInterface $handlers - * @param PipelineBuilderFactoryInterface $pipelineFactory + * @param PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory */ public function __construct( private readonly QueryHandlerContainerInterface $handlers, - private readonly PipelineBuilderFactoryInterface $pipelineFactory = new PipelineBuilderFactory(), + PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory = null, ) { + $this->pipelineFactory = PipelineBuilderFactory::make($pipelineFactory); } /** diff --git a/src/Bus/Validation/AbstractValidator.php b/src/Bus/Validation/AbstractValidator.php index bac6b13..28499a2 100644 --- a/src/Bus/Validation/AbstractValidator.php +++ b/src/Bus/Validation/AbstractValidator.php @@ -47,7 +47,7 @@ abstract class AbstractValidator implements ValidatorInterface public function __construct( PipelineBuilderFactoryInterface|PipeContainerInterface $pipelineFactory = new PipelineBuilderFactory(), ) { - $this->pipelineFactory = PipelineBuilderFactory::cast($pipelineFactory); + $this->pipelineFactory = PipelineBuilderFactory::make($pipelineFactory); } /** diff --git a/src/Infrastructure/DomainEventDispatching/Dispatcher.php b/src/Infrastructure/DomainEventDispatching/Dispatcher.php index 932c185..3fa4fea 100644 --- a/src/Infrastructure/DomainEventDispatching/Dispatcher.php +++ b/src/Infrastructure/DomainEventDispatching/Dispatcher.php @@ -24,6 +24,7 @@ use CloudCreativity\Modules\Domain\Events\OccursImmediately; use CloudCreativity\Modules\Infrastructure\Persistence\UnitOfWorkManagerInterface; use CloudCreativity\Modules\Toolkit\Pipeline\MiddlewareProcessor; +use CloudCreativity\Modules\Toolkit\Pipeline\PipeContainerInterface; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactory; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactoryInterface; use Generator; @@ -31,6 +32,11 @@ class Dispatcher implements DispatcherInterface { + /** + * @var PipelineBuilderFactoryInterface + */ + private readonly PipelineBuilderFactoryInterface $pipelineFactory; + /** * @var array> */ @@ -46,13 +52,14 @@ class Dispatcher implements DispatcherInterface * * @param ListenerContainerInterface $listeners * @param UnitOfWorkManagerInterface $unitOfWorkManager - * @param PipelineBuilderFactoryInterface $pipelineFactory + * @param PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory */ public function __construct( private readonly ListenerContainerInterface $listeners, private readonly UnitOfWorkManagerInterface $unitOfWorkManager, - private readonly PipelineBuilderFactoryInterface $pipelineFactory = new PipelineBuilderFactory(), + PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory = null, ) { + $this->pipelineFactory = PipelineBuilderFactory::make($pipelineFactory); } /** diff --git a/src/Infrastructure/Queue/Queue.php b/src/Infrastructure/Queue/Queue.php index 4396ce6..ba99bc2 100644 --- a/src/Infrastructure/Queue/Queue.php +++ b/src/Infrastructure/Queue/Queue.php @@ -20,11 +20,17 @@ namespace CloudCreativity\Modules\Infrastructure\Queue; use CloudCreativity\Modules\Toolkit\Pipeline\MiddlewareProcessor; +use CloudCreativity\Modules\Toolkit\Pipeline\PipeContainerInterface; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactory; use CloudCreativity\Modules\Toolkit\Pipeline\PipelineBuilderFactoryInterface; class Queue implements QueueInterface { + /** + * @var PipelineBuilderFactoryInterface + */ + private readonly PipelineBuilderFactoryInterface $pipelineFactory; + /** * @var array */ @@ -34,12 +40,13 @@ class Queue implements QueueInterface * Queue constructor. * * @param QueueHandlerContainerInterface $handlers - * @param PipelineBuilderFactoryInterface $pipelineFactory + * @param PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory */ public function __construct( private readonly QueueHandlerContainerInterface $handlers, - private readonly PipelineBuilderFactoryInterface $pipelineFactory = new PipelineBuilderFactory(), + PipelineBuilderFactoryInterface|PipeContainerInterface|null $pipelineFactory = null, ) { + $this->pipelineFactory = PipelineBuilderFactory::make($pipelineFactory); } /** diff --git a/src/Toolkit/Pipeline/PipelineBuilderFactory.php b/src/Toolkit/Pipeline/PipelineBuilderFactory.php index 50b76c2..af456ea 100644 --- a/src/Toolkit/Pipeline/PipelineBuilderFactory.php +++ b/src/Toolkit/Pipeline/PipelineBuilderFactory.php @@ -22,17 +22,17 @@ final class PipelineBuilderFactory implements PipelineBuilderFactoryInterface { /** - * @param PipelineBuilderFactoryInterface|PipeContainerInterface $factoryOrContainer + * @param PipelineBuilderFactoryInterface|PipeContainerInterface|null $factoryOrContainer * @return PipelineBuilderFactoryInterface */ - public static function cast( - PipelineBuilderFactoryInterface|PipeContainerInterface $factoryOrContainer, + public static function make( + PipelineBuilderFactoryInterface|PipeContainerInterface|null $factoryOrContainer, ): PipelineBuilderFactoryInterface { - if ($factoryOrContainer instanceof PipeContainerInterface) { - return new self($factoryOrContainer); + if ($factoryOrContainer instanceof PipelineBuilderFactoryInterface) { + return $factoryOrContainer; } - return $factoryOrContainer; + return new self($factoryOrContainer); } /**