Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add consumer target and replace old projector targets #499

Merged
merged 1 commit into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 10 additions & 25 deletions docs/pages/pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
);
```

Expand Down Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions src/Pipeline/Target/ConsumerTarget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Pipeline\Target;

use Patchlevel\EventSourcing\EventBus\Consumer;
use Patchlevel\EventSourcing\EventBus\DefaultConsumer;
use Patchlevel\EventSourcing\EventBus\Message;

final class ConsumerTarget implements Target
{
public function __construct(
private readonly Consumer $consumer,
) {
}

public function save(Message ...$messages): void
{
foreach ($messages as $message) {
$this->consumer->consume($message);
}
}

/** @param iterable<object> $listeners */
public static function create(iterable $listeners): self
{
return new self(DefaultConsumer::create($listeners));
}
}
34 changes: 0 additions & 34 deletions src/Pipeline/Target/ProjectorRepositoryTarget.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/Pipeline/Target/ProjectorTarget.php

This file was deleted.

33 changes: 33 additions & 0 deletions tests/Unit/Pipeline/Target/ConsumerTargetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Pipeline\Target;

use Patchlevel\EventSourcing\EventBus\Consumer;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Pipeline\Target\ConsumerTarget;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

/** @covers \Patchlevel\EventSourcing\Pipeline\Target\ConsumerTarget */
final class ConsumerTargetTest extends TestCase
{
use ProphecyTrait;

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

$consumer = $this->prophesize(Consumer::class);
$consumer->consume($message)->shouldBeCalledOnce();

$consumerTarget = new ConsumerTarget($consumer->reveal());
$consumerTarget->save($message);
}
}
75 changes: 0 additions & 75 deletions tests/Unit/Pipeline/Target/ProjectorRepositoryTargetTest.php

This file was deleted.

68 changes: 0 additions & 68 deletions tests/Unit/Pipeline/Target/ProjectorTargetTest.php

This file was deleted.

Loading