DateTimeImmutable 👉 Clock

This commit is contained in:
theUniC
2022-04-18 10:37:29 +02:00
parent 5999d1aa5e
commit 215eb39c5c
7 changed files with 32 additions and 15 deletions

View File

@@ -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

View File

@@ -6,7 +6,7 @@ namespace Cheeper\AllChapters\DomainModel\Clock;
use DateTimeImmutable;
class DateCollectionClockStrategy implements ClockStrategy
final class DateCollectionClockStrategy implements ClockStrategy
{
private int $iterator;

View File

@@ -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);
}

View File

@@ -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(

View File

@@ -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'))
);
}

View File

@@ -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

View File

@@ -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')
];
}