Skip to content

Commit

Permalink
refactor projector
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 25, 2023
1 parent 56c5928 commit 622b6b8
Show file tree
Hide file tree
Showing 35 changed files with 275 additions and 408 deletions.
9 changes: 8 additions & 1 deletion baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.17.0@c620f6e80d0abfca532b00bda366062aaedf6e5d">
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="src/Aggregate/AggregateRootBehaviour.php">
<UnsafeInstantiation>
<code>new static()</code>
Expand Down Expand Up @@ -55,6 +55,13 @@
<code><![CDATA[$this->projectors]]></code>
</InvalidOperand>
</file>
<file src="src/Projection/Projector/MetadataProjectorResolver.php">
<MixedMethodCall>
<code>$method</code>
<code>$method</code>
<code>$subscribeMethod</code>
</MixedMethodCall>
</file>
<file src="src/Repository/DefaultRepository.php">
<PropertyTypeCoercion>
<code>new WeakMap()</code>
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
path: src/EventBus/Message.php

-
message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projector\\\\InMemoryProjectorRepository\\:\\:projectors\\(\\) should return array\\<int, Patchlevel\\\\EventSourcing\\\\Projection\\\\Projector\\\\Projector\\> but returns array\\<int\\|string, Patchlevel\\\\EventSourcing\\\\Projection\\\\Projector\\\\Projector\\>\\.$#"
message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projector\\\\InMemoryProjectorRepository\\:\\:projectors\\(\\) should return array\\<int, object\\> but returns array\\<int\\|string, object\\>\\.$#"
count: 1
path: src/Projection/Projector/InMemoryProjectorRepository.php

Expand Down
16 changes: 13 additions & 3 deletions src/Metadata/Projector/AttributeProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

use Patchlevel\EventSourcing\Attribute\Create;
use Patchlevel\EventSourcing\Attribute\Drop;
use Patchlevel\EventSourcing\Attribute\Projection;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Projection\Projector\Projector;
use ReflectionClass;

use function array_key_exists;

final class AttributeProjectorMetadataFactory implements ProjectorMetadataFactory
{
/** @var array<class-string<Projector>, ProjectorMetadata> */
/** @var array<class-string, ProjectorMetadata> */
private array $projectorMetadata = [];

/** @param class-string<Projector> $projector */
/** @param class-string $projector */
public function metadata(string $projector): ProjectorMetadata
{
if (array_key_exists($projector, $this->projectorMetadata)) {
Expand All @@ -26,6 +26,14 @@ public function metadata(string $projector): ProjectorMetadata

$reflector = new ReflectionClass($projector);

$attributes = $reflector->getAttributes(Projection::class);

if ($attributes === []) {
throw new ClassIsNotAProjector($projector);
}

$projection = $attributes[0]->newInstance();

$methods = $reflector->getMethods();

$subscribeMethods = [];
Expand Down Expand Up @@ -79,6 +87,8 @@ public function metadata(string $projector): ProjectorMetadata
}

$metadata = new ProjectorMetadata(
$projection->name(),
$projection->version(),
$subscribeMethods,
$createMethod,
$dropMethod,
Expand Down
23 changes: 23 additions & 0 deletions src/Metadata/Projector/ClassIsNotAProjector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Metadata\MetadataException;

use function sprintf;

final class ClassIsNotAProjector extends MetadataException
{
/** @param class-string $class */
public function __construct(string $class)
{
parent::__construct(
sprintf(
'Class "%s" is not a projector',
$class,
),
);
}
}
3 changes: 1 addition & 2 deletions src/Metadata/Projector/DuplicateCreateMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Metadata\MetadataException;
use Patchlevel\EventSourcing\Projection\Projector\Projector;

use function sprintf;

final class DuplicateCreateMethod extends MetadataException
{
/** @param class-string<Projector> $projector */
/** @param class-string $projector */
public function __construct(string $projector, string $fistMethod, string $secondMethod)
{
parent::__construct(
Expand Down
3 changes: 1 addition & 2 deletions src/Metadata/Projector/DuplicateDropMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Metadata\MetadataException;
use Patchlevel\EventSourcing\Projection\Projector\Projector;

use function sprintf;

final class DuplicateDropMethod extends MetadataException
{
/** @param class-string<Projector> $projector */
/** @param class-string $projector */
public function __construct(string $projector, string $fistMethod, string $secondMethod)
{
parent::__construct(
Expand Down
5 changes: 2 additions & 3 deletions src/Metadata/Projector/DuplicateSubscribeMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Metadata\MetadataException;
use Patchlevel\EventSourcing\Projection\Projector\Projector;

use function sprintf;

final class DuplicateSubscribeMethod extends MetadataException
{
/**
* @param class-string<Projector> $projector
* @param class-string $event
* @param class-string $projector
* @param class-string $event
*/
public function __construct(string $projector, string $event, string $fistMethod, string $secondMethod)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/Projector/ProjectorMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
final class ProjectorMetadata
{
public function __construct(
public readonly string $name,
public readonly int $version,
/** @var array<class-string, string> */
public readonly array $subscribeMethods = [],
public readonly string|null $createMethod = null,
Expand Down
4 changes: 1 addition & 3 deletions src/Metadata/Projector/ProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Projection\Projector\Projector;

interface ProjectorMetadataFactory
{
/** @param class-string<Projector> $projector */
/** @param class-string $projector */
public function metadata(string $projector): ProjectorMetadata;
}
3 changes: 1 addition & 2 deletions src/Metadata/Projector/Psr16ProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Psr\SimpleCache\CacheInterface;

final class Psr16ProjectorMetadataFactory implements ProjectorMetadataFactory
Expand All @@ -15,7 +14,7 @@ public function __construct(
) {
}

/** @param class-string<Projector> $projector */
/** @param class-string $projector */
public function metadata(string $projector): ProjectorMetadata
{
/** @var ?ProjectorMetadata $metadata */
Expand Down
3 changes: 1 addition & 2 deletions src/Metadata/Projector/Psr6ProjectorMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Patchlevel\EventSourcing\Metadata\Projector;

use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Psr\Cache\CacheItemPoolInterface;

use function assert;
Expand All @@ -17,7 +16,7 @@ public function __construct(
) {
}

/** @param class-string<Projector> $projector */
/** @param class-string $projector */
public function metadata(string $projector): ProjectorMetadata
{
$item = $this->cache->getItem($projector);
Expand Down
3 changes: 1 addition & 2 deletions src/Pipeline/Target/ProjectorTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@

use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver;
use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorHelper;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;

final class ProjectorTarget implements Target
{
public function __construct(
private readonly Projector $projector,
private readonly object $projector,
private readonly ProjectorResolver $projectorResolver = new MetadataProjectorResolver(),
) {
}
Expand Down
6 changes: 5 additions & 1 deletion src/Projection/Projection/Store/ErrorSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ public static function serialize(Throwable|null $error): string|null
return null;
}

return serialize($error);
try {
return serialize($error);
} catch (Throwable) {
return null;
}
}

public static function unserialize(string|null $error): Throwable|null
Expand Down
20 changes: 10 additions & 10 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus;
use Patchlevel\EventSourcing\Projection\Projection\Store\ProjectionStore;
use Patchlevel\EventSourcing\Projection\Projector\MetadataProjectorResolver;
use Patchlevel\EventSourcing\Projection\Projector\Projector;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository;
use Patchlevel\EventSourcing\Projection\Projector\ProjectorResolver;
use Patchlevel\EventSourcing\Store\CriteriaBuilder;
Expand All @@ -24,7 +23,7 @@

final class DefaultProjectionist implements Projectionist
{
/** @var array<string, Projector>|null */
/** @var array<string, object>|null */
private array|null $projectors = null;

public function __construct(
Expand Down Expand Up @@ -354,15 +353,16 @@ public function reactivate(ProjectionCriteria $criteria = new ProjectionCriteria
public function projections(): ProjectionCollection
{
$projections = $this->projectionStore->all();
$projectors = $this->projectors();

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

if ($projections->has($targetProjection)) {
if ($projections->has($projectionId)) {
continue;
}

$projections = $projections->add(new Projection($targetProjection));
$projections = $projections->add(new Projection($projectionId));
}

return $projections;
Expand Down Expand Up @@ -419,23 +419,23 @@ private function handleMessage(Message $message, Projection $projection, bool $t
$this->projectionStore->save($projection);
}

private function projector(ProjectionId $projectorId): Projector|null
private function projector(ProjectionId $projectorId): object|null
{
$projectors = $this->projectors();

return $projectors[$projectorId->toString()] ?? null;
}

/** @return array<string, Projector> */
/** @return array<string, object> */
private function projectors(): array
{
if ($this->projectors === null) {
$this->projectors = [];

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

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

Expand Down
37 changes: 0 additions & 37 deletions src/Projection/Projector/BasicProjector.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Projection/Projector/InMemoryProjectorRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

final class InMemoryProjectorRepository implements ProjectorRepository
{
/** @param iterable<Projector> $projectors */
/** @param iterable<object> $projectors */
public function __construct(
private readonly iterable $projectors = [],
) {
}

/** @return list<Projector> */
/** @return list<object> */
public function projectors(): array
{
return [...$this->projectors];
Expand Down
Loading

0 comments on commit 622b6b8

Please sign in to comment.