Skip to content

Commit

Permalink
FEATURE: Major rework (2.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaidelich committed Jun 19, 2023
1 parent af07b5c commit 92c3443
Show file tree
Hide file tree
Showing 20 changed files with 610 additions and 423 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"roave/security-advisories": "dev-latest",
"phpstan/phpstan": "^1.10",
"squizlabs/php_codesniffer": "^4.0.x-dev",
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^10.2"
},
"autoload": {
"psr-4": {
Expand All @@ -44,11 +44,13 @@
"test-phpstan": "phpstan",
"test-cs": "phpcs --colors --standard=PSR12 --exclude=Generic.Files.LineLength src",
"test-cs:fix": "phpcbf --colors --standard=PSR12 --exclude=Generic.Files.LineLength src",
"test-unit": "phpunit tests",
"test-unit": "phpunit tests/Unit",
"test-integration": "phpunit tests/Integration",
"test": [
"@test-phpstan",
"@test-cs",
"@test-unit"
"@test-unit",
"@test-integration"
]
}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
failOnWarning="true">
<testsuites>
<testsuite name="default">
<directory>tests/unit</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

Expand Down
58 changes: 0 additions & 58 deletions src/Aggregate/Aggregate.php

This file was deleted.

88 changes: 0 additions & 88 deletions src/Aggregate/AggregateLoader.php

This file was deleted.

34 changes: 0 additions & 34 deletions src/Aggregate/AggregateTrait.php

This file was deleted.

24 changes: 17 additions & 7 deletions src/EventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Wwwision\DCBEventStore;

use Wwwision\DCBEventStore\Exception\ConditionalAppendFailed;
use Wwwision\DCBEventStore\Model\EventId;
use Wwwision\DCBEventStore\Model\Events;
use Wwwision\DCBEventStore\Model\ExpectedLastEventId;
use Wwwision\DCBEventStore\Model\StreamQuery;

/**
Expand All @@ -20,18 +20,28 @@ interface EventStore
public function setup(): void;

/**
* Returns an event stream matching the specified {@see StreamQuery}
* Returns an event stream that contains all events in the order they occurred
*/
public function streamAll(): EventStream;

/**
* Returns an event stream that contains events matching the specified {@see StreamQuery} in the order they occurred
*/
public function stream(StreamQuery $query): EventStream;

/**
* Commits the specified $events by checking if the specified $query and $lastEventId match
* Commits the specified $events without checking any constraints
*/
public function append(Events $events): void;

/**
* Commits the specified $events by checking if the specified $query and $expectedLastEventId match
*
* *NOTE:* If the $lastEventId is specified, the last event matching the given $query has to match.
* If $lastEventId is NULL, _no_ event must match the specified $query
* If {@see $expectedLastEventId->isNone()}, _no_ event must match the specified $query
*
* @param ?EventId $lastEventId If specified, the last event matching the given $query has to match. If NULL, _no_ event must match the specified $query
* @throws ConditionalAppendFailed If specified $query and $lastEventId don't match
* @param ExpectedLastEventId $expectedLastEventId If not NONE, the last event matching the given $query has to match. Otherwise, _no_ event must match the specified $query
* @throws ConditionalAppendFailed If specified $query and $expectedLastEventId don't match
*/
public function append(Events $events, StreamQuery $query, ?EventId $lastEventId): void;
public function conditionalAppend(Events $events, StreamQuery $query, ExpectedLastEventId $expectedLastEventId): void;
}
7 changes: 4 additions & 3 deletions src/Exception/ConditionalAppendFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
use RuntimeException;
use Wwwision\DCBEventStore\EventStore;
use Wwwision\DCBEventStore\Model\EventId;
use Wwwision\DCBEventStore\Model\ExpectedLastEventId;

/**
* An exception that is thrown when a {@see EventStore::append()} call has failed
* An exception that is thrown when a {@see EventStore::conditionalAppend()} call has failed
*/
final class ConditionalAppendFailed extends RuntimeException
{
Expand All @@ -27,8 +28,8 @@ public static function becauseNoEventWhereExpected(): self
return new self('The event store contained events matching the specified query but none were expected');
}

public static function becauseEventIdsDontMatch(EventId $expectedId, EventId $actualId): self
public static function becauseEventIdsDontMatch(ExpectedLastEventId $expectedId, EventId $actualId): self
{
return new self("Expected event id \"$expectedId->value\" does not match the actual id of \"$actualId->value\"");
return new self("Expected event id \"$expectedId\" does not match the actual id of \"$actualId->value\"");
}
}
14 changes: 0 additions & 14 deletions src/Exception/ConstraintException.php

This file was deleted.

28 changes: 19 additions & 9 deletions src/Helper/InMemoryEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Wwwision\DCBEventStore\Helper;

use Wwwision\DCBEventStore\EventStore;
use Wwwision\DCBEventStore\EventStream;
use Wwwision\DCBEventStore\Exception\ConditionalAppendFailed;
use Wwwision\DCBEventStore\Model\EventEnvelope;
use Wwwision\DCBEventStore\Model\EventId;
use Wwwision\DCBEventStore\Model\Events;
use Wwwision\DCBEventStore\Model\ExpectedLastEventId;
use Wwwision\DCBEventStore\Model\SequenceNumber;
use Wwwision\DCBEventStore\Model\StreamQuery;

Expand All @@ -17,6 +18,8 @@
/**
* An in-memory implementation of the {@see EventStore} interface that mostly serves testing or debugging purposes
*
* NOTE: This implementation is not transaction-safe (and obviously not thread-safe), it should never be used in productive code!
*
* Usage:
* $eventStore = InMemoryEventStore::create();
* $eventStore->append($events);
Expand Down Expand Up @@ -44,26 +47,33 @@ public function setup(): void
// In-memory event store does not need any setup
}

public function streamAll(): EventStream
{
return InMemoryEventStream::create(...$this->eventEnvelopes);
}

public function stream(StreamQuery $query): InMemoryEventStream
{
if ($query->matchesNone()) {
return InMemoryEventStream::empty();
}
return InMemoryEventStream::create(...array_filter($this->eventEnvelopes, static fn (EventEnvelope $eventEnvelope) => $query->matches($eventEnvelope->event)));
}

public function append(Events $events, StreamQuery $query, ?EventId $lastEventId): void
public function conditionalAppend(Events $events, StreamQuery $query, ExpectedLastEventId $expectedLastEventId): void
{
$lastEvent = $this->stream($query)->last();
if ($lastEvent === null) {
if ($lastEventId !== null) {
if (!$expectedLastEventId->isNone()) {
throw ConditionalAppendFailed::becauseNoEventMatchedTheQuery();
}
} elseif ($lastEventId === null) {
} elseif ($expectedLastEventId->isNone()) {
throw ConditionalAppendFailed::becauseNoEventWhereExpected();
} elseif (!$lastEvent->event->id->equals($lastEventId)) {
throw ConditionalAppendFailed::becauseEventIdsDontMatch($lastEventId, $lastEvent->event->id);
} elseif (!$expectedLastEventId->matches($lastEvent->event->id)) {
throw ConditionalAppendFailed::becauseEventIdsDontMatch($expectedLastEventId, $lastEvent->event->id);
}
$this->append($events);
}

public function append(Events $events): void
{
$sequenceNumber = SequenceNumber::fromInteger(count($this->eventEnvelopes));
foreach ($events as $event) {
$sequenceNumber = $sequenceNumber->next();
Expand Down
5 changes: 5 additions & 0 deletions src/Model/DomainEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static function none(): self
return new self();
}

public static function single(DomainEvent $domainEvent): self
{
return new self($domainEvent);
}

/**
* @param DomainEvent[] $domainEvents
*/
Expand Down
Loading

0 comments on commit 92c3443

Please sign in to comment.