<commit_before><?php

namespace Accordia\Cqrs\EventStore;

use Accordia\Cqrs\Aggregate\AggregateRevision;
use Accordia\Cqrs\Aggregate\DomainEventSequence;
use Accordia\MessageBus\Metadata\Metadata;

final class CommitStream implements CommitStreamInterface
{
    /**
     * @var CommitStreamId
     */
    private $streamId;

    /**
     * @var CommitSequence
     */
    private $commitSequence;

    /**
     * @var string
     */
    private $commitImplementor;

    /**
     * @param CommitStreamId $streamId
     * @param string $commitImplementor
     * @return CommitStreamInterface
     */
    public static function fromStreamId(
        CommitStreamId $streamId,
        string $commitImplementor = Commit::class
    ): CommitStreamInterface {
        return new static($streamId);
    }

    /**
     * @param mixed[] $streamState
     * @return CommitStream
     */
    public static function fromArray(array $streamState): CommitStream
    {
        return new static(
            CommitStreamId::fromNative($streamState["commitStreamId"]),
            CommitSequence::fromArray($streamState["commitStreamSequence"]),
            $commitStreamState["commitImplementor"]
        );
    }

    /**
     * @param CommitStreamId $streamId
     * @param CommitSequence|null $commitSequence
     * @param string $commitImplementor
     */
    public function __construct(
        CommitStreamId $streamId,
        CommitSequence $commitSequence = null,
        string $commitImplementor = Commit::class
    ) {
        $this->streamId = $streamId;
        $this->commitSequence = $commitSequence ?? new CommitSequence;
        $this->commitImplementor = $commitImplementor;
    }

    /**
     * @return CommitStreamId
     */
    public function getStreamId(): CommitStreamId
    {
        return $this->streamId;
    }

    /**
     * @return CommitStreamRevision
     */
    public function getStreamRevision(): CommitStreamRevision
    {
        return CommitStreamRevision::fromNative($this->commitSequence->getLength());
    }

    /**
     * @return AggregateRevision
     */
    public function getAggregateRevision(): AggregateRevision
    {
        return $this->commitSequence->getHead()->getAggregateRevision();
    }

    /**
     * @param DomainEventSequence $eventLog
     * @param Metadata $metadata
     * @return CommitStreamInterface
     */
    public function appendEvents(DomainEventSequence $eventLog, Metadata $metadata): CommitStreamInterface
    {
        $previousCommits = $this->findCommitsSince($eventLog->getHeadRevision());
        if (!$previousCommits->isEmpty()) {
            $conflictingEvents = $this->detectConflictingEvents($eventLog, $conflictingCommits);
            // @todo pass $conflictingEvents to an exception and throw?
        }
        return $this->appendCommit(
            $this->commitImplementor::make(
                $this->streamId,
                $this->getStreamRevision()->increment(),
                $eventLog,
                $metadata
            )<commit_msg>fixed typo in local var within the CommitStream.appendEvents mthd<commit_after><?php

namespace Accordia\Cqrs\EventStore;

use Accordia\Cqrs\Aggregate\AggregateRevision;
use Accordia\Cqrs\Aggregate\DomainEventSequence;
use Accordia\MessageBus\Metadata\Metadata;

final class CommitStream implements CommitStreamInterface
{
    /**
     * @var CommitStreamId
     */
    private $streamId;

    /**
     * @var CommitSequence
     */
    private $commitSequence;

    /**
     * @var string
     */
    private $commitImplementor;

    /**
     * @param CommitStreamId $streamId
     * @param string $commitImplementor
     * @return CommitStreamInterface
     */
    public static function fromStreamId(
        CommitStreamId $streamId,
        string $commitImplementor = Commit::class
    ): CommitStreamInterface {
        return new static($streamId);
    }

    /**
     * @param mixed[] $streamState
     * @return CommitStream
     */
    public static function fromArray(array $streamState): CommitStream
    {
        return new static(
            CommitStreamId::fromNative($streamState["commitStreamId"]),
            CommitSequence::fromArray($streamState["commitStreamSequence"]),
            $commitStreamState["commitImplementor"]
        );
    }

    /**
     * @param CommitStreamId $streamId
     * @param CommitSequence|null $commitSequence
     * @param string $commitImplementor
     */
    public function __construct(
        CommitStreamId $streamId,
        CommitSequence $commitSequence = null,
        string $commitImplementor = Commit::class
    ) {
        $this->streamId = $streamId;
        $this->commitSequence = $commitSequence ?? new CommitSequence;
        $this->commitImplementor = $commitImplementor;
    }

    /**
     * @return CommitStreamId
     */
    public function getStreamId(): CommitStreamId
    {
        return $this->streamId;
    }

    /**
     * @return CommitStreamRevision
     */
    public function getStreamRevision(): CommitStreamRevision
    {
        return CommitStreamRevision::fromNative($this->commitSequence->getLength());
    }

    /**
     * @return AggregateRevision
     */
    public function getAggregateRevision(): AggregateRevision
    {
        return $this->commitSequence->getHead()->getAggregateRevision();
    }

    /**
     * @param DomainEventSequence $eventLog
     * @param Metadata $metadata
     * @return CommitStreamInterface
     */
    public function appendEvents(DomainEventSequence $eventLog, Metadata $metadata): CommitStreamInterface
    {
        $previousCommits = $this->findCommitsSince($eventLog->getHeadRevision());
        if (!$previousCommits->isEmpty()) {
            $conflictingEvents = $this->detectConflictingEvents($eventLog, $previousCommits);
            // @todo pass $conflictingEvents to an exception and throw?
        }
        return $this->appendCommit(
            $this->commitImplementor::make(
                $this->streamId,
                $this->getStreamRevision()->increment(),
                $eventLog,
                $metadata
            )
{'fraction_non_alphanumeric': 0.10508252982513483, 'fraction_numerical': 0.0, 'mean_word_length': 2.617021276595745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '4915688', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1620, 'n_words': 314}