<?php
declare(strict_types=1);

namespace App\Framework\CommandBus;

use App\Framework\Core\AttributeMapper;
use ReflectionMethod;
use RuntimeException;

final class CommandHandlerMapper implements AttributeMapper
{
    public function getAttributeClass(): string
    {
        return CommandHandler::class;
    }

    public function map(object $reflectionTarget, object $attributeInstance): ?array
    {
        if (!$reflectionTarget instanceof ReflectionMethod || !$attributeInstance instanceof CommandHandler) {
            return null;
        }

        if(empty($reflectionTarget->getParameters()[0])) {
            throw new RuntimeException('Missing Command-Type');
        }

        return [
            'class' => $reflectionTarget->getDeclaringClass()->getName(),
            'method' => $reflectionTarget->getName(),
            'command' => $reflectionTarget->getParameters()[0]->getType()->getName(),
        ];
    }
}
