Skip to content

Commit

Permalink
allow to get the index of the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Aug 22, 2023
1 parent 73064b5 commit 687f780
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Store/ArrayStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function position(): int
return $this->position;
}

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

public function current(): Message|null
{
return $this->iterator->current() ?: null;
Expand Down
15 changes: 14 additions & 1 deletion src/Store/DoctrineDbalStoreStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ final class DoctrineDbalStoreStream implements Stream, IteratorAggregate

private int $position;

private int|null $index;

public function __construct(
private readonly Result $result,
EventSerializer $serializer,
Expand All @@ -30,6 +32,7 @@ public function __construct(
) {
$this->generator = $this->buildGenerator($result, $serializer, $aggregateRootRegistry, $platform);
$this->position = 0;
$this->index = null;
}

public function close(): void
Expand All @@ -47,6 +50,15 @@ public function position(): int
return $this->position;
}

public function index(): int
{
if (!$this->index) {
throw new InvalidIndex();
}

return $this->index;
}

/** @return Traversable<Message> */
public function getIterator(): Traversable
{
Expand All @@ -60,9 +72,10 @@ private function buildGenerator(
AggregateRootRegistry $aggregateRootRegistry,
AbstractPlatform $platform,
): Generator {
/** @var array{aggregate: string, aggregate_id: string, playhead: int|string, event: string, payload: string, recorded_on: string, custom_headers: string} $data */
/** @var array{id: string, aggregate: string, aggregate_id: string, playhead: int|string, event: string, payload: string, recorded_on: string, custom_headers: string} $data */
foreach ($result->iterateAssociative() as $data) {
$this->position++;
$this->index = $data['id'];

Check failure on line 78 in src/Store/DoctrineDbalStoreStream.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.2, ubuntu-latest)

InvalidPropertyAssignmentValue

src/Store/DoctrineDbalStoreStream.php:78:28: InvalidPropertyAssignmentValue: $this->index with declared type 'int|null' cannot be assigned type 'string' (see https://psalm.dev/145)

Check failure on line 78 in src/Store/DoctrineDbalStoreStream.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.2, ubuntu-latest)

Property Patchlevel\EventSourcing\Store\DoctrineDbalStoreStream::$index (int|null) does not accept string.

$event = $serializer->deserialize(new SerializedEvent($data['event'], $data['payload']));

Expand Down
15 changes: 15 additions & 0 deletions src/Store/InvalidIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Store;

use RuntimeException;

final class InvalidIndex extends RuntimeException
{
public function __construct()
{
parent::__construct('invalid index');
}
}
2 changes: 2 additions & 0 deletions src/Store/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public function close(): void;
public function current(): Message|null;

public function position(): int;

public function index(): int;
}
4 changes: 4 additions & 0 deletions tests/Unit/Store/ArrayStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ public function testWithMessages(): void

$stream = new ArrayStream($messages);

self::assertSame(0, $stream->index());
self::assertSame(0, $stream->position());
self::assertSame($message, $stream->current());

$array = iterator_to_array($stream);

self::assertSame(1, $stream->index());
self::assertSame(1, $stream->position());
self::assertSame(null, $stream->current());

Expand Down Expand Up @@ -80,10 +82,12 @@ public function testPosition(): void

$stream = new ArrayStream($messages);

self::assertSame(0, $stream->index());
self::assertSame(0, $stream->position());

iterator_to_array($stream);

self::assertSame(3, $stream->index());

Check failure on line 90 in tests/Unit/Store/ArrayStreamTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.2, ubuntu-latest)

DocblockTypeContradiction

tests/Unit/Store/ArrayStreamTest.php:90:15: DocblockTypeContradiction: Docblock-defined type 0 for $stream->index() is never =int(3) (see https://psalm.dev/155)
self::assertSame(3, $stream->position());

Check failure on line 91 in tests/Unit/Store/ArrayStreamTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis by Psalm (locked, 8.2, ubuntu-latest)

DocblockTypeContradiction

tests/Unit/Store/ArrayStreamTest.php:91:15: DocblockTypeContradiction: Docblock-defined type 0 for $stream->position() is never =int(3) (see https://psalm.dev/155)
}
}
2 changes: 2 additions & 0 deletions tests/Unit/Store/DoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function testLoadWithOneEvent(): void
$result->iterateAssociative()->willReturn(new ArrayIterator(
[
[
'id' => 1,
'aggregate' => 'profile',
'aggregate_id' => '1',
'playhead' => '1',
Expand Down Expand Up @@ -136,6 +137,7 @@ public function testLoadWithOneEvent(): void

$message = $stream->current();

self::assertSame(1, $stream->index());
self::assertInstanceOf(Message::class, $message);
self::assertInstanceOf(ProfileCreated::class, $message->event());
self::assertSame('1', $message->aggregateId());
Expand Down

0 comments on commit 687f780

Please sign in to comment.