Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Mar 4, 2024
1 parent 2fbcbe1 commit 4c9dd22
Show file tree
Hide file tree
Showing 16 changed files with 207 additions and 217 deletions.
7 changes: 5 additions & 2 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@
<code><![CDATA[$projectionError->errorContext]]></code>
</PossiblyNullPropertyFetch>
</file>
<file src="src/Projection/Projectionist/DefaultProjectionist.php">
<file src="src/Projection/Projector/MetadataProjectorAccessor.php">
<MixedMethodCall>
<code><![CDATA[$method]]></code>
<code><![CDATA[$method]]></code>
<code><![CDATA[$method]]></code>
</MixedMethodCall>
<MixedReturnTypeCoercion>
<code><![CDATA[$this->projector->$method(...)]]></code>
<code><![CDATA[Closure(Message):void]]></code>
</MixedReturnTypeCoercion>
</file>
<file src="src/Repository/DefaultRepository.php">
<PropertyTypeCoercion>
Expand Down Expand Up @@ -135,7 +139,6 @@
<code><![CDATA[$bus]]></code>
<code><![CDATA[$id]]></code>
<code><![CDATA[$repository]]></code>
<code><![CDATA[$snapshotStore]]></code>
<code><![CDATA[$store]]></code>
</MissingConstructor>
</file>
Expand Down
3 changes: 1 addition & 2 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ private function discoverNewProjections(): void
new ProjectionCriteria(),
function (array $projections): void {
foreach ($this->projectorRepository->all() as $projector) {

foreach ($projections as $projection) {
if ($projection->id() === $projector->id()) {
continue 2;
Expand All @@ -804,7 +803,7 @@ function (array $projections): void {
$projector->id(),
$projector->group(),
$projector->runMode(),
)
),
);

$this->logger?->info(
Expand Down
20 changes: 13 additions & 7 deletions src/Projection/Projector/MetadataProjectorAccessor.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

use Closure;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadata;
use Patchlevel\EventSourcing\Projection\Projection\RunMode;

use function array_key_exists;
use function array_map;
use function array_merge;

final class MetadataProjectorAccessor implements ProjectorAccessor
{
/**
* @var array<class-string, iterable<Closure>>
*/
/** @var array<class-string, list<Closure(Message):void>> */
private array $subscribeCache = [];

public function __construct(
Expand Down Expand Up @@ -60,9 +65,9 @@ public function teardownMethod(): Closure|null
/**
* @param class-string $eventClass
*
* @return iterable<Closure>
* @return list<Closure(Message):void>
*/
public function subscribeMethods(string $eventClass): iterable
public function subscribeMethods(string $eventClass): array
{
if (array_key_exists($eventClass, $this->subscribeCache)) {
return $this->subscribeCache[$eventClass];
Expand All @@ -74,10 +79,11 @@ public function subscribeMethods(string $eventClass): iterable
);

$this->subscribeCache[$eventClass] = array_map(
fn(string $method) => $this->projector->$method(...),
/** @return Closure(Message):void */
fn (string $method) => $this->projector->$method(...),
$methods,
);

return $this->subscribeCache[$eventClass];
}
}
}
40 changes: 19 additions & 21 deletions src/Projection/Projector/MetadataProjectorAccessorRepository.php
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

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

use function array_values;

final class MetadataProjectorAccessorRepository implements ProjectorAccessorRepository
{
private bool $init = false;

/**
* @var array<string, ProjectorAccessor>
*/
/** @var array<string, ProjectorAccessor> */
private array $projectorsMap = [];

/** @param iterable<object> $projectors */
public function __construct(
private readonly iterable $projectors,
private readonly ProjectorMetadataFactory $metadataFactory = new AttributeProjectorMetadataFactory()
private readonly ProjectorMetadataFactory $metadataFactory = new AttributeProjectorMetadataFactory(),
) {
}

/**
* @return iterable<ProjectorAccessor>
*/
/** @return iterable<ProjectorAccessor> */
public function all(): iterable
{
if ($this->init === false) {
$this->init();
}

return array_values($this->projectorsMap);
return array_values($this->projectorAccessorMap());
}

public function get(string $id): ProjectorAccessor|null
{
if ($this->init === false) {
$this->init();
}
$map = $this->projectorAccessorMap();

return $this->projectorsMap[$id] ?? null;
return $map[$id] ?? null;
}

private function init(): void
/** @return array<string, ProjectorAccessor> */
private function projectorAccessorMap(): array
{
$this->init = true;
if ($this->projectorsMap !== []) {
return $this->projectorsMap;
}

foreach ($this->projectors as $projector) {
$metadata = $this->metadataFactory->metadata($projector::class);
$this->projectorsMap[$metadata->id] = new MetadataProjectorAccessor($projector, $metadata);
}

return $this->projectorsMap;
}
}
}
10 changes: 7 additions & 3 deletions src/Projection/Projector/ProjectorAccessor.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

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

interface ProjectorAccessor
Expand All @@ -12,14 +15,15 @@ public function id(): string;
public function group(): string;

public function runMode(): RunMode;

public function setupMethod(): Closure|null;

public function teardownMethod(): Closure|null;

/**
* @param class-string $eventClass
*
* @return iterable<Closure>
* @return list<Closure(Message):void>
*/
public function subscribeMethods(string $eventClass): iterable;
}
public function subscribeMethods(string $eventClass): array;
}
8 changes: 4 additions & 4 deletions src/Projection/Projector/ProjectorAccessorRepository.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

interface ProjectorAccessorRepository
{
/**
* @return iterable<ProjectorAccessor>
*/
/** @return iterable<ProjectorAccessor> */
public function all(): iterable;

public function get(string $id): ProjectorAccessor|null;
}
}
38 changes: 32 additions & 6 deletions src/Projection/Projector/TraceableProjectorAccessor.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

use Closure;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Projection\Projection\RunMode;
use Patchlevel\EventSourcing\Repository\MessageDecorator\Trace;
use Patchlevel\EventSourcing\Repository\MessageDecorator\TraceStack;

use function array_map;

/** @experimental */
final class TraceableProjectorAccessor implements ProjectorAccessor
{
public function __construct(
private readonly ProjectorAccessor $parent,
private readonly Closure $wrapper
private readonly TraceStack $traceStack,
) {
}

Expand Down Expand Up @@ -41,13 +49,31 @@ public function teardownMethod(): Closure|null
/**
* @param class-string $eventClass
*
* @return iterable<Closure>
* @return list<Closure(Message):void>
*/
public function subscribeMethods(string $eventClass): iterable
public function subscribeMethods(string $eventClass): array
{
return array_map(
fn(Closure $closure) => ($this->wrapper)($this, $closure),
$this->parent->subscribeMethods($eventClass)
/**
* @param Closure(Message):void $closure
*
* @return Closure(Message):void
*/
fn (Closure $closure) => function (Message $message) use ($closure): void {
$trace = new Trace(
$this->id(),
'event_sourcing/projector/' . $this->group(),
);

$this->traceStack->add($trace);

try {
$closure($message);
} finally {
$this->traceStack->remove($trace);
}
},
$this->parent->subscribeMethods($eventClass),
);
}
}
}
60 changes: 19 additions & 41 deletions src/Projection/Projector/TraceableProjectorAccessorRepository.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projector;

use Closure;
use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\Repository\MessageDecorator\Trace;
use Patchlevel\EventSourcing\Repository\MessageDecorator\TraceStack;

use function array_values;

/** @experimental */
final class TraceableProjectorAccessorRepository implements ProjectorAccessorRepository
{
private bool $init = false;

/**
* @var array<string, TraceableProjectorAccessor>
*/
/** @var array<string, TraceableProjectorAccessor> */
private array $projectorsMap = [];

public function __construct(
Expand All @@ -22,53 +20,33 @@ public function __construct(
) {
}

/**
* @return iterable<ProjectorAccessor>
*/
/** @return iterable<TraceableProjectorAccessor> */
public function all(): iterable
{
if ($this->init === false) {
$this->init();
}

return array_values($this->projectorsMap);
return array_values($this->projectorAccessorMap());
}

public function get(string $id): ProjectorAccessor|null
public function get(string $id): TraceableProjectorAccessor|null
{
if ($this->init === false) {
$this->init();
}
$map = $this->projectorAccessorMap();

return $this->projectorsMap[$id] ?? null;
return $map[$id] ?? null;
}

private function init(): void
/** @return array<string, TraceableProjectorAccessor> */
private function projectorAccessorMap(): array
{
$this->init = true;
if ($this->projectorsMap !== []) {
return $this->projectorsMap;
}

foreach ($this->parent->all() as $projectorAccessor) {
$this->projectorsMap[$projectorAccessor->id()] = new TraceableProjectorAccessor(
$projectorAccessor,
$this->wrapper(...)
$this->traceStack,
);
}
}

public function wrapper($projectorAccessor, Closure $closure): Closure
{
return function (Message $message) use ($projectorAccessor, $closure) {
$trace = new Trace(
$projectorAccessor->id(),
'event_sourcing:' . $projectorAccessor->group()
);

$this->traceStack->add($trace);
try {
return $closure($message);
} finally {
$this->traceStack->remove($trace);
}
};
return $this->projectorsMap;
}
}
}
8 changes: 5 additions & 3 deletions src/Repository/MessageDecorator/Trace.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Repository\MessageDecorator;

/** @experimental */
final class Trace
{
public function __construct(
public readonly string $name,
public readonly string $category,
)
{
) {
}
}
}
Loading

0 comments on commit 4c9dd22

Please sign in to comment.