Skip to content

Commit

Permalink
finish traceable repository
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 12, 2023
1 parent d40619c commit 271d035
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
12 changes: 10 additions & 2 deletions src/Debug/ProfileData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<AggregateRoot> */
private readonly string $aggregateClass,
Expand All @@ -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;
Expand All @@ -57,13 +59,19 @@ public function duration(): float|null
}

/** @param class-string<AggregateRoot> $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<AggregateRoot> $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<AggregateRoot> $aggregateClass */
public static function saveAggregate(string $aggregateClass, string $aggregateId): self
{
return new ProfileData('save', $aggregateClass, $aggregateId);
}
Expand Down
32 changes: 24 additions & 8 deletions src/Debug/TraceableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit 271d035

Please sign in to comment.