Skip to content

Commit

Permalink
allow only one handle attribute per method
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Dec 20, 2024
1 parent c6fae49 commit ad0eb7c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Attribute/Handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]
#[Attribute(Attribute::TARGET_METHOD)]
final class Handle
{
/** @param class-string|null $commandClass */
Expand Down
42 changes: 21 additions & 21 deletions src/CommandBus/AggregateHandlerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Patchlevel\EventSourcing\CommandBus\Handler\HandlerFactory;
use ReflectionClass;
use ReflectionMethod;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;

use function array_key_exists;
Expand Down Expand Up @@ -49,9 +50,15 @@ public function handlerForCommand(string $commandClass): HandlerDescriptor
}

if ($method->isStatic()) {
$this->handlers[$commandClass] = new HandlerDescriptor($this->handlerFactory->createHandler($aggregateClass, $method->getName()));
$this->handlers[$commandClass] = new HandlerDescriptor($this->handlerFactory->createHandler(
$aggregateClass,
$method->getName(),
));
} else {
$this->handlers[$commandClass] = new HandlerDescriptor($this->handlerFactory->updateHandler($aggregateClass, $method->getName()));
$this->handlers[$commandClass] = new HandlerDescriptor($this->handlerFactory->updateHandler(
$aggregateClass,
$method->getName(),
));
}

return $this->handlers[$commandClass];
Expand Down Expand Up @@ -105,29 +112,22 @@ private function canHandle(ReflectionMethod $reflectionMethod, string $commandCl
);
}

$guessType = false;

foreach ($handleAttributes as $handleAttribute) {
$handle = $handleAttribute->newInstance();

if (!$handle->commandClass) {
$guessType = true;
continue;
}

if ($handle->commandClass === $commandClass) {
return true;
}
$type = $this->typeResolver->resolve($reflectionType);

return is_a($commandClass, $handle->commandClass, true);
if (!$type instanceof ObjectType) {
throw InvalidHandleMethod::incompatibleType(
$reflectionMethod->getDeclaringClass()->getName(),
$reflectionMethod->getName(),
);
}

if (!$guessType) {
return false;
}
$handle = $handleAttributes[0]->newInstance();
$handleClassName = $handle->commandClass ?: $type->getClassName();

$type = $this->typeResolver->resolve($reflectionType);
if ($handleClassName === $commandClass) {
return true;
}

return $type->isIdentifiedBy($commandClass);
return is_a($commandClass, $handleClassName, true);
}
}

0 comments on commit ad0eb7c

Please sign in to comment.