Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProjectorId as value object #454

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,11 +8,10 @@
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;

final class MetadataProjectorResolver implements ProjectorResolver

Check failure on line 14 in src/Projection/Projector/MetadataProjectorResolver.php

View workflow job for this annotation

GitHub Actions / Backward Compatibility Check (locked, 8.2, ubuntu-latest)

Method Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver#resolveCreateMethod() was removed
{
public function __construct(
private readonly ProjectorMetadataFactory $metadataFactory = new AttributeProjectorMetadataFactory(),
Expand Down Expand Up @@ -57,11 +56,11 @@
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
Loading