Skip to content

Commit

Permalink
Throw specific exceptions on duplicate playheads
Browse files Browse the repository at this point in the history
This will allow you to handle race conditions within your domain,
without having to bind it to a specific DBAL store.
  • Loading branch information
Reen Lokum committed Jan 20, 2017
1 parent f6130af commit 6700a44
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/Broadway/EventStore/DBALEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Broadway\Domain\DomainEventStream;
use Broadway\Domain\DomainEventStreamInterface;
use Broadway\Domain\DomainMessage;
use Broadway\EventStore\Exception\DuplicatePlayheadException;
use Broadway\EventStore\Exception\InvalidIdentifierException;
use Broadway\EventStore\Management\Criteria;
use Broadway\EventStore\Management\CriteriaNotSupportedException;
Expand All @@ -23,6 +24,7 @@
use Broadway\UuidGenerator\Converter\BinaryUuidConverterInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Version;

Expand Down Expand Up @@ -115,6 +117,10 @@ public function append($id, DomainEventStreamInterface $eventStream)
}

$this->connection->commit();
} catch (UniqueConstraintViolationException $exception) {
$this->connection->rollBack();

throw DuplicatePlayheadException::create($exception);
} catch (DBALException $exception) {
$this->connection->rollBack();

Expand Down
3 changes: 3 additions & 0 deletions src/Broadway/EventStore/EventStoreInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Broadway\EventStore;

use Broadway\Domain\DomainEventStreamInterface;
use Broadway\EventStore\Exception\DuplicatePlayheadException;

/**
* Loads and stores events.
Expand All @@ -28,6 +29,8 @@ public function load($id);
/**
* @param mixed $id
* @param DomainEventStreamInterface $eventStream
*
* @throws DuplicatePlayheadException
*/
public function append($id, DomainEventStreamInterface $eventStream);
}
14 changes: 14 additions & 0 deletions src/Broadway/EventStore/Exception/DuplicatePlayheadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Broadway\EventStore\Exception;

use Broadway\EventStore\EventStoreException;
use Exception;

class DuplicatePlayheadException extends EventStoreException
{
public static function create(Exception $exception)
{
return new self(null, 0, $exception);
}
}
3 changes: 2 additions & 1 deletion src/Broadway/EventStore/InMemoryEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Broadway\Domain\DomainEventStream;
use Broadway\Domain\DomainEventStreamInterface;
use Broadway\EventStore\Exception\DuplicatePlayheadException;
use Broadway\EventStore\Management\Criteria;
use Broadway\EventStore\Management\EventStoreManagementInterface;

Expand Down Expand Up @@ -61,7 +62,7 @@ public function append($id, DomainEventStreamInterface $eventStream)
private function assertPlayhead($events, $playhead)
{
if (isset($events[$playhead])) {
throw new InMemoryEventStoreException(
throw new DuplicatePlayheadException(
sprintf("An event with playhead '%d' is already committed.", $playhead)
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Broadway/EventStore/EventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function it_throws_an_exception_when_requesting_the_stream_of_a_non_exist
/**
* @test
* @dataProvider idDataProvider
* @expectedException \Broadway\EventStore\EventStoreException
* @expectedException Broadway\EventStore\Exception\DuplicatePlayheadException
*/
public function it_throws_an_exception_when_appending_a_duplicate_playhead($id)
{
Expand Down

0 comments on commit 6700a44

Please sign in to comment.