Skip to content

Commit

Permalink
Merge pull request #454 from patchlevel/projector-id
Browse files Browse the repository at this point in the history
Add ProjectorId as VO
  • Loading branch information
DavidBadura authored Jan 5, 2024
2 parents fc89381 + c443311 commit da42f8c
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 49 deletions.
7 changes: 4 additions & 3 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ public function projections(): ProjectionCollection
$projectors = $this->projectors();

foreach ($projectors as $projector) {
$projectionId = $this->projectorResolver->projectionId($projector);
$projectorId = $this->projectorResolver->projectorId($projector);
$projectionId = $projectorId->toProjectionId();

if ($projections->has($projectionId)) {
continue;
Expand Down Expand Up @@ -432,9 +433,9 @@ private function projectors(): array
$this->projectors = [];

foreach ($this->projectorRepository->projectors() as $projector) {
$projectionId = $this->projectorResolver->projectionId($projector);
$projectorId = $this->projectorResolver->projectorId($projector);

$this->projectors[$projectionId->toString()] = $projector;
$this->projectors[$projectorId->toString()] = $projector;
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/Projection/Projector/MetadataProjectorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Metadata\Projector\AttributeProjectorMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadataFactory;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;

use function array_key_exists;

Expand Down Expand Up @@ -57,11 +56,11 @@ public function resolveSubscribeMethod(object $projector, Message $message): Clo
return $projector->$subscribeMethod(...);
}

public function projectionId(object $projector): ProjectionId
public function projectorId(object $projector): ProjectorId
{
$metadata = $this->metadataFactory->metadata($projector::class);

return new ProjectionId(
return new ProjectorId(
$metadata->name,
$metadata->version,
);
Expand Down
22 changes: 11 additions & 11 deletions src/Projection/Projector/ProjectorHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Patchlevel\EventSourcing\Projection\Projector;

use Patchlevel\EventSourcing\Metadata\Projector\AttributeProjectorMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadata;
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadataFactory;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;

final class ProjectorHelper
{
Expand All @@ -17,23 +17,23 @@ public function __construct(

public function name(object $projector): string
{
$metadata = $this->metadataFactory->metadata($projector::class);

return $metadata->name;
return $this->getProjectorMetadata($projector)->name;
}

public function version(object $projector): int
{
$metadata = $this->metadataFactory->metadata($projector::class);
return $this->getProjectorMetadata($projector)->version;
}

public function projectorId(object $projector): ProjectorId
{
$metadata = $this->getProjectorMetadata($projector);

return $metadata->version;
return new ProjectorId($metadata->name, $metadata->version);
}

public function projectionId(object $projector): ProjectionId
private function getProjectorMetadata(object $projector): ProjectorMetadata
{
return new ProjectionId(
$this->name($projector),
$this->version($projector),
);
return $this->metadataFactory->metadata($projector::class);
}
}
69 changes: 69 additions & 0 deletions src/Projection/Projector/ProjectorId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

use InvalidArgumentException;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;

use function array_pop;
use function count;
use function ctype_digit;
use function explode;
use function implode;
use function sprintf;

/** @psalm-immutable */
final class ProjectorId
{
public function __construct(
private readonly string $name,
private readonly int $version,
) {
}

public function toString(): string
{
return sprintf('%s-%s', $this->name, $this->version);
}

public function name(): string
{
return $this->name;
}

public function version(): int
{
return $this->version;
}

public function equals(self $other): bool
{
return $this->name === $other->name && $this->version === $other->version;
}

public static function fromString(string $value): self
{
$parts = explode('-', $value);

if (count($parts) < 2) {
throw new InvalidArgumentException();
}

$version = array_pop($parts);

if (!ctype_digit($version)) {
throw new InvalidArgumentException();
}

$name = implode('-', $parts);

return new self($name, (int)$version);
}

public function toProjectionId(): ProjectionId
{
return new ProjectionId($this->name, $this->version);
}
}
3 changes: 1 addition & 2 deletions src/Projection/Projector/ProjectorResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Closure;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;

interface ProjectorResolver
{
Expand All @@ -16,5 +15,5 @@ public function resolveTeardownMethod(object $projector): Closure|null;

public function resolveSubscribeMethod(object $projector, Message $message): Closure|null;

public function projectionId(object $projector): ProjectionId;
public function projectorId(object $projector): ProjectorId;
}
18 changes: 11 additions & 7 deletions src/Projection/Projector/ProjectorUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Patchlevel\EventSourcing\Metadata\Projector\AttributeProjectorMetadataFactory;
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadataFactory;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionId;

trait ProjectorUtil
{
Expand All @@ -26,18 +25,23 @@ private static function metadataFactory(): ProjectorMetadataFactory
return self::$metadataFactory;
}

private function projectionName(): string
private function getProjectorHelper(): ProjectorHelper
{
return (new ProjectorHelper(self::metadataFactory()))->name($this);
return new ProjectorHelper(self::metadataFactory());
}

private function projectionVersion(): int
private function projectorName(): string
{
return (new ProjectorHelper(self::metadataFactory()))->version($this);
return $this->getProjectorHelper()->name($this);
}

private function projectionId(): ProjectionId
private function projectorVersion(): int
{
return (new ProjectorHelper(self::metadataFactory()))->projectionId($this);
return $this->getProjectorHelper()->version($this);
}

private function projectorId(): ProjectorId
{
return $this->getProjectorHelper()->projectorId($this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ private function tableName(): string
{
return sprintf(
'projection_%s_%s',
$this->projectionName(),
$this->projectionVersion(),
$this->projectorName(),
$this->projectorVersion(),
);
}
}
Loading

0 comments on commit da42f8c

Please sign in to comment.