Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/event_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ So let's directly jump into it and see what you can do with it.

## Event Hooks

Requirements: an event store implementing \Prooph\EventStore\ActionEventEmitterAware.
Requirements: an event store implementing \Prooph\EventStore\ActionEventEmitterAwareEventStore.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a suffix of EventStore when you're inside the EventStore namespace? In my opinion it isn't very idiomatic.


Action events are triggered when methods of the event store are invoked. The action events are named like the
event store methods and most of them have a suffix to indicate whether they are triggered before or after the
Expand All @@ -23,7 +23,7 @@ logic of the method itself is executed. The following events are available (even
- `hasStream`: event params: `streamName` - result params: `result`
- `fetchStreamMetadata`: event params: `streamName` - result params: `metadata`

If the event store implements additionally \Prooph\EventStore\CanControlTransactionActionEventEmitterAware,
If the event store implements additionally \Prooph\EventStore\CanControlTransactionActionEventEmitterAwareEventStore,
the following additional events are available:

- `beginTransaction`: event params: `inTransaction` - result params: none
Expand Down
26 changes: 13 additions & 13 deletions examples/quickstart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use ArrayIterator;
use Prooph\Common\Event\ActionEvent;
use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\EventStore\ActionEventEmitterAware;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAware;
use Prooph\EventStore\ActionEventEmitterAwareEventStore;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAwareEventStore;
use Prooph\EventStore\InMemoryEventStore;
use Prooph\EventStore\QuickStart\Event\QuickStartSucceeded;
use Prooph\EventStore\Stream;
Expand All @@ -36,16 +36,16 @@
* by your web framework.
*/
$eventEmitter = new ProophActionEventEmitter([
CanControlTransactionActionEventEmitterAware::EVENT_APPEND_TO,
CanControlTransactionActionEventEmitterAware::EVENT_CREATE,
CanControlTransactionActionEventEmitterAware::EVENT_LOAD,
CanControlTransactionActionEventEmitterAware::EVENT_LOAD_REVERSE,
CanControlTransactionActionEventEmitterAware::EVENT_DELETE,
CanControlTransactionActionEventEmitterAware::EVENT_HAS_STREAM,
CanControlTransactionActionEventEmitterAware::EVENT_FETCH_STREAM_METADATA,
CanControlTransactionActionEventEmitterAware::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAware::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAware::EVENT_ROLLBACK,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_APPEND_TO,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_CREATE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_LOAD,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_LOAD_REVERSE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_DELETE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_HAS_STREAM,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_FETCH_STREAM_METADATA,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_ROLLBACK,
]);

$eventStore = new InMemoryEventStore($eventEmitter);
Expand Down Expand Up @@ -86,7 +86,7 @@
* Plugins are simple event listeners. See the docs of prooph/common for more details about event listeners.
*/
$eventStore->getActionEventEmitter()->attachListener(
ActionEventEmitterAware::EVENT_APPEND_TO, // InMemoryEventStore provides event hooks
ActionEventEmitterAwareEventStore::EVENT_APPEND_TO, // InMemoryEventStore provides event hooks
function (ActionEvent $actionEvent): void {
/**
* In the *commit.post* action event a plugin has access to
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractActionEventEmitterAwareEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Prooph\EventStore\Metadata\MetadataMatcher;
use Prooph\EventStore\Util\Assertion;

abstract class AbstractActionEventEmitterAwareEventStore implements EventStore, ActionEventEmitterAware
abstract class AbstractActionEventEmitterAwareEventStore implements ActionEventEmitterAwareEventStore
{
/**
* @var ActionEventEmitter
Expand All @@ -34,7 +34,7 @@ public function getActionEventEmitter(): ActionEventEmitter

public function create(Stream $stream): void
{
$argv = ['stream' => $stream, 'streamEvents' => $stream->streamEvents()];
$argv = ['stream' => $stream];

$event = $this->actionEventEmitter->getNewActionEvent(self::EVENT_CREATE, $this, $argv);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Prooph\EventStore\Exception\TransactionNotStarted;

abstract class AbstractCanControlTransactionActionEventEmitterAwareEventStore extends AbstractActionEventEmitterAwareEventStore implements
CanControlTransactionActionEventEmitterAware
CanControlTransactionActionEventEmitterAwareEventStore
{
/**
* @var bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use Prooph\Common\Event\ActionEventEmitter;

interface ActionEventEmitterAware
interface ActionEventEmitterAwareEventStore extends EventStore
{
const EVENT_APPEND_TO = 'appendTo';
const EVENT_CREATE = 'create';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

namespace Prooph\EventStore;

interface CanControlTransactionActionEventEmitterAware extends CanControlTransaction, ActionEventEmitterAware
interface CanControlTransactionActionEventEmitterAwareEventStore extends
ActionEventEmitterAwareEventStore,
CanControlTransactionEventStore
{
const EVENT_BEGIN_TRANSACTION = 'beginTransaction';
const EVENT_COMMIT = 'commit';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* This interfaces describes that an event store implementation allows control of the transaction handling
*/
interface CanControlTransaction
interface CanControlTransactionEventStore extends EventStore
{
public function beginTransaction(): void;

Expand Down
22 changes: 11 additions & 11 deletions src/Container/InMemoryEventStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Interop\Config\RequiresConfigId;
use Interop\Container\ContainerInterface;
use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAware;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAwareEventStore;
use Prooph\EventStore\Exception\ConfigurationException;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\InMemoryEventStore;
Expand Down Expand Up @@ -79,16 +79,16 @@ public function __invoke(ContainerInterface $container): InMemoryEventStore

if (! isset($config['event_emitter'])) {
$eventEmitter = new ProophActionEventEmitter([
CanControlTransactionActionEventEmitterAware::EVENT_APPEND_TO,
CanControlTransactionActionEventEmitterAware::EVENT_CREATE,
CanControlTransactionActionEventEmitterAware::EVENT_LOAD,
CanControlTransactionActionEventEmitterAware::EVENT_LOAD_REVERSE,
CanControlTransactionActionEventEmitterAware::EVENT_DELETE,
CanControlTransactionActionEventEmitterAware::EVENT_HAS_STREAM,
CanControlTransactionActionEventEmitterAware::EVENT_FETCH_STREAM_METADATA,
CanControlTransactionActionEventEmitterAware::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAware::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAware::EVENT_ROLLBACK,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_APPEND_TO,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_CREATE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_LOAD,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_LOAD_REVERSE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_DELETE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_HAS_STREAM,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_FETCH_STREAM_METADATA,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_ROLLBACK,
]);
} else {
$eventEmitter = $container->get($config['event_emitter']);
Expand Down
10 changes: 5 additions & 5 deletions src/Metadata/MetadataEnricherPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace Prooph\EventStore\Metadata;

use Prooph\Common\Event\ActionEvent;
use Prooph\EventStore\ActionEventEmitterAware;
use Prooph\EventStore\ActionEventEmitterAwareEventStore;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\Plugin\Plugin;
Expand All @@ -33,18 +33,18 @@ public function __construct(MetadataEnricher $metadataEnricher)

public function setUp(EventStore $eventStore): void
{
if (! $eventStore instanceof ActionEventEmitterAware) {
if (! $eventStore instanceof ActionEventEmitterAwareEventStore) {
throw new InvalidArgumentException(
sprintf(
'MetadataEnricherPlugin expects an EventStore implementing %s',
ActionEventEmitterAware::class
ActionEventEmitterAwareEventStore::class
)
);
}
$eventEmitter = $eventStore->getActionEventEmitter();

$eventEmitter->attachListener(ActionEventEmitterAware::EVENT_CREATE, [$this, 'onEventStoreCreateStream'], -1000);
$eventEmitter->attachListener(ActionEventEmitterAware::EVENT_APPEND_TO, [$this, 'onEventStoreAppendToStream'], -1000);
$eventEmitter->attachListener(ActionEventEmitterAwareEventStore::EVENT_CREATE, [$this, 'onEventStoreCreateStream'], -1000);
$eventEmitter->attachListener(ActionEventEmitterAwareEventStore::EVENT_APPEND_TO, [$this, 'onEventStoreAppendToStream'], -1000);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions tests/InMemoryEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use ArrayIterator;
use Prooph\Common\Event\ActionEvent;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAware;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAwareEventStore;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\Exception\RuntimeException;
use Prooph\EventStore\Exception\StreamExistsAlready;
Expand Down Expand Up @@ -45,7 +45,9 @@ public function it_creates_a_new_stream_and_records_the_stream_events_and_delete
$this->eventStore->getActionEventEmitter()->attachListener(
'create',
function (ActionEvent $event) use (&$recordedEvents): void {
foreach ($event->getParam('streamEvents', new \ArrayIterator()) as $recordedEvent) {
$stream = $event->getParam('stream');

foreach ($stream->streamEvents() as $recordedEvent) {
$recordedEvents[] = $recordedEvent;
}
},
Expand Down Expand Up @@ -108,7 +110,9 @@ public function it_appends_events_to_stream_and_records_them(): void
$this->eventStore->getActionEventEmitter()->attachListener(
'create',
function (ActionEvent $event) use (&$recordedEvents): void {
foreach ($event->getParam('streamEvents', new \ArrayIterator()) as $recordedEvent) {
$stream = $event->getParam('stream');

foreach ($stream->streamEvents() as $recordedEvent) {
$recordedEvents[] = $recordedEvent;
}
},
Expand Down Expand Up @@ -689,7 +693,7 @@ public function it_throws_exception_when_transaction_already_started_2(): void
$this->expectException(TransactionAlreadyStarted::class);

$this->eventStore->getActionEventEmitter()->attachListener(
CanControlTransactionActionEventEmitterAware::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_BEGIN_TRANSACTION,
function (ActionEvent $event) {
$event->setParam('inTransaction', false);
$event->stopPropagation();
Expand Down Expand Up @@ -723,7 +727,7 @@ public function it_throws_exception_when_transaction_could_not_commit(): void
$this->assertFalse($this->eventStore->hasStream($streamName));

$this->eventStore->getActionEventEmitter()->attachListener(
CanControlTransactionActionEventEmitterAware::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_COMMIT,
function (ActionEvent $event) {
$event->setParam('inTransaction', true);
$event->stopPropagation();
Expand Down Expand Up @@ -757,7 +761,7 @@ public function it_throws_exception_when_transaction_could_not_rollback(): void
$this->assertFalse($this->eventStore->hasStream($streamName));

$this->eventStore->getActionEventEmitter()->attachListener(
CanControlTransactionActionEventEmitterAware::EVENT_ROLLBACK,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_ROLLBACK,
function (ActionEvent $event) {
$event->setParam('inTransaction', true);
$event->stopPropagation();
Expand Down
45 changes: 29 additions & 16 deletions tests/Mock/EventLoggerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

namespace ProophTest\EventStore\Mock;

use Iterator;
use Prooph\Common\Event\ActionEvent;
use Prooph\EventStore\ActionEventEmitterAwareEventStore;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Exception\InvalidArgumentException;
use Prooph\EventStore\Plugin\Plugin;

class EventLoggerPlugin implements Plugin
{
/**
* @var \Iterator
* @var Iterator
*/
protected $loggedStreamEvents;

Expand All @@ -28,27 +31,37 @@ public function __construct()
$this->loggedStreamEvents = new \ArrayIterator();
}

/**
* @param EventStore $eventStore
* @return void
*/
public function setUp(EventStore $eventStore): void
{
$callable = \Closure::fromCallable([$this, 'on']);
if (! $eventStore instanceof ActionEventEmitterAwareEventStore) {
throw new InvalidArgumentException(
sprintf(
'EventStore must implement %s',
ActionEventEmitterAwareEventStore::class
)
);
}

$eventStore->getActionEventEmitter()->attachListener('create', $callable, -10000);
$eventStore->getActionEventEmitter()->attachListener('appendTo', $callable, -10000);
}
$eventStore->getActionEventEmitter()->attachListener(
ActionEventEmitterAwareEventStore::EVENT_CREATE,
function (ActionEvent $event): void {
$stream = $event->getParam('stream');

/**
* @param ActionEvent $e
*/
private function on(ActionEvent $e): void
{
$this->loggedStreamEvents = $e->getParam('streamEvents', new \ArrayIterator());
$this->loggedStreamEvents = $stream->streamEvents();
},
-10000
);

$eventStore->getActionEventEmitter()->attachListener(
ActionEventEmitterAwareEventStore::EVENT_APPEND_TO,
function (ActionEvent $event): void {
$this->loggedStreamEvents = $event->getParam('streamEvents', new \ArrayIterator());
},
-10000
);
}

public function getLoggedStreamEvents(): \Iterator
public function getLoggedStreamEvents(): Iterator
{
return $this->loggedStreamEvents;
}
Expand Down
22 changes: 11 additions & 11 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace ProophTest\EventStore;

use Prooph\Common\Event\ProophActionEventEmitter;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAware;
use Prooph\EventStore\CanControlTransactionActionEventEmitterAwareEventStore;
use Prooph\EventStore\InMemoryEventStore;

abstract class TestCase extends \PHPUnit_Framework_TestCase
Expand All @@ -26,16 +26,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
protected function setUp(): void
{
$eventEmitter = new ProophActionEventEmitter([
CanControlTransactionActionEventEmitterAware::EVENT_APPEND_TO,
CanControlTransactionActionEventEmitterAware::EVENT_CREATE,
CanControlTransactionActionEventEmitterAware::EVENT_LOAD,
CanControlTransactionActionEventEmitterAware::EVENT_LOAD_REVERSE,
CanControlTransactionActionEventEmitterAware::EVENT_DELETE,
CanControlTransactionActionEventEmitterAware::EVENT_HAS_STREAM,
CanControlTransactionActionEventEmitterAware::EVENT_FETCH_STREAM_METADATA,
CanControlTransactionActionEventEmitterAware::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAware::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAware::EVENT_ROLLBACK,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_APPEND_TO,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_CREATE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_LOAD,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_LOAD_REVERSE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_DELETE,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_HAS_STREAM,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_FETCH_STREAM_METADATA,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_BEGIN_TRANSACTION,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_COMMIT,
CanControlTransactionActionEventEmitterAwareEventStore::EVENT_ROLLBACK,
]);

$this->eventStore = new InMemoryEventStore($eventEmitter);
Expand Down