diff --git a/docs/pages/pipeline.md b/docs/pages/pipeline.md index 96af29bd4..c7a0e0ff3 100644 --- a/docs/pages/pipeline.md +++ b/docs/pages/pipeline.md @@ -40,11 +40,11 @@ The pipeline can also be used to create or rebuild a projection: ```php use Patchlevel\EventSourcing\Pipeline\Pipeline; use Patchlevel\EventSourcing\Pipeline\Source\StoreSource; -use Patchlevel\EventSourcing\Pipeline\Target\ProjectionTarget; +use Patchlevel\EventSourcing\Pipeline\Target\ConsumerTarget; $pipeline = new Pipeline( new StoreSource($store), - new ProjectionTarget($projection) + ConsumerTarget::create([$projection]), ); ``` @@ -140,38 +140,23 @@ $target = new StoreTarget($store); It does not matter whether the previous store was a SingleTable or a MultiTable. You can switch back and forth between both store types using the pipeline. -### Projector +### Consumer -A projector can also be used as a target. -For example, to set up a new projection or to build a new projection. +A consumer can also be used as a target. ```php -use Patchlevel\EventSourcing\Pipeline\Target\ProjectorTarget; +use Patchlevel\EventSourcing\Pipeline\Target\ConsumerTarget; -$target = new ProjectorTarget($projector); +$target = new ConsumerTarget($consumer); ``` -!!! warning - - This is only recommended for exceptional cases. - By default, projections should only be created using the projectionist. - -### Projector Repository +!!! tip -If you want to build or create all projections from scratch, -then you can also use the ProjectorRepositoryTarget. -In this, the individual projectors are iterated and the events are then passed on. - -```php -use Patchlevel\EventSourcing\Pipeline\Target\ProjectorRepositoryTarget; + You can also use it to build a new projection from scratch. -$target = new ProjectorRepositoryTarget($projectorRepository); -``` - -!!! warning +!!! note - This is only recommended for exceptional cases. - By default, projections should only be created using the projectionist. + More about the consumer can be found [here](event_bus.md). ### In Memory diff --git a/src/Pipeline/Target/ConsumerTarget.php b/src/Pipeline/Target/ConsumerTarget.php new file mode 100644 index 000000000..9b01ddea2 --- /dev/null +++ b/src/Pipeline/Target/ConsumerTarget.php @@ -0,0 +1,30 @@ +consumer->consume($message); + } + } + + /** @param iterable $listeners */ + public static function create(iterable $listeners): self + { + return new self(DefaultConsumer::create($listeners)); + } +} diff --git a/src/Pipeline/Target/ProjectorRepositoryTarget.php b/src/Pipeline/Target/ProjectorRepositoryTarget.php deleted file mode 100644 index ebc2ed60d..000000000 --- a/src/Pipeline/Target/ProjectorRepositoryTarget.php +++ /dev/null @@ -1,34 +0,0 @@ -projectorRepository->projectors(); - - foreach ($messages as $message) { - foreach ($projectors as $projector) { - $subscribeMethods = $this->projectorResolver->resolveSubscribeMethods($projector, $message); - - foreach ($subscribeMethods as $subscribeMethod) { - $subscribeMethod($message); - } - } - } - } -} diff --git a/src/Pipeline/Target/ProjectorTarget.php b/src/Pipeline/Target/ProjectorTarget.php deleted file mode 100644 index 49ce59274..000000000 --- a/src/Pipeline/Target/ProjectorTarget.php +++ /dev/null @@ -1,29 +0,0 @@ -projectorResolver->resolveSubscribeMethods($this->projector, $message); - - foreach ($subscribeMethods as $subscribeMethod) { - $subscribeMethod($message); - } - } - } -} diff --git a/tests/Unit/Pipeline/Target/ConsumerTargetTest.php b/tests/Unit/Pipeline/Target/ConsumerTargetTest.php new file mode 100644 index 000000000..4cee25516 --- /dev/null +++ b/tests/Unit/Pipeline/Target/ConsumerTargetTest.php @@ -0,0 +1,33 @@ +prophesize(Consumer::class); + $consumer->consume($message)->shouldBeCalledOnce(); + + $consumerTarget = new ConsumerTarget($consumer->reveal()); + $consumerTarget->save($message); + } +} diff --git a/tests/Unit/Pipeline/Target/ProjectorRepositoryTargetTest.php b/tests/Unit/Pipeline/Target/ProjectorRepositoryTargetTest.php deleted file mode 100644 index 2adff0b75..000000000 --- a/tests/Unit/Pipeline/Target/ProjectorRepositoryTargetTest.php +++ /dev/null @@ -1,75 +0,0 @@ -message = $message; - } - }; - - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->shouldBeCalledOnce()->willReturn([$projector]); - - $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveSubscribeMethods($projector, $message)->shouldBeCalledOnce()->willReturn([$projector(...)]); - - $projectorRepositoryTarget = new ProjectorRepositoryTarget($projectorRepository->reveal(), $projectorResolver->reveal()); - $projectorRepositoryTarget->save($message); - - self::assertSame($message, $projector->message); - } - - public function testSaveNoHit(): void - { - $message = new Message( - new ProfileCreated(ProfileId::fromString('1'), Email::fromString('foo@test.com')), - ); - - $projector = new class { - public Message|null $message = null; - - public function __invoke(Message $message): void - { - $this->message = $message; - } - }; - - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->shouldBeCalledOnce()->willReturn([$projector]); - - $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveSubscribeMethods($projector, $message)->shouldBeCalledOnce()->willReturn([]); - - $projectorRepositoryTarget = new ProjectorRepositoryTarget($projectorRepository->reveal(), $projectorResolver->reveal()); - $projectorRepositoryTarget->save($message); - - self::assertNull($projector->message); - } -} diff --git a/tests/Unit/Pipeline/Target/ProjectorTargetTest.php b/tests/Unit/Pipeline/Target/ProjectorTargetTest.php deleted file mode 100644 index 7916b0343..000000000 --- a/tests/Unit/Pipeline/Target/ProjectorTargetTest.php +++ /dev/null @@ -1,68 +0,0 @@ -message = $message; - } - }; - - $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveSubscribeMethods($projector, $message)->shouldBeCalledOnce()->willReturn([$projector(...)]); - - $projectorTarget = new ProjectorTarget($projector, $projectorResolver->reveal()); - $projectorTarget->save($message); - - self::assertSame($message, $projector->message); - } - - public function testSaveNoHit(): void - { - $message = new Message( - new ProfileCreated(ProfileId::fromString('1'), Email::fromString('foo@test.com')), - ); - - $projector = new class { - public Message|null $message = null; - - public function __invoke(Message $message): void - { - $this->message = $message; - } - }; - - $projectorResolver = $this->prophesize(ProjectorResolver::class); - $projectorResolver->resolveSubscribeMethods($projector, $message)->shouldBeCalledOnce()->willReturn([]); - - $projectorTarget = new ProjectorTarget($projector, $projectorResolver->reveal()); - $projectorTarget->save($message); - - self::assertNull($projector->message); - } -}