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

remove own clock interface and use only psr-20 #425

Merged
merged 1 commit into from
Nov 30, 2023
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
21 changes: 15 additions & 6 deletions docs/pages/clock.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
11 changes: 0 additions & 11 deletions src/Clock/Clock.php

This file was deleted.

3 changes: 2 additions & 1 deletion src/Clock/FrozenClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;
use Psr\Clock\ClockInterface;

use function sprintf;

final class FrozenClock implements Clock
final class FrozenClock implements ClockInterface
{
public function __construct(
private DateTimeImmutable $frozenDateTime,
Expand Down
3 changes: 2 additions & 1 deletion src/Clock/SystemClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace Patchlevel\EventSourcing\Clock;

use DateTimeImmutable;
use Psr\Clock\ClockInterface;

final class SystemClock implements Clock
final class SystemClock implements ClockInterface
{
public function now(): DateTimeImmutable
{
Expand Down
6 changes: 3 additions & 3 deletions src/Repository/DefaultRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,6 +17,7 @@
use Patchlevel\EventSourcing\Store\CriteriaBuilder;
use Patchlevel\EventSourcing\Store\Store;
use Patchlevel\EventSourcing\Store\Stream;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Throwable;
Expand All @@ -35,7 +35,7 @@
*/
final class DefaultRepository implements Repository
{
private Clock $clock;
private ClockInterface $clock;
private LoggerInterface $logger;

/** @var WeakMap<T, bool> */
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/Repository/DefaultRepositoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,14 +14,15 @@
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;

use function array_key_exists;

final class DefaultRepositoryManager implements RepositoryManager
{
private Clock $clock;
private ClockInterface $clock;
private AggregateRootMetadataFactory $metadataFactory;
private LoggerInterface $logger;

Expand All @@ -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,
) {
Expand Down
Loading