Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Nov 7, 2024
1 parent 17921fe commit a53aed2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/Message/Pipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ final class Pipe implements IteratorAggregate
{
private Translator $translator;

/**
* @param iterable<Message> $messages
* @param list<Translator>|Translator $translators
*/
/** @param iterable<Message> $messages */
public function __construct(
private readonly iterable $messages,
array|Translator $translators = [],
Translator ...$translators,
) {
$this->translator = $translators instanceof Translator
? $translators
: new ChainTranslator($translators);
$this->translator = new ChainTranslator($translators);
}

/** @return Traversable<Message> */
Expand Down Expand Up @@ -56,7 +51,9 @@ public function toArray(): array
private function createGenerator(iterable $messages, Translator $translator): Generator
{
foreach ($messages as $message) {
yield from $translator($message);
foreach ($translator($message) as $translatedMessage) {
yield $translatedMessage;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function beginBatch(): void

public function commitBatch(): void
{
$pipeline = new Pipe($this->messages, $this->middlewares);
$pipeline = new Pipe($this->messages, ...$this->middlewares);
$this->messages = [];

$this->targetStore->save(...$pipeline);
Expand Down
6 changes: 2 additions & 4 deletions tests/Unit/Message/PipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,8 @@ public function testWithMiddlewares(): void

$stream = new Pipe(
$messages,
[
new ExcludeEventTranslator([ProfileCreated::class]),
new RecalculatePlayheadTranslator(),
],
new ExcludeEventTranslator([ProfileCreated::class]),
new RecalculatePlayheadTranslator(),
);

$resultMessages = iterator_to_array($stream);
Expand Down
16 changes: 15 additions & 1 deletion tests/Unit/Message/Translator/ChainTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ final class ChainTranslatorTest extends TestCase
{
use ProphecyTrait;

public function testEmptyChain(): void
{
$message = new Message(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
);

$translator = new ChainTranslator([]);

self::assertSame([$message], $translator($message));
}

public function testChain(): void
{
$message = new Message(
Expand All @@ -38,6 +52,6 @@ public function testChain(): void
$child2->reveal(),
]);

$translator($message);
self::assertSame([$message], $translator($message));
}
}

0 comments on commit a53aed2

Please sign in to comment.