Skip to content

Commit

Permalink
feat!: improve constructors on dispatchers by allowing pipe container
Browse files Browse the repository at this point in the history
...instead of having to always provide a pipeline builder factory.
  • Loading branch information
lindyhopchris committed Nov 22, 2023
1 parent 423e8ca commit a3ebadf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
11 changes: 9 additions & 2 deletions src/Bus/CommandDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|callable>
*/
Expand All @@ -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);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/Bus/QueryDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|callable>
*/
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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Bus/Validation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Infrastructure/DomainEventDispatching/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@
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;
use InvalidArgumentException;

class Dispatcher implements DispatcherInterface
{
/**
* @var PipelineBuilderFactoryInterface
*/
private readonly PipelineBuilderFactoryInterface $pipelineFactory;

/**
* @var array<string, array<string|callable>>
*/
Expand All @@ -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);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Infrastructure/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string|callable>
*/
Expand All @@ -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);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Toolkit/Pipeline/PipelineBuilderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit a3ebadf

Please sign in to comment.