From de6af6d5e031be55a6d45be76ef0cfa3b8ce48ff Mon Sep 17 00:00:00 2001 From: David Badura Date: Sat, 23 Dec 2023 13:35:00 +0100 Subject: [PATCH] use variadic syntax in pipeline to allow bulk save --- src/Pipeline/Middleware/ExcludeEventMiddleware.php | 2 +- src/Pipeline/Middleware/IncludeEventMiddleware.php | 2 +- src/Pipeline/Middleware/ReplaceEventMiddleware.php | 2 +- src/Pipeline/Middleware/UntilEventMiddleware.php | 2 +- src/Pipeline/Pipeline.php | 9 +++------ src/Pipeline/Source/StoreSource.php | 4 ++-- src/Pipeline/Target/InMemoryTarget.php | 6 ++++-- src/Pipeline/Target/ProjectorRepositoryTarget.php | 9 ++++++--- src/Pipeline/Target/ProjectorTarget.php | 9 ++++++--- src/Pipeline/Target/StoreTarget.php | 6 +++--- src/Pipeline/Target/Target.php | 2 +- 11 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/Pipeline/Middleware/ExcludeEventMiddleware.php b/src/Pipeline/Middleware/ExcludeEventMiddleware.php index ed544ab16..04d71429c 100644 --- a/src/Pipeline/Middleware/ExcludeEventMiddleware.php +++ b/src/Pipeline/Middleware/ExcludeEventMiddleware.php @@ -10,7 +10,7 @@ final class ExcludeEventMiddleware implements Middleware { /** @param list $classes */ public function __construct( - private array $classes, + private readonly array $classes, ) { } diff --git a/src/Pipeline/Middleware/IncludeEventMiddleware.php b/src/Pipeline/Middleware/IncludeEventMiddleware.php index 11ab92402..703fd5ee5 100644 --- a/src/Pipeline/Middleware/IncludeEventMiddleware.php +++ b/src/Pipeline/Middleware/IncludeEventMiddleware.php @@ -10,7 +10,7 @@ final class IncludeEventMiddleware implements Middleware { /** @param list $classes */ public function __construct( - private array $classes, + private readonly array $classes, ) { } diff --git a/src/Pipeline/Middleware/ReplaceEventMiddleware.php b/src/Pipeline/Middleware/ReplaceEventMiddleware.php index 32452d802..4d9d2878c 100644 --- a/src/Pipeline/Middleware/ReplaceEventMiddleware.php +++ b/src/Pipeline/Middleware/ReplaceEventMiddleware.php @@ -17,7 +17,7 @@ final class ReplaceEventMiddleware implements Middleware * @param callable(T $event):object $callable */ public function __construct( - private string $class, + private readonly string $class, callable $callable, ) { $this->callable = $callable; diff --git a/src/Pipeline/Middleware/UntilEventMiddleware.php b/src/Pipeline/Middleware/UntilEventMiddleware.php index da3e0bc7b..bc76ac3ea 100644 --- a/src/Pipeline/Middleware/UntilEventMiddleware.php +++ b/src/Pipeline/Middleware/UntilEventMiddleware.php @@ -10,7 +10,7 @@ final class UntilEventMiddleware implements Middleware { public function __construct( - private DateTimeImmutable $until, + private readonly DateTimeImmutable $until, ) { } diff --git a/src/Pipeline/Pipeline.php b/src/Pipeline/Pipeline.php index 4d40574d5..cecab3974 100644 --- a/src/Pipeline/Pipeline.php +++ b/src/Pipeline/Pipeline.php @@ -16,8 +16,8 @@ final class Pipeline /** @param list $middlewares */ public function __construct( - private Source $source, - private Target $target, + private readonly Source $source, + private readonly Target $target, array $middlewares = [], ) { $this->middlewares = new ChainMiddleware($middlewares); @@ -27,10 +27,7 @@ public function run(Closure|null $observer = null): void { foreach ($this->source->load() as $message) { $result = ($this->middlewares)($message); - - foreach ($result as $resultMessage) { - $this->target->save($resultMessage); - } + $this->target->save(...$result); if (!$observer) { continue; diff --git a/src/Pipeline/Source/StoreSource.php b/src/Pipeline/Source/StoreSource.php index fa8187716..fd284dbaa 100644 --- a/src/Pipeline/Source/StoreSource.php +++ b/src/Pipeline/Source/StoreSource.php @@ -13,8 +13,8 @@ final class StoreSource implements Source { public function __construct( - private Store $store, - private int $fromIndex = 0, + private readonly Store $store, + private readonly int $fromIndex = 0, ) { } diff --git a/src/Pipeline/Target/InMemoryTarget.php b/src/Pipeline/Target/InMemoryTarget.php index b64d7c723..d8bc36feb 100644 --- a/src/Pipeline/Target/InMemoryTarget.php +++ b/src/Pipeline/Target/InMemoryTarget.php @@ -11,9 +11,11 @@ final class InMemoryTarget implements Target /** @var list */ private array $messages = []; - public function save(Message $message): void + public function save(Message ...$messages): void { - $this->messages[] = $message; + foreach ($messages as $message) { + $this->messages[] = $message; + } } /** @return list */ diff --git a/src/Pipeline/Target/ProjectorRepositoryTarget.php b/src/Pipeline/Target/ProjectorRepositoryTarget.php index 412c41e70..db8f81328 100644 --- a/src/Pipeline/Target/ProjectorRepositoryTarget.php +++ b/src/Pipeline/Target/ProjectorRepositoryTarget.php @@ -18,9 +18,12 @@ public function __construct( ) { } - public function save(Message $message): void + public function save(Message ...$messages): void { - (new ProjectorHelper($this->projectorResolver)) - ->handleMessage($message, ...$this->projectorRepository->projectors()); + $helper = new ProjectorHelper($this->projectorResolver); + + foreach ($messages as $message) { + $helper->handleMessage($message, ...$this->projectorRepository->projectors()); + } } } diff --git a/src/Pipeline/Target/ProjectorTarget.php b/src/Pipeline/Target/ProjectorTarget.php index 2f9aa9304..bdb02b909 100644 --- a/src/Pipeline/Target/ProjectorTarget.php +++ b/src/Pipeline/Target/ProjectorTarget.php @@ -18,9 +18,12 @@ public function __construct( ) { } - public function save(Message $message): void + public function save(Message ...$messages): void { - (new ProjectorHelper($this->projectorResolver)) - ->handleMessage($message, $this->projector); + $helper = new ProjectorHelper($this->projectorResolver); + + foreach ($messages as $message) { + $helper->handleMessage($message, $this->projector); + } } } diff --git a/src/Pipeline/Target/StoreTarget.php b/src/Pipeline/Target/StoreTarget.php index c643683eb..c2041bfa9 100644 --- a/src/Pipeline/Target/StoreTarget.php +++ b/src/Pipeline/Target/StoreTarget.php @@ -10,12 +10,12 @@ final class StoreTarget implements Target { public function __construct( - private Store $store, + private readonly Store $store, ) { } - public function save(Message $message): void + public function save(Message ...$messages): void { - $this->store->save($message); + $this->store->save(...$messages); } } diff --git a/src/Pipeline/Target/Target.php b/src/Pipeline/Target/Target.php index f157a4b4b..e3f1f5749 100644 --- a/src/Pipeline/Target/Target.php +++ b/src/Pipeline/Target/Target.php @@ -8,5 +8,5 @@ interface Target { - public function save(Message $message): void; + public function save(Message ...$messages): void; }