<?php

namespace App\Tests\Shared\Adapter;

use App\Domain\Shared\Domain\Contract\EventDispatcherInterface;

class InMemoryEventDispatcher implements EventDispatcherInterface
{
    /** @var array<object> */
    private array $dispatchedEvents = [];

    public function dispatch(object $event): void
    {
        $this->dispatchedEvents[] = $event;
    }

    /**
     * @return array<object>
     */
    public function getDispatchedEvents(): array
    {
        return $this->dispatchedEvents;
    }

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

    /**
     * @template T of object
     * @param class-string<T> $eventClass
     * @return array<T>
     */
    public function getDispatchedEventsOfType(string $eventClass): array
    {
        return array_filter(
            $this->dispatchedEvents,
            fn(object $event) => $event instanceof $eventClass
        );
    }
}

