<?php

declare(strict_types=1);

namespace App\Framework\EventSourcing;

final class DefaultEventRecorder implements EventRecorder
{
    /** @var object[] */
    private array $recordedEvents = [];

    public function __construct(
        private(set) int $version = 0,
    ) {
    }

    public function record(object $event): void
    {
        $this->recordedEvents[] = $event;
        $this->version++;
    }

    /** @return object[] */
    public function pullRecordedEvents(): array
    {
        $events = $this->recordedEvents;
        $this->clearRecordedEvents();

        return $events;
    }

    public function clearRecordedEvents(): void
    {
        $this->recordedEvents = [];
    }
}
