Skip to content

Commit

Permalink
Merge pull request #445 from patchlevel/use-variadic-in-pipeline
Browse files Browse the repository at this point in the history
use variadic syntax in pipeline to allow bulk save
  • Loading branch information
DavidBadura authored Dec 29, 2023
2 parents 7fecaa1 + de6af6d commit b38fc14
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Pipeline/Middleware/ExcludeEventMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class ExcludeEventMiddleware implements Middleware
{
/** @param list<class-string> $classes */
public function __construct(
private array $classes,
private readonly array $classes,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Pipeline/Middleware/IncludeEventMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class IncludeEventMiddleware implements Middleware
{
/** @param list<class-string> $classes */
public function __construct(
private array $classes,
private readonly array $classes,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Pipeline/Middleware/ReplaceEventMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Pipeline/Middleware/UntilEventMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
final class UntilEventMiddleware implements Middleware
{
public function __construct(
private DateTimeImmutable $until,
private readonly DateTimeImmutable $until,
) {
}

Expand Down
9 changes: 3 additions & 6 deletions src/Pipeline/Pipeline.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ final class Pipeline

/** @param list<Middleware> $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);
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Pipeline/Source/StoreSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
}

Expand Down
6 changes: 4 additions & 2 deletions src/Pipeline/Target/InMemoryTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ final class InMemoryTarget implements Target
/** @var list<Message> */
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<Message> */
Expand Down
9 changes: 6 additions & 3 deletions src/Pipeline/Target/ProjectorRepositoryTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
9 changes: 6 additions & 3 deletions src/Pipeline/Target/ProjectorTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions src/Pipeline/Target/StoreTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/Pipeline/Target/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface Target
{
public function save(Message $message): void;
public function save(Message ...$messages): void;
}

0 comments on commit b38fc14

Please sign in to comment.