Skip to content

Commit

Permalink
Merge pull request #499 from patchlevel/add-consumer-pipeline-target
Browse files Browse the repository at this point in the history
add consumer target and replace old projector targets
  • Loading branch information
DavidBadura authored Feb 17, 2024
2 parents a469f0d + f7a7617 commit d52717a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 231 deletions.
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.

0 comments on commit d52717a

Please sign in to comment.