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

Clock service and decorator #257

Merged
merged 6 commits into from
May 20, 2022
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
44 changes: 0 additions & 44 deletions src/Clock.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Clock/Clock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;

interface Clock
{
public function create(): DateTimeImmutable;
}
34 changes: 34 additions & 0 deletions src/Clock/FreezeClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;

use function sprintf;

final class FreezeClock implements Clock
{
public function __construct(private DateTimeImmutable $frozenDateTime)
{
}

public function update(DateTimeImmutable $frozenDateTime): void
{
$this->frozenDateTime = $frozenDateTime;
}

/**
* @param positive-int $seconds
*/
public function sleep(int $seconds): void
{
$this->frozenDateTime = $this->frozenDateTime->modify(sprintf('+%s seconds', $seconds));
}

public function create(): DateTimeImmutable
{
return $this->frozenDateTime;
}
}
15 changes: 15 additions & 0 deletions src/Clock/SystemClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;

final class SystemClock implements Clock
{
public function create(): DateTimeImmutable
{
return new DateTimeImmutable();
}
}
20 changes: 20 additions & 0 deletions src/EventBus/Decorator/RecordedOnDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\EventBus\Decorator;

use Patchlevel\EventSourcing\Clock\Clock;
use Patchlevel\EventSourcing\EventBus\Message;

final class RecordedOnDecorator implements MessageDecorator
{
public function __construct(private readonly Clock $clock)
{
}

public function __invoke(Message $message): Message
{
return $message->withRecordedOn($this->clock->create());
}
}
16 changes: 6 additions & 10 deletions src/Repository/DefaultRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Patchlevel\EventSourcing\Repository;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Clock;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\EventBus\Decorator\MessageDecorator;
use Patchlevel\EventSourcing\EventBus\Decorator\RecordedOnDecorator;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootMetadata;
Expand Down Expand Up @@ -37,7 +38,7 @@ final class DefaultRepository implements Repository
private ?SnapshotStore $snapshotStore;
private LoggerInterface $logger;
private AggregateRootMetadata $metadata;
private ?MessageDecorator $messageDecorator;
private MessageDecorator $messageDecorator;

/**
* @param class-string<T> $aggregateClass
Expand All @@ -54,7 +55,7 @@ public function __construct(
$this->eventBus = $eventBus;
$this->aggregateClass = $aggregateClass;
$this->snapshotStore = $snapshotStore;
$this->messageDecorator = $messageDecorator;
$this->messageDecorator = $messageDecorator ?? new RecordedOnDecorator(new SystemClock());
$this->logger = $logger ?? new NullLogger();
$this->metadata = $aggregateClass::metadata();
}
Expand Down Expand Up @@ -128,14 +129,9 @@ static function (object $event) use ($aggregate, &$playhead, $messageDecorator)
$message = Message::create($event)
->withAggregateClass($aggregate::class)
->withAggregateId($aggregate->aggregateRootId())
->withPlayhead(++$playhead)
->withRecordedOn(Clock::createDateTimeImmutable());
->withPlayhead(++$playhead);

if ($messageDecorator) {
$message = $messageDecorator($message);
}

return $message;
return $messageDecorator($message);
},
$events
);
Expand Down
6 changes: 4 additions & 2 deletions src/Repository/DefaultRepositoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Patchlevel\EventSourcing\Repository;

use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\EventBus\Decorator\MessageDecorator;
use Patchlevel\EventSourcing\EventBus\Decorator\RecordedOnDecorator;
use Patchlevel\EventSourcing\EventBus\EventBus;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootClassNotRegistered;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
Expand All @@ -22,7 +24,7 @@ final class DefaultRepositoryManager implements RepositoryManager
private Store $store;
private EventBus $eventBus;
private ?SnapshotStore $snapshotStore;
private ?MessageDecorator $messageDecorator;
private MessageDecorator $messageDecorator;
private LoggerInterface $logger;

/** @var array<class-string<AggregateRoot>, Repository> */
Expand All @@ -40,7 +42,7 @@ public function __construct(
$this->store = $store;
$this->eventBus = $eventBus;
$this->snapshotStore = $snapshotStore;
$this->messageDecorator = $messageDecorator;
$this->messageDecorator = $messageDecorator ?? new RecordedOnDecorator(new SystemClock());
$this->logger = $logger ?? new NullLogger();
}

Expand Down
74 changes: 54 additions & 20 deletions tests/Integration/BasicImplementation/BasicIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
namespace Patchlevel\EventSourcing\Tests\Integration\BasicImplementation;

use Doctrine\DBAL\Connection;
use Patchlevel\EventSourcing\Clock\SystemClock;
use Patchlevel\EventSourcing\EventBus\Decorator\ChainMessageDecorator;
use Patchlevel\EventSourcing\EventBus\Decorator\RecordedOnDecorator;
use Patchlevel\EventSourcing\EventBus\DefaultEventBus;
use Patchlevel\EventSourcing\EventBus\SymfonyEventBus;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AttributeAggregateRootRegistryFactory;
use Patchlevel\EventSourcing\Projection\MetadataAwareProjectionHandler;
use Patchlevel\EventSourcing\Projection\ProjectionListener;
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaManager;
use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer;
use Patchlevel\EventSourcing\Snapshot\Adapter\InMemorySnapshotAdapter;
Expand Down Expand Up @@ -60,13 +64,14 @@ public function testSuccessful(): void
'eventstore'
);

$repository = new DefaultRepository(
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
Profile::class,
null,
new FooMessageDecorator()
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock()), new FooMessageDecorator()])
);
$repository = $manager->get(Profile::class);

// create tables
$profileProjection->create();
Expand All @@ -82,7 +87,14 @@ public function testSuccessful(): void
self::assertSame('1', $result['id']);
self::assertSame('John', $result['name']);

$repository = new DefaultRepository($store, $eventStream, Profile::class);
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
null,
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock())])
);
$repository = $manager->get(Profile::class);
$profile = $repository->load('1');

self::assertInstanceOf(Profile::class, $profile);
Expand Down Expand Up @@ -111,7 +123,14 @@ public function testWithSymfonySuccessful(): void
'eventstore'
);

$repository = new DefaultRepository($store, $eventStream, Profile::class);
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
null,
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock())])
);
$repository = $manager->get(Profile::class);

// create tables
$profileProjection->create();
Expand All @@ -127,13 +146,14 @@ public function testWithSymfonySuccessful(): void
self::assertSame('1', $result['id']);
self::assertSame('John', $result['name']);

$repository = new DefaultRepository(
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
Profile::class,
null,
new FooMessageDecorator()
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock()), new FooMessageDecorator()])
);
$repository = $manager->get(Profile::class);

$profile = $repository->load('1');

Expand Down Expand Up @@ -161,13 +181,14 @@ public function testMultiTableSuccessful(): void
(new AttributeAggregateRootRegistryFactory())->create([__DIR__ . '/Aggregate']),
);

$repository = new DefaultRepository(
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
Profile::class,
null,
new FooMessageDecorator()
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock()), new FooMessageDecorator()])
);
$repository = $manager->get(Profile::class);

// create tables
$profileProjection->create();
Expand All @@ -183,7 +204,14 @@ public function testMultiTableSuccessful(): void
self::assertSame('1', $result['id']);
self::assertSame('John', $result['name']);

$repository = new DefaultRepository($store, $eventStream, Profile::class);
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
null,
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock())])
);
$repository = $manager->get(Profile::class);
$profile = $repository->load('1');

self::assertInstanceOf(Profile::class, $profile);
Expand Down Expand Up @@ -211,15 +239,14 @@ public function testSnapshot(): void
'eventstore'
);

$snapshotStore = new DefaultSnapshotStore(['default' => new InMemorySnapshotAdapter()]);

$repository = new DefaultRepository(
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
Profile::class,
$snapshotStore,
new FooMessageDecorator()
new DefaultSnapshotStore(['default' => new InMemorySnapshotAdapter()]),
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock()), new FooMessageDecorator()])
);
$repository = $manager->get(Profile::class);

// create tables
$profileProjection->create();
Expand All @@ -235,7 +262,14 @@ public function testSnapshot(): void
self::assertSame('1', $result['id']);
self::assertSame('John', $result['name']);

$repository = new DefaultRepository($store, $eventStream, Profile::class, $snapshotStore);
$manager = new DefaultRepositoryManager(
new AggregateRootRegistry(['profile' => Profile::class]),
$store,
$eventStream,
null,
new ChainMessageDecorator([new RecordedOnDecorator(new SystemClock())])
);
$repository = $manager->get(Profile::class);
$profile = $repository->load('1');

self::assertInstanceOf(Profile::class, $profile);
Expand Down
Loading