diff --git a/docs/pages/clock.md b/docs/pages/clock.md index 09a3dbc9a..f10c1105f 100644 --- a/docs/pages/clock.md +++ b/docs/pages/clock.md @@ -1,11 +1,9 @@ # Clock -We have a `Clock` interface which enables you to replace the actual clock implementation in your services for testing -purposes. We are using this clock to create the `recorded_on` datetime for the events. - -!!! note - - The `Clock` interface is PSR-20 compatible. For more information see [here](https://github.com/php-fig/fig-standards/blob/master/proposed/clock.md). +We are using the clock to get the current datetime. This is needed to create the `recorded_on` datetime for the events. +We have two implementations of the clock, one for the production and one for the tests. +But you can also create your own implementation that is PSR-20 compatible. +For more information see [here](https://github.com/php-fig/fig-standards/blob/master/proposed/clock.md). ## SystemClock @@ -56,6 +54,17 @@ $firstDate == $frozenDate // false $secondDate == $frozenDate // true ``` +Or you can use the `sleep` method to simulate a time jump. + +```php +use Patchlevel\EventSourcing\Clock\FrozenClock; + +$firstDate = new DateTimeImmutable(); +$clock = new FrozenClock($firstDate); + +$clock->sleep(10); // sleep 10 seconds +``` + !!! note The instance of the frozen datetime will be cloned internally, so the it's not the same instance but equals. \ No newline at end of file diff --git a/src/Clock/Clock.php b/src/Clock/Clock.php deleted file mode 100644 index ddb52b255..000000000 --- a/src/Clock/Clock.php +++ /dev/null @@ -1,11 +0,0 @@ - */ @@ -48,7 +48,7 @@ public function __construct( private readonly AggregateRootMetadata $metadata, private SnapshotStore|null $snapshotStore = null, private MessageDecorator|null $messageDecorator = null, - Clock|null $clock = null, + ClockInterface|null $clock = null, LoggerInterface|null $logger = null, ) { $this->clock = $clock ?? new SystemClock(); diff --git a/src/Repository/DefaultRepositoryManager.php b/src/Repository/DefaultRepositoryManager.php index 58755e9c0..7c4f7e4b7 100644 --- a/src/Repository/DefaultRepositoryManager.php +++ b/src/Repository/DefaultRepositoryManager.php @@ -5,7 +5,6 @@ namespace Patchlevel\EventSourcing\Repository; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; -use Patchlevel\EventSourcing\Clock\Clock; use Patchlevel\EventSourcing\Clock\SystemClock; use Patchlevel\EventSourcing\EventBus\Decorator\MessageDecorator; use Patchlevel\EventSourcing\EventBus\EventBus; @@ -15,6 +14,7 @@ use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry; use Patchlevel\EventSourcing\Snapshot\SnapshotStore; use Patchlevel\EventSourcing\Store\Store; +use Psr\Clock\ClockInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -22,7 +22,7 @@ final class DefaultRepositoryManager implements RepositoryManager { - private Clock $clock; + private ClockInterface $clock; private AggregateRootMetadataFactory $metadataFactory; private LoggerInterface $logger; @@ -35,7 +35,7 @@ public function __construct( private EventBus $eventBus, private SnapshotStore|null $snapshotStore = null, private MessageDecorator|null $messageDecorator = null, - Clock|null $clock = null, + ClockInterface|null $clock = null, AggregateRootMetadataFactory|null $metadataFactory = null, LoggerInterface|null $logger = null, ) {