Skip to content
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
2 changes: 0 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ jobs:
- "lowest"
- "highest"
php-version:
- "8.2"
- "8.3"
- "8.4"
- "8.5"
operating-system:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
"php": "~8.4.0 || ~8.5.0",
"patchlevel/event-sourcing": "4.0.x-dev",
"patchlevel/hydrator": "^2.0.3",
"symfony/cache": "^7.3.0 || ^8.0.0",
Expand Down
19 changes: 10 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ patchlevel_event_sourcing:
default_max_retries: 3
default_exceptions:
- Patchlevel\EventSourcing\Repository\AggregateOutdated
- Patchlevel\EventSourcing\Store\AppendConditionNotMet
```
:::note
You can find out more about instant retry [here](/docs/event-sourcing/latest/command-bus/#instant-retry).
Expand Down Expand Up @@ -914,7 +915,7 @@ patchlevel_event_sourcing:
## Dynamic Consistency Boundary

You can enable the experimental dynamic consistency boundary (DCB).
This registers the `DecisionModelBuilder` and the `EventAppender` services.
This registers the `DecisionFactory` and the `EventAppender` services.

```yaml
patchlevel_event_sourcing:
Expand All @@ -930,4 +931,38 @@ DCB needs a store that supports appending, like `dbal_taggable` or `in_memory` f
We recommend PostgreSQL for DCB, because only there the tag queries can use an index.
:::

Command handlers are messenger handlers, so they only get the command.
Inject the `DecisionFactory` and begin a new `Decision` for every command.

```php
use Patchlevel\EventSourcing\DecisionModel\DecisionFactory;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'command.bus')]
final class CreateHotelHandler
{
public function __construct(
private readonly DecisionFactory $decisionFactory,
) {
}

public function __invoke(CreateHotel $command): void
{
$decision = $this->decisionFactory->begin();

if ($decision->read(new HotelExists($command->hotelId))) {
throw new RuntimeException('Hotel already exists');
}

$decision->append([
new HotelCreated($command->hotelId, $command->hotelName),
]);
}
}
```
:::note
If something the decision has read changed in the meantime, an `AppendConditionNotMet` exception is thrown.
If you dispatch the command with the `CommandBus` service, its instant retry retries the command.
:::

If you want to learn more about DCB, read the [library documentation](/docs/event-sourcing/latest/dynamic-consistency-boundary).
3 changes: 2 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcingBundle\DependencyInjection;

use Patchlevel\EventSourcing\Repository\AggregateOutdated;
use Patchlevel\EventSourcing\Store\AppendConditionNotMet;
use Patchlevel\Hydrator\Extension\Cryptography\Cipher\OpensslCipherKeyFactory;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
Expand Down Expand Up @@ -285,7 +286,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue(3)
->end()
->arrayNode('default_exceptions')
->defaultValue([AggregateOutdated::class])
->defaultValue([AggregateOutdated::class, AppendConditionNotMet::class])
->scalarPrototype()->end()
->end()
->end()
Expand Down
13 changes: 7 additions & 6 deletions src/DependencyInjection/PatchlevelEventSourcingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
use Patchlevel\EventSourcing\Console\Command\WatchCommand;
use Patchlevel\EventSourcing\Console\DoctrineHelper;
use Patchlevel\EventSourcing\Cryptography\DoctrineCipherKeyStore;
use Patchlevel\EventSourcing\DecisionModel\DecisionModelBuilder;
use Patchlevel\EventSourcing\DecisionModel\DecisionFactory;
use Patchlevel\EventSourcing\DecisionModel\EventAppender;
use Patchlevel\EventSourcing\DecisionModel\StoreDecisionModelBuilder;
use Patchlevel\EventSourcing\DecisionModel\StoreEventAppender;
use Patchlevel\EventSourcing\EventBus\AttributeListenerProvider;
use Patchlevel\EventSourcing\EventBus\Consumer;
Expand Down Expand Up @@ -1337,16 +1336,18 @@ private function configureDCB(array $config, ContainerBuilder $container): void
);
}

$container->register(StoreDecisionModelBuilder::class)
->setArguments([new Reference(Store::class)]);
$container->setAlias(DecisionModelBuilder::class, StoreDecisionModelBuilder::class);

$container->register(StoreEventAppender::class)
->setArguments([
new Reference(Store::class),
new Reference(EventTagExtractor::class),
]);
$container->setAlias(EventAppender::class, StoreEventAppender::class);

$container->register(DecisionFactory::class)
->setArguments([
new Reference(Store::class),
new Reference(EventAppender::class),
]);
}

private function configureValueResolver(ContainerBuilder $container): void
Expand Down
13 changes: 9 additions & 4 deletions tests/Unit/PatchlevelEventSourcingBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
use Patchlevel\EventSourcing\Console\Command\SubscriptionStatusCommand;
use Patchlevel\EventSourcing\Console\Command\SubscriptionTeardownCommand;
use Patchlevel\EventSourcing\Console\Command\WatchCommand;
use Patchlevel\EventSourcing\DecisionModel\DecisionModelBuilder;
use Patchlevel\EventSourcing\DecisionModel\DecisionFactory;
use Patchlevel\EventSourcing\DecisionModel\EventAppender;
use Patchlevel\EventSourcing\DecisionModel\StoreDecisionModelBuilder;
use Patchlevel\EventSourcing\DecisionModel\StoreEventAppender;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\EventBus;
Expand All @@ -63,6 +62,7 @@
use Patchlevel\EventSourcing\Metadata\Subscriber\Psr6SubscriberMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Subscriber\SubscriberMetadataFactory;
use Patchlevel\EventSourcing\QueryBus\QueryBus;
use Patchlevel\EventSourcing\Repository\AggregateOutdated;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
use Patchlevel\EventSourcing\Repository\MessageDecorator\ChainMessageDecorator;
Expand All @@ -79,6 +79,7 @@
use Patchlevel\EventSourcing\Snapshot\Adapter\Psr6SnapshotAdapter;
use Patchlevel\EventSourcing\Snapshot\DefaultSnapshotStore;
use Patchlevel\EventSourcing\Snapshot\SnapshotStore;
use Patchlevel\EventSourcing\Store\AppendConditionNotMet;
use Patchlevel\EventSourcing\Store\ArchivedHeader;
use Patchlevel\EventSourcing\Store\Dbal\PostgreSQLPlatform as EventSourcingPostgreSQLPlatform;
use Patchlevel\EventSourcing\Store\Dbal\PostgreSQLPlatformMiddleware;
Expand Down Expand Up @@ -592,6 +593,10 @@ public function testCommandBus(): void
self::assertEquals(CreateProfile::class, $tag['handles']);
self::assertEquals('command.bus', $tag['bus']);
self::assertInstanceOf(InstantRetryCommandBus::class, $container->get(CommandBus::class));
self::assertSame(
[AggregateOutdated::class, AppendConditionNotMet::class],
$container->getDefinition(InstantRetryCommandBus::class)->getArgument(2),
);
}

public function testQueryBus(): void
Expand Down Expand Up @@ -644,7 +649,7 @@ public function testDCB(): void
);

self::assertInstanceOf(StoreEventAppender::class, $container->get(EventAppender::class));
self::assertInstanceOf(StoreDecisionModelBuilder::class, $container->get(DecisionModelBuilder::class));
self::assertInstanceOf(DecisionFactory::class, $container->get(DecisionFactory::class));
}

public function testDCBWithInMemoryStore(): void
Expand All @@ -663,7 +668,7 @@ public function testDCBWithInMemoryStore(): void
);

self::assertInstanceOf(StoreEventAppender::class, $container->get(EventAppender::class));
self::assertInstanceOf(StoreDecisionModelBuilder::class, $container->get(DecisionModelBuilder::class));
self::assertInstanceOf(DecisionFactory::class, $container->get(DecisionFactory::class));
}

public function testDCBWithStreamStore(): void
Expand Down
Loading