DateTimeImmutable 👉 Clock
This commit is contained in:
@@ -8,23 +8,23 @@ use Cheeper\AllChapters\DomainModel\Clock\ClockStrategy;
|
||||
use Cheeper\AllChapters\DomainModel\Clock\DefaultClockStrategy;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Clock
|
||||
final class Clock
|
||||
{
|
||||
protected static ?Clock $instance = null;
|
||||
protected ClockStrategy $strategy;
|
||||
private static ?Clock $instance = null;
|
||||
private ClockStrategy $strategy;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
$this->strategy = new DefaultClockStrategy();
|
||||
}
|
||||
|
||||
public static function instance(): static
|
||||
public static function instance(): self
|
||||
{
|
||||
if (null === static::$instance) {
|
||||
static::$instance = new static();
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return static::$instance;
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function now(): DateTimeImmutable
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Cheeper\AllChapters\DomainModel\Clock;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class DateCollectionClockStrategy implements ClockStrategy
|
||||
final class DateCollectionClockStrategy implements ClockStrategy
|
||||
{
|
||||
private int $iterator;
|
||||
|
||||
|
||||
@@ -5,6 +5,8 @@ declare(strict_types=1);
|
||||
namespace Cheeper\Chapter2;
|
||||
|
||||
//snippet cheep
|
||||
use Cheeper\AllChapters\DomainModel\Clock;
|
||||
|
||||
final class Cheep
|
||||
{
|
||||
private ?int $id = null;
|
||||
@@ -18,7 +20,7 @@ final class Cheep
|
||||
|
||||
private function __construct(private int $authorId, string $message)
|
||||
{
|
||||
$this->date = new \DateTimeImmutable();
|
||||
$this->date = Clock::instance()->now();
|
||||
$this->setMessage($message);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ use Cheeper\AllChapters\DomainModel\Author\AuthorId;
|
||||
use Cheeper\AllChapters\DomainModel\Cheep\CheepDate;
|
||||
use Cheeper\AllChapters\DomainModel\Cheep\CheepId;
|
||||
use Cheeper\AllChapters\DomainModel\Cheep\CheepMessage;
|
||||
use Cheeper\AllChapters\DomainModel\Clock;
|
||||
use Cheeper\Chapter7\DomainModel\TriggerEventsTrait;
|
||||
|
||||
class Cheep
|
||||
@@ -27,8 +28,13 @@ class Cheep
|
||||
|
||||
public static function compose(AuthorId $authorId, CheepId $cheepId, CheepMessage $cheepMessage): self
|
||||
{
|
||||
$now = Clock::instance()
|
||||
->now()
|
||||
->setTimezone(new \DateTimeZone('UTC'))
|
||||
;
|
||||
|
||||
$cheepDate = new CheepDate(
|
||||
(new \DateTimeImmutable(timezone: new \DateTimeZone('UTC')))->format('Y-m-d H:i:s')
|
||||
$now->format('Y-m-d H:i:s')
|
||||
);
|
||||
|
||||
return new self(
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Cheeper\Chapter7\DomainModel\Follow;
|
||||
|
||||
use Cheeper\AllChapters\DomainModel\Clock;
|
||||
use Cheeper\Chapter7\Application\MessageTrait;
|
||||
use Cheeper\Chapter7\DomainModel\DomainEvent;
|
||||
use DateTimeImmutable;
|
||||
@@ -28,9 +29,9 @@ final class AuthorFollowed implements DomainEvent
|
||||
$follow->followId()->toString(),
|
||||
$follow->fromAuthorId()->toString(),
|
||||
$follow->toAuthorId()->toString(),
|
||||
new DateTimeImmutable(
|
||||
timezone: new DateTimeZone("UTC")
|
||||
)
|
||||
Clock::instance()
|
||||
->now()
|
||||
->setTimezone(new \DateTimeZone('UTC'))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,8 @@ class NewAuthorSigned implements DomainEvent
|
||||
{
|
||||
use MessageTrait;
|
||||
|
||||
private DateTimeImmutable $occurredOn;
|
||||
|
||||
private function __construct(
|
||||
private string $authorId,
|
||||
private string $authorUsername,
|
||||
@@ -23,8 +25,13 @@ class NewAuthorSigned implements DomainEvent
|
||||
private ?string $authorLocation = null,
|
||||
private ?string $authorWebsite = null,
|
||||
private ?DateTimeImmutable $authorBirthDate = null,
|
||||
private DateTimeImmutable $occurredOn = new DateTimeImmutable(),
|
||||
?DateTimeImmutable $occurredOn = null,
|
||||
) {
|
||||
if (null === $occurredOn) {
|
||||
$occurredOn = Clock::instance()->now();
|
||||
}
|
||||
|
||||
$this->occurredOn = $occurredOn;
|
||||
}
|
||||
|
||||
public static function fromAuthor(Author $author): static
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Cheeper\Chapter9\Infrastructure\DomainModel;
|
||||
|
||||
use Cheeper\AllChapters\DomainModel\Clock;
|
||||
use Cheeper\Chapter7\DomainModel\DomainEvent;
|
||||
use Cheeper\Chapter9\DomainModel\EventStore;
|
||||
use Cheeper\Chapter9\DomainModel\EventStream;
|
||||
@@ -69,7 +70,7 @@ final class RedisEventStore implements EventStore
|
||||
{
|
||||
return [
|
||||
'type' => get_class($event),
|
||||
'created_on' => (new \DateTimeImmutable())->format('YmdHis'),
|
||||
'created_on' => Clock::instance()->now()->format('YmdHis'),
|
||||
'data' => $this->serializer->serialize($event, 'json')
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user