<?php

namespace KupShop\MessengerBundle\Handler;

use KupShop\MessengerBundle\Message\AsyncMessage;
use KupShop\MessengerBundle\Message\EmailMessage;
use KupShop\MessengerBundle\Message\Envelope\EnvelopeInterface;
use KupShop\MessengerBundle\Util\MessengerUtil;
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;

class TestHandler implements MessageSubscriberInterface
{
    private $messengerUtil;

    public function __construct(MessengerUtil $messengerUtil)
    {
        $this->messengerUtil = $messengerUtil;
    }

    public function __invoke($message): array
    {
        if ($message instanceof EnvelopeInterface) {
            $message = unserialize($message->getSerializedMessage());
        }

        return $this->messengerUtil->handleMessage($message, false);
    }

    public static function getHandledMessages(): iterable
    {
        return [
            EnvelopeInterface::class,
            EmailMessage::class,
            AsyncMessage::class,
        ];
    }
}
