<commit_before><?php
/*
 * This file is part of prooph/snapshotter.
 * (c) 2014-2015 prooph software GmbH <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Date: 11/02/15 - 03:52 PM
 */

namespace Prooph\Snapshotter;

use Assert\Assertion;
use Prooph\Common\Event\ActionEvent;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Plugin\Plugin;
use Prooph\ServiceBus\CommandBus;

/**
 * Class SnapshotPlugin
 * @package Prooph\Snapshotter
 */
final class SnapshotPlugin implements Plugin
{
    /**
     * @var CommandBus
     */
    private $commandBus;

    /**
     * @var int
     */
    private $versionStep;

    /**
     * @param CommandBus $commandBus
     * @param int $versionStep
     */
    public function __construct(CommandBus $commandBus, $versionStep)
    {
        Assertion::min($versionStep, 1);
        $this->commandBus = $commandBus;
        $this->versionStep = $versionStep;
    }

    /**
     * @param EventStore $eventStore
     * @return void
     */
    public function setUp(EventStore $eventStore)
    {
        $eventStore->getActionEventEmitter()->attachListener('commit.post', [$this, 'onEventStoreCommitPost'], -1000);
    }

    /**
     * Take snapshots on event-store::commit.post
     *
     * @param ActionEvent $actionEvent
     */
    public function onEventStoreCommitPost(ActionEvent $actionEvent)
    {
        $recordedEvents = $actionEvent->getParam('recordedEvents', []);

        $snapshots = [];

        foreach ($recordedEvents as $recordedEvent) {
            if ($recordedEvent->version() % $this->versionStep !== 0) {
                continue;
            }
            $metadata = $recordedEvent->metadata();
            if (!isset($metadata['aggregate_type']) || !isset($metadata['aggregate_id'])) {
                continue;
            }
            $snapshots[$metadata['aggregate_type']][] = $metadata['aggregate_id'];
        }

        foreach ($snapshots as $aggregateType => $aggregateIds) {
            foreach ($aggregateIds as $aggregateId) {
                $command = TakeSnapshot::withData($aggregateType, $aggregateId);
                $this->commandBus->dispatch($command);
            }
        }<commit_msg>fix ->getParam('recordedEvents', new \ArrayIterator())<commit_after><?php
/*
 * This file is part of prooph/snapshotter.
 * (c) 2014-2015 prooph software GmbH <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * Date: 11/02/15 - 03:52 PM
 */

namespace Prooph\Snapshotter;

use Assert\Assertion;
use Prooph\Common\Event\ActionEvent;
use Prooph\EventStore\EventStore;
use Prooph\EventStore\Plugin\Plugin;
use Prooph\ServiceBus\CommandBus;

/**
 * Class SnapshotPlugin
 * @package Prooph\Snapshotter
 */
final class SnapshotPlugin implements Plugin
{
    /**
     * @var CommandBus
     */
    private $commandBus;

    /**
     * @var int
     */
    private $versionStep;

    /**
     * @param CommandBus $commandBus
     * @param int $versionStep
     */
    public function __construct(CommandBus $commandBus, $versionStep)
    {
        Assertion::min($versionStep, 1);
        $this->commandBus = $commandBus;
        $this->versionStep = $versionStep;
    }

    /**
     * @param EventStore $eventStore
     * @return void
     */
    public function setUp(EventStore $eventStore)
    {
        $eventStore->getActionEventEmitter()->attachListener('commit.post', [$this, 'onEventStoreCommitPost'], -1000);
    }

    /**
     * Take snapshots on event-store::commit.post
     *
     * @param ActionEvent $actionEvent
     */
    public function onEventStoreCommitPost(ActionEvent $actionEvent)
    {
        $recordedEvents = $actionEvent->getParam('recordedEvents', new \ArrayIterator());

        $snapshots = [];

        foreach ($recordedEvents as $recordedEvent) {
            if ($recordedEvent->version() % $this->versionStep !== 0) {
                continue;
            }
            $metadata = $recordedEvent->metadata();
            if (!isset($metadata['aggregate_type']) || !isset($metadata['aggregate_id'])) {
                continue;
            }
            $snapshots[$metadata['aggregate_type']][] = $metadata['aggregate_id'];
        }

        foreach ($snapshots as $aggregateType => $aggregateIds) {
            foreach ($aggregateIds as $aggregateId) {
                $command = TakeSnapshot::withData($aggregateType, $aggregateId);
                $this->commandBus->dispatch($command);
            }
        }
{'fraction_non_alphanumeric': 0.12919281540792035, 'fraction_numerical': 0.010387362042847868, 'mean_word_length': 2.594090202177294, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 30, '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': '5487666', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1280, 'n_words': 283}