<commit_before><?php

namespace LiteCQRS\EventStore;

use LiteCQRS\Bus\MessageHandlerInterface;
use LiteCQRS\Bus\MessageInterface;

/**
 * Event Store Transaction around a command handler.
 *
 * If you want to use EventSourcing then the command handler has to wrap this
 * event store handler, which passes all events from the identity map
 * containing all aggregate roots, to event store. The event store then commits
 * its transaction and publishes all the events to all listeners.
 */
class EventStoreHandler implements MessageHandlerInterface
{
    private $next;
    private $eventStore;
    private $identityMap;

    public function __construct(MessageHandlerInterface $next, EventStoreInterface $eventStore)
    {
        $this->next        = $next;
        $this->eventStore  = $eventStore;
    }

    public function handle(MessageInterface $message)
    {
        $this->eventStore->store($message);
        $this->next->handle($message);
    }

    protected function passEventsToStore()
    {
        if (!$this->identityMap) {
            return;
        }

        foreach ($this->identityMap->all() as $aggregateRoot) {
            $id = $this->identityMap->getAggregateId($aggregateRoot);
            foreach ($aggregateRoot->popAppliedEvents() as $event) {
                $header = $event->getMessageHeader();
                $header->aggregateType = get_class($aggregateRoot);
                $header->aggregateId   = $id;
                $header->setAggregate(null);

                $this->eventStore->add($event);
            }
        }
    }
}
<commit_msg>EventStoreHandler removed some dead code<commit_after><?php

namespace LiteCQRS\EventStore;

use LiteCQRS\Bus\MessageHandlerInterface;
use LiteCQRS\Bus\MessageInterface;

/**
 * Event Store Handler
 *
 * This handler takes care of saving all events into an event store.
 */
class EventStoreHandler implements MessageHandlerInterface
{
    private $next;
    private $eventStore;
    private $identityMap;

    public function __construct(MessageHandlerInterface $next, EventStoreInterface $eventStore)
    {
        $this->next        = $next;
        $this->eventStore  = $eventStore;
    }

    public function handle(MessageInterface $message)
    {
        $this->eventStore->store($message);
        $this->next->handle($message);
    }
}

{'fraction_non_alphanumeric': 0.1005586592178771, 'fraction_numerical': 0.0, 'mean_word_length': 2.899497487437186, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4617791', 'n_tokens_mistral': 639, 'n_tokens_neox': 621, 'n_words': 163}