Skip to content

Commit

Permalink
make lookup immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 13, 2025
1 parent c1041b7 commit d675197
Show file tree
Hide file tree
Showing 7 changed files with 335 additions and 50 deletions.
5 changes: 5 additions & 0 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@
<code><![CDATA[SubscriberAccessor|null]]></code>
</DeprecatedInterface>
</file>
<file src="src/Subscription/Lookup/Lookup.php">
<ArgumentTypeCoercion>
<code><![CDATA[$event]]></code>
</ArgumentTypeCoercion>
</file>
<file src="src/Subscription/Store/DoctrineSubscriptionStore.php">
<MixedArgument>
<code><![CDATA[$context]]></code>
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ parameters:
count: 1
path: src/Store/StreamDoctrineDbalStoreStream.php

-
message: '#^Parameter \#1 \$eventClass of method Patchlevel\\EventSourcing\\Metadata\\Event\\EventRegistry\:\:eventName\(\) expects class\-string, string given\.$#'
identifier: argument.type
count: 1
path: src/Subscription/Lookup/Lookup.php

-
message: '#^Strict comparison using \=\=\= between DateTimeImmutable and false will always evaluate to false\.$#'
identifier: identical.alwaysFalse
Expand Down
1 change: 0 additions & 1 deletion src/Metadata/AggregateRoot/AggregateRootRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public function aggregateClass(string $aggregateName): string
return $this->nameToClassMap[$aggregateName];
}

/** @phpstan-assert-if-true class-string<AggregateRoot> $aggregateClass */
public function hasAggregateClass(string $aggregateClass): bool
{
return array_key_exists($aggregateClass, $this->classToNameMap);
Expand Down
1 change: 0 additions & 1 deletion src/Metadata/Event/EventRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function eventClass(string $eventName): string
return $this->nameToClassMap[$eventName];
}

/** @phpstan-assert-if-true class-string $eventClass */
public function hasEventClass(string $eventClass): bool
{
return array_key_exists($eventClass, $this->classToNameMap);
Expand Down
99 changes: 52 additions & 47 deletions src/Subscription/Lookup/Lookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,12 @@ public function __construct(
$this->criteria = new Criteria();
}

/**
* @param class-string|string ...$events
*
* @return $this
*/
/** @param class-string|string ...$events */
public function events(string ...$events): self
{
$this->criteria = $this->criteria->add(
$self = clone $this;

$self->criteria = $self->criteria->add(
new EventsCriterion(
array_values(
array_map(
Expand All @@ -59,78 +57,86 @@ function (string $event): string {
),
);

return $this;
return $self;
}

/** @return $this */
public function stream(string|null $stream = null): self
{
$self = clone $this;

if ($stream === null) {
$this->criteria = $this->criteria->remove(StreamCriterion::class);
$self->criteria = $self->criteria->remove(StreamCriterion::class);
} else {
$this->criteria = $this->criteria->add(new StreamCriterion($stream));
$self->criteria = $self->criteria->add(new StreamCriterion($stream));
}

return $this;
return $self;
}

/** @return $this */
public function currentStream(): self
{
$stream = $this->currentMessage->header(StreamNameHeader::class)->streamName;
$this->criteria = $this->criteria->add(new StreamCriterion($stream));
$self = clone $this;

$stream = $self->currentMessage->header(StreamNameHeader::class)->streamName;
$self->criteria = $self->criteria->add(new StreamCriterion($stream));

return $this;
return $self;
}

/** @return $this */
public function aggregateName(string|null $aggregateName = null): self
{
$self = clone $this;

if ($aggregateName === null) {
$this->criteria = $this->criteria->remove(AggregateNameCriterion::class);
$self->criteria = $self->criteria->remove(AggregateNameCriterion::class);
} else {
$this->criteria = $this->criteria->add(new AggregateNameCriterion($aggregateName));
$self->criteria = $self->criteria->add(new AggregateNameCriterion($aggregateName));
}

return $this;
return $self;
}

/** @return $this */
public function aggregateId(string|null $aggregateId = null): self
{
$self = clone $this;

if ($aggregateId === null) {
$this->criteria = $this->criteria->remove(AggregateIdCriterion::class);
$self->criteria = $self->criteria->remove(AggregateIdCriterion::class);
} else {
$this->criteria = $this->criteria->add(new AggregateIdCriterion($aggregateId));
$self->criteria = $self->criteria->add(new AggregateIdCriterion($aggregateId));
}

return $this;
return $self;
}

public function currentAggregate(): self
{
$aggregateHeader = $this->currentMessage->header(AggregateHeader::class);
$this->criteria = $this->criteria
$self = clone $this;

$aggregateHeader = $self->currentMessage->header(AggregateHeader::class);
$self->criteria = $self->criteria
->add(new AggregateNameCriterion($aggregateHeader->aggregateName))
->add(new AggregateIdCriterion($aggregateHeader->aggregateId));

return $this;
return $self;
}

/** @return $this */
public function forward(): self
{
$this->backwards = false;
$self = clone $this;

return $this;
$self->backwards = false;

return $self;
}

/** @return $this */
public function backwards(): self
{
$this->backwards = true;
$self = clone $this;

$self->backwards = true;

return $this;
return $self;
}

public function fetchAll(): Stream
Expand All @@ -145,7 +151,13 @@ public function fetchAll(): Stream

public function fetchFirst(): Message
{
$stream = $this->fetchAll();
$stream = $this->store->load(
$this->criteria->add(
new ToIndexCriterion($this->currentMessage->header(IndexHeader::class)->index),
),
limit: 1,
backwards: $this->backwards,
);

$message = $stream->current();

Expand All @@ -158,11 +170,13 @@ public function fetchFirst(): Message

public function fetchLast(): Message
{
$stream = $this->fetchAll();

while (!$stream->end()) {
$stream->next();
}
$stream = $this->store->load(
$this->criteria->add(
new ToIndexCriterion($this->currentMessage->header(IndexHeader::class)->index),
),
limit: 1,
backwards: !$this->backwards,
);

$message = $stream->current();

Expand All @@ -172,13 +186,4 @@ public function fetchLast(): Message

return $message;
}

/** @return $this */
public function reset(): self
{
$this->criteria = new Criteria();
$this->backwards = false;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
public function onAdminPromoted(AdminPromoted $event, Lookup $lookup): void
{
$messages = $lookup
->currentAggregate()
->stream('profile')
->events(
ProfileCreated::class,
NameChanged::class,
Expand Down
Loading

0 comments on commit d675197

Please sign in to comment.