<?php

namespace App\Tests\Shared\Adapter;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

class InMemoryMessageBus implements MessageBusInterface
{
    /** @var array<object> */
    public static array $messages = [];

    public function dispatch(object $message, array $stamps = []): Envelope
    {
        self::$messages[] = $message;
        return new Envelope($message);
    }

    public function getDispatchedMessages(): array
    {
        return self::$messages;
    }

    public function clear(): void
    {
        self::$messages = [];
    }

    public function hasMessageOfType(string $messageClass): bool
    {
        foreach (self::$messages as $message) {
            if ($message instanceof $messageClass) {
                return true;
            }
        }
        return false;
    }
}
