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 3 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: 7 additions & 9 deletions src/Repository/DefaultRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Patchlevel\EventSourcing\Repository;

use DateTimeImmutable;
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 +39,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 +56,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 @@ -129,13 +131,9 @@ static function (object $event) use ($aggregate, &$playhead, $messageDecorator)
->withAggregateClass($aggregate::class)
->withAggregateId($aggregate->aggregateRootId())
->withPlayhead(++$playhead)
->withRecordedOn(Clock::createDateTimeImmutable());
->withRecordedOn(new DateTimeImmutable());
DanielBadura marked this conversation as resolved.
Show resolved Hide resolved

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
50 changes: 50 additions & 0 deletions tests/Unit/Clock/FreezeClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Clock;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock\FreezeClock;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Clock\FreezeClock */
class FreezeClockTest extends TestCase
{
public function testCreateDateTimeImmutableWithFrozenClock(): void
{
$current = new DateTimeImmutable();
$clock = new FreezeClock($current);

$new = $clock->create();

self::assertSame($current, $new);
}

public function testSleep(): void
{
$date1 = new DateTimeImmutable();
$clock = new FreezeClock($date1);
$clock->sleep(1);
$date2 = $clock->create();

$diff = $date1->diff($date2);

self::assertSame(1, $diff->s);
}

public function testReFreeze(): void
{
$date1 = new DateTimeImmutable();
$clock = new FreezeClock($date1);
$new1 = $clock->create();

$date2 = new DateTimeImmutable();
$clock->update($date2);
$new2 = $clock->create();

self::assertSame($date1, $new1);
self::assertSame($date2, $new2);
self::assertNotSame($new1, $new2);
}
}
23 changes: 23 additions & 0 deletions tests/Unit/Clock/SystemClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Clock;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Clock\SystemClock;
use PHPUnit\Framework\TestCase;

/** @covers \Patchlevel\EventSourcing\Clock\SystemClock */
class SystemClockTest extends TestCase
{
public function testCreateDateTimeImmutable(): void
{
$before = new DateTimeImmutable();
$date = (new SystemClock())->create();
$after = new DateTimeImmutable();

self::assertGreaterThanOrEqual($before, $date);
self::assertLessThanOrEqual($after, $date);
}
}
84 changes: 0 additions & 84 deletions tests/Unit/ClockTest.php

This file was deleted.

Loading