Skip to content

Commit

Permalink
Fixes CS
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed May 30, 2022
1 parent 0a418c2 commit e7babb4
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 14 deletions.
6 changes: 6 additions & 0 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function __construct(
) {
}

/**
* @throws CommandNotRegisteredException
* @throws \Throwable
*
* @psalm-suppress InvalidReturnType
*/
public function dispatch(CommandInterface $command): mixed
{
try {
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/CommandNotRegisteredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(CommandInterface $command, \Throwable $previous)
{
parent::__construct(
sprintf("The command <%s> hasn't a command handler associated", get_class($command)),
$previous->getCode(),
(int)$previous->getCode(),
$previous
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/HandlerTypeIsNotSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class HandlerTypeIsNotSupported extends CqrsException
{
public function __construct(Envelope $envelope, ?Throwable $previous = null)
public function __construct(Envelope $envelope, ?\Throwable $previous = null)
{
parent::__construct(
\sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/QueryNotRegisteredException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(QueryInterface $query, \Throwable $previous = null)
{
parent::__construct(
sprintf("The query <%s> hasn't a query handler associated", get_class($query)),
$previous->getCode(),
(int)$previous->getCode(),
$previous
);
}
Expand Down
42 changes: 32 additions & 10 deletions src/HandlersLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

namespace Spiral\Cqrs;

use Generator;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionType;
use Spiral\Attributes\ReaderInterface;
use Spiral\Core\Container;
use Spiral\Cqrs\Attribute\CommandHandler;
Expand Down Expand Up @@ -84,11 +88,12 @@ private function shouldHandle(Envelope $envelope, HandlerDescriptor $handlerDesc
return true;
}

/** @var ReceivedStamp $received */
return $received->getTransportName() === $expectedTransport;
}

/**
* @param array{0: class-string, 1: non-empty-string} $handler
* @param array{0: class-string, 1: non-empty-string} $handler
*/
private function buildHandlerDescriptor(array $handler): HandlerDescriptor
{
Expand All @@ -113,27 +118,27 @@ private function lookForHandlers()
}
}

private function processCommandHandler(\ReflectionMethod $method): void
private function processCommandHandler(ReflectionMethod $method): void
{
$this->assertHandlerMethodIsPublic($method);

foreach ($method->getParameters() as $parameter) {
if (is_a($parameter->getType()->getName(), CommandInterface::class, true)) {
$this->commandHandlers[$parameter->getType()->getName()][] = [
foreach ($this->getMethodParameters($method) as $parameter) {
if (is_a($parameter->getName(), CommandInterface::class, true)) {
$this->commandHandlers[$parameter->getName()][] = [
$method->getDeclaringClass()->getName(),
$method->getName(),
];
}
}
}

private function processQueryHandler(\ReflectionMethod $method)
private function processQueryHandler(ReflectionMethod $method)
{
$this->assertHandlerMethodIsPublic($method);

foreach ($method->getParameters() as $parameter) {
if (is_a($parameter->getType()->getName(), QueryInterface::class, true)) {
$this->queryHandlers[$parameter->getType()->getName()][] = [
foreach ($this->getMethodParameters($method) as $parameter) {
if (is_a($parameter->getName(), QueryInterface::class, true)) {
$this->queryHandlers[$parameter->getName()][] = [
$method->getDeclaringClass()->getName(),
$method->getName(),
];
Expand All @@ -144,7 +149,7 @@ private function processQueryHandler(\ReflectionMethod $method)
/**
* @throws InvalidHandlerException
*/
private function assertHandlerMethodIsPublic(\ReflectionMethod $method): void
private function assertHandlerMethodIsPublic(ReflectionMethod $method): void
{
if (! $method->isPublic()) {
throw new InvalidHandlerException(
Expand All @@ -156,4 +161,21 @@ private function assertHandlerMethodIsPublic(\ReflectionMethod $method): void
);
}
}

/**
* @param ReflectionMethod $method
* @return Generator<int, ReflectionNamedType|ReflectionType|null>
*/
private function getMethodParameters(ReflectionMethod $method): Generator
{
foreach ($method->getParameters() as $parameter) {
if ($parameter->getType() instanceof \ReflectionUnionType) {
foreach ($parameter->getType()->getTypes() as $type) {
yield $type;
}
} else {
yield $parameter->getType();
}
}
}
}
3 changes: 3 additions & 0 deletions src/MessageBusExceptionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

trait MessageBusExceptionTrait
{
/**
* @throws \Throwable
*/
public function throwException(HandlerFailedException $exception): void
{
while ($exception instanceof HandlerFailedException) {
Expand Down
6 changes: 6 additions & 0 deletions src/QueryBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function __construct(
) {
}

/**
* @throws QueryNotRegisteredException
* @throws \Throwable
*
* @psalm-suppress InvalidReturnType
*/
public function ask(QueryInterface $query): mixed
{
try {
Expand Down
16 changes: 16 additions & 0 deletions tests/app/Command/UpdateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Spiral\Cqrs\Tests\App\Command;

use Spiral\Cqrs\CommandInterface;

class UpdateUser implements CommandInterface
{
public function __construct(
public string $username,
public string $password
) {
}
}
3 changes: 2 additions & 1 deletion tests/app/Handler/StoreUserHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Spiral\Cqrs\Tests\App\Handler;

use Spiral\Cqrs\Tests\App\Command\StoreUser;
use Spiral\Cqrs\Tests\App\Command\UpdateUser;
use Spiral\Cqrs\Tests\App\EntityManagerInterface;

final class StoreUserHandler
Expand All @@ -16,7 +17,7 @@ public function __construct(
}

#[\Spiral\Cqrs\Attribute\CommandHandler]
public function __invoke(StoreUser $command)
public function __invoke(StoreUser|UpdateUser $command)
{
$this->entityManager->store([
'uuid' => $command->uuid,
Expand Down

0 comments on commit e7babb4

Please sign in to comment.