-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad0eb7c
commit d2ce86a
Showing
24 changed files
with
325 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\CommandBus; | ||
|
||
/** @internal */ | ||
final class AggregateHandler | ||
{ | ||
/** @param class-string $commandClass */ | ||
public function __construct( | ||
public readonly string $method, | ||
public readonly string $commandClass, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\CommandBus; | ||
|
||
use Patchlevel\EventSourcing\Aggregate\AggregateRoot; | ||
use Patchlevel\EventSourcing\Attribute\Handle; | ||
use ReflectionClass; | ||
use Symfony\Component\TypeInfo\Type\ObjectType; | ||
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver; | ||
|
||
use function class_exists; | ||
|
||
/** @internal */ | ||
final class AggregateHandlerFinder | ||
{ | ||
/** @var list<AggregateHandler> */ | ||
private array $createHandlers = []; | ||
|
||
/** @var list<AggregateHandler> */ | ||
private array $updateHandlers = []; | ||
|
||
/** @param class-string<AggregateRoot> $aggregateClass */ | ||
public function __construct(string $aggregateClass) | ||
{ | ||
$typeResolver = TypeResolver::create(); | ||
$reflectionClass = new ReflectionClass($aggregateClass); | ||
|
||
foreach ($reflectionClass->getMethods() as $reflectionMethod) { | ||
$handleAttributes = $reflectionMethod->getAttributes(Handle::class); | ||
|
||
if ($handleAttributes === []) { | ||
continue; | ||
} | ||
|
||
$parameters = $reflectionMethod->getParameters(); | ||
|
||
if ($parameters === []) { | ||
throw InvalidHandleMethod::noParameters( | ||
$reflectionMethod->getDeclaringClass()->getName(), | ||
$reflectionMethod->getName(), | ||
); | ||
} | ||
|
||
$reflectionType = $parameters[0]->getType(); | ||
|
||
if ($reflectionType === null) { | ||
throw InvalidHandleMethod::incompatibleType( | ||
$reflectionMethod->getDeclaringClass()->getName(), | ||
$reflectionMethod->getName(), | ||
); | ||
} | ||
|
||
$type = $typeResolver->resolve($reflectionType); | ||
|
||
if (!$type instanceof ObjectType) { | ||
throw InvalidHandleMethod::incompatibleType( | ||
$reflectionMethod->getDeclaringClass()->getName(), | ||
$reflectionMethod->getName(), | ||
); | ||
} | ||
|
||
$handle = $handleAttributes[0]->newInstance(); | ||
$commandClass = $handle->commandClass ?: $type->getClassName(); | ||
|
||
if (!class_exists($commandClass)) { | ||
throw InvalidHandleMethod::incompatibleType( | ||
$reflectionMethod->getDeclaringClass()->getName(), | ||
$reflectionMethod->getName(), | ||
); | ||
} | ||
|
||
if ($reflectionMethod->isStatic()) { | ||
$this->createHandlers[] = new AggregateHandler( | ||
$reflectionMethod->getName(), | ||
$commandClass, | ||
); | ||
} else { | ||
$this->updateHandlers[] = new AggregateHandler( | ||
$reflectionMethod->getName(), | ||
$commandClass, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** @return iterable<AggregateHandler> */ | ||
public function createHandlers(): iterable | ||
{ | ||
return $this->createHandlers; | ||
} | ||
|
||
/** @return iterable<AggregateHandler> */ | ||
public function updateHandlers(): iterable | ||
{ | ||
return $this->updateHandlers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.