CodefyPHP FrameworkCodefyPHP Framework
  • Home
  • Community
    • Forum
    • Github
    • YouTube
  • News
  • Home
  • Community
    • Forum
    • Github
    • YouTube
  • News
home/Knowledge Base/Aggregates/Event Store
Popular Search:installation, codex, mail

Event Store

22 views 0

Written by Joshua
August 14, 2024

A full-blown event store is beyond the scope of this documentation, but Codefy has an InMemoryEventStore we can use. It implements the EventStore interface that consists of two methods: append, getAggregateHistoryFor, and loadFromPlayhead. We won’t be concerning ourselves with the third method in this documentation.

An append/commit should always be transactional. The whole set of DomainEvents should be persisted, or none at all.

The EventStore supports two read operations. One that fetches an aggregates complete history and the other that fetches a history from a version point.

TDD

Let’s test it out:

<?php

use App\Domain\Post\Post;
use App\Domain\Post\ValueObject\PostId;
use App\Domain\Post\ValueObject\Title;
use App\Domain\Post\ValueObject\Content;
use Codefy\Domain\EventSourcing\InMemoryEventStore;

use function expect;
use function it;
use function iterator_to_array;

it('should retrieve all events from the event store based on aggregate id.', function () {
    $postId = new PostId(value: '1cf57c2c-5c82-45a0-8a42-f0b725cfc42f');

    $post = Post::createPost(
        postId: $postId,
        title: new Title(value: 'Second Post Title'),
        content: new Content(value: 'Another short form content.')
    );

    $events = $post->getRecordedEvents();
    $eventStore = new InMemoryEventStore();

    foreach ($events as $event) {
        $eventStore->append(event: $event);
    }

    $iterator = $eventStore->getAggregateHistoryFor(
        aggregateId: PostId::fromString(
            postId: '1cf57c2c-5c82-45a0-8a42-f0b725cfc42f'
        )
    );
    $aggregateHistory = iterator_to_array(iterator: $iterator->getIterator());

    foreach ($aggregateHistory as $event) {
        expect(value: $post->title())->toEqual(expected: $event->title());
        expect(value: $post->aggregateId())->toEqual(expected: $event->aggregateId());
    }
});

Github

An in-memory implementation of EventStore is on Github.

Forum

If you have any questions or issues, please feel free to post to the Documentation Forum.

SLA Support

If you are needing more hands on support, needing consultation, or help with setup, support me on Github at $60 or more. Once you've sponsored me, you will receive an email on the best way to contact me to start your support.

Edit on Github

Last Updated on August 14, 2024 by Joshua

Related Articles
  • Aggregate Repository
  • Event Sourcing
  • Protecting Invariants
  • Value Objects
  • Records Events
  • Identifies Aggregate

Didn't find your answer? Check out the Forum

  Event Sourcing

Aggregate Repository  

  • Copyright 2025 CodefyPHP.com. All Rights Reserved

Popular Search:installation, codex, mail