From 271d0354ca0645739bce03b4bcbd92be24057935 Mon Sep 17 00:00:00 2001 From: David Badura Date: Tue, 12 Dec 2023 22:29:17 +0100 Subject: [PATCH] finish traceable repository --- src/Debug/ProfileData.php | 12 ++++++++++-- src/Debug/TraceableRepository.php | 32 +++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/Debug/ProfileData.php b/src/Debug/ProfileData.php index ea1fb9fdc..7d9937d32 100644 --- a/src/Debug/ProfileData.php +++ b/src/Debug/ProfileData.php @@ -14,6 +14,7 @@ final class ProfileData private float|null $duration = null; private function __construct( + /** @var 'load'|'has'|'save' */ private readonly string $type, /** @var class-string */ private readonly string $aggregateClass, @@ -35,6 +36,7 @@ public function stop(): void $this->duration = microtime(true) - $this->start; } + /** @return 'load'|'has'|'save' */ public function type(): string { return $this->type; @@ -57,13 +59,19 @@ public function duration(): float|null } /** @param class-string $aggregateClass */ - public static function load(string $aggregateClass, string $aggregateId): self + public static function loadAggregate(string $aggregateClass, string $aggregateId): self { return new ProfileData('load', $aggregateClass, $aggregateId); } /** @param class-string $aggregateClass */ - public static function save(string $aggregateClass, string $aggregateId): self + public static function hasAggregate(string $aggregateClass, string $aggregateId): self + { + return new ProfileData('has', $aggregateClass, $aggregateId); + } + + /** @param class-string $aggregateClass */ + public static function saveAggregate(string $aggregateClass, string $aggregateId): self { return new ProfileData('save', $aggregateClass, $aggregateId); } diff --git a/src/Debug/TraceableRepository.php b/src/Debug/TraceableRepository.php index 1c04504b2..d5bc41a34 100644 --- a/src/Debug/TraceableRepository.php +++ b/src/Debug/TraceableRepository.php @@ -27,34 +27,50 @@ public function __construct( /** @return T */ public function load(string $id): AggregateRoot { - $data = ProfileData::load($this->aggregateClass, $id); + $data = ProfileData::loadAggregate($this->aggregateClass, $id); $this->dataHolder->addData($data); $event = $this->stopwatch?->start('event_sourcing', 'event_sourcing'); $data->start(); try { - $aggregate = $this->repository->load($id); + return $this->repository->load($id); } finally { $data->stop(); $event?->stop(); } - - return $aggregate; } public function has(string $id): bool { - return $this->repository->has($id); + $data = ProfileData::hasAggregate($this->aggregateClass, $id); + + $this->dataHolder->addData($data); + $event = $this->stopwatch?->start('event_sourcing', 'event_sourcing'); + $data->start(); + + try { + return $this->repository->has($id); + } finally { + $data->stop(); + $event?->stop(); + } } /** @param T $aggregate */ public function save(AggregateRoot $aggregate): void { - $event = $this->stopwatch?->start('event_sourcing', 'event_sourcing'); + $data = ProfileData::saveAggregate($this->aggregateClass, $aggregate->aggregateRootId()); - $this->repository->save($aggregate); + $this->dataHolder->addData($data); + $event = $this->stopwatch?->start('event_sourcing', 'event_sourcing'); + $data->start(); - $event?->stop(); + try { + $this->repository->save($aggregate); + } finally { + $data->stop(); + $event?->stop(); + } } }