Skip to content

Commit

Permalink
#11 Refactored and fixed naming
Browse files Browse the repository at this point in the history
  • Loading branch information
awd-studio committed May 31, 2020
1 parent bb3c228 commit 469f7a7
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 207 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
```php
<?php

use AwdStudio\Bus\Handler\InMemoryHandlers;
use AwdStudio\Bus\Handler\InMemoryHandlerLocator;
use AwdStudio\Bus\Middleware\MiddlewareChain;
use AwdStudio\Command\CommandBus;

Expand All @@ -43,11 +43,11 @@ class MyCommand {
// No any of implementation or extending required.
}

$handlers = new InMemoryHandlers();
$handlers->add(MyCommand::class, function (MyCommand $command): void {});
$handlers = new InMemoryHandlerLocator();
$handlers->add(MyCommand::class, static function (MyCommand $command): void {});

$middleware = new InMemoryHandlers();
$middleware->add(MyCommand::class, function (MyCommand $command, callable $next): void {
$middleware = new InMemoryHandlerLocator();
$middleware->add(MyCommand::class, static function (MyCommand $command, callable $next): void {
// Do whatever you need before the handler
$next($command);
// Or after...
Expand Down
30 changes: 0 additions & 30 deletions src/Bus/Handler/ExternalHandlers.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/Bus/Handler/HandlerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace AwdStudio\Bus\Handler;

use AwdStudio\Bus\HandlerLocator;

/**
* @psalm-template TCallback of callable
* @phpstan-template TCallback of callable
*
* @extends HandlerLocator<TCallback>
*/
interface HandlerRegistry extends HandlerLocator
{
/**
* Registers a handler from a PSR-container as a message handler.
*
* @param string $messageId
* @param string $handlerId
*
* @throws \AwdStudio\Bus\Exception\InvalidHandler
*
* @psalm-param class-string $messageId
* @phpstan-param class-string $messageId
*/
public function register(string $messageId, string $handlerId): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace AwdStudio\Bus\Handler;

use AwdStudio\Bus\HandlerLocator;

/**
* @psalm-external-mutation-free
*
* @implements ExternalHandlers<callable(object $message, mixed ...$extraParams): mixed>
* @implements HandlerLocator<callable(object $message, mixed ...$extraParams): mixed>
*/
final class InMemoryHandlers implements ExternalHandlers
final class InMemoryHandlerLocator implements HandlerLocator
{
/**
* @var array
Expand Down Expand Up @@ -43,7 +45,7 @@ public function has(string $messageId): bool
/**
* {@inheritdoc}
*/
public function get(string $messageId): iterable
public function get(string $messageId): \Traversable
{
yield from $this->handlers[$messageId] ?? [];
}
Expand Down
90 changes: 82 additions & 8 deletions src/Bus/Handler/PsrContainerHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,92 @@

namespace AwdStudio\Bus\Handler;

interface PsrContainerHandlerRegistry
use AwdStudio\Bus\Exception\InvalidHandler;
use AwdStudio\Bus\HandlerLocator;
use Psr\Container\ContainerInterface;

/**
* @implements HandlerRegistry<callable(object $message, mixed ...$extraParams): mixed>
*/
final class PsrContainerHandlerRegistry implements HandlerRegistry
{
/** @var \Psr\Container\ContainerInterface */
private $serviceLocator;

/**
* Registers a handler from a PSR-container as a message handler.
* @var \AwdStudio\Bus\HandlerLocator
*
* @param string $messageId
* @param string $handlerId
* @psalm-var HandlerLocator<callable(object $message, mixed ...$extraParams): mixed>
* @phpstan-var HandlerLocator<callable(object $message, mixed ...$extraParams): mixed>
*/
private $dynamicHandlers;

/**
* @var array
*
* @throws \AwdStudio\Bus\Exception\InvalidHandler
* @psalm-var array<class-string, array<array-key, string>>
* @phpstan-var array<class-string, array<array-key, string>>
*/
private $containerHandlers;

/**
* @param \Psr\Container\ContainerInterface $serviceLocator
* @param \AwdStudio\Bus\HandlerLocator|null $dynamicHandlers
*
* @psalm-param class-string $messageId
* @phpstan-param class-string $messageId
* @psalm-param HandlerLocator<callable(object $message, mixed ...$extraParams): mixed>|null $dynamicHandlers
* @phpstan-param HandlerLocator<callable(object $message, mixed ...$extraParams): mixed>|null $dynamicHandlers
*/
public function __construct(ContainerInterface $serviceLocator, ?HandlerLocator $dynamicHandlers = null)
{
$this->serviceLocator = $serviceLocator;
$this->dynamicHandlers = $dynamicHandlers ?? new InMemoryHandlerLocator();
$this->containerHandlers = [];
}

/**
* {@inheritdoc}
*/
public function register(string $messageId, string $handlerId): void;
public function register(string $messageId, string $handlerId): void
{
if (false === $this->serviceLocator->has($handlerId)) {
throw new InvalidHandler(
\sprintf('There is no registered services such a "%s" to handle a "%s" message', $handlerId, $messageId)
);
}

$this->containerHandlers[$messageId][] = $handlerId;
}

/**
* {@inheritdoc}
*/
public function add(string $messageId, callable $handler): void
{
$this->dynamicHandlers->add($messageId, $handler);
}

/**
* {@inheritdoc}
*/
public function has(string $messageId): bool
{
return $this->dynamicHandlers->has($messageId) || !empty($this->containerHandlers[$messageId]);
}

/**
* {@inheritdoc}
*/
public function get(string $messageId): \Traversable
{
if (true === $this->dynamicHandlers->has($messageId)) {
yield from $this->dynamicHandlers->get($messageId);
}

if (false === empty($this->containerHandlers[$messageId])) {
$array_unique = \array_unique($this->containerHandlers[$messageId]);
foreach ($array_unique as $handlerId) {
yield $this->serviceLocator->get($handlerId);
}
}
}
}
94 changes: 0 additions & 94 deletions src/Bus/Handler/PsrContainerHandlers.php

This file was deleted.

24 changes: 19 additions & 5 deletions src/Bus/Handlers.php → src/Bus/HandlerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,22 @@
* @psalm-template TCallback of callable
* @phpstan-template TCallback of callable
*/
interface Handlers
interface HandlerLocator
{
/**
* Assigns a handler to a particular message.
*
* @param string $messageId
* @param callable $handler
*
* @psalm-param class-string $messageId
* @phpstan-param class-string $messageId
*
* @psalm-param TCallback $handler
* @phpstan-param TCallback $handler
*/
public function add(string $messageId, callable $handler): void;

/**
* Checks if there are handlers for particular message.
*
Expand All @@ -27,13 +41,13 @@ public function has(string $messageId): bool;
*
* @param string $messageId
*
* @return iterable<callable>|callable[]
* @return \Traversable<callable>|callable[]
*
* @psalm-param class-string $messageId
* @phpstan-param class-string $messageId
*
* @psalm-return iterable<array-key, TCallback>
* @phpstan-return iterable<array-key, TCallback>
* @psalm-return \Traversable<array-key, TCallback>
* @phpstan-return \Traversable<array-key, TCallback>
*/
public function get(string $messageId): iterable;
public function get(string $messageId): \Traversable;
}
16 changes: 8 additions & 8 deletions src/Bus/Middleware/MiddlewareChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

namespace AwdStudio\Bus\Middleware;

use AwdStudio\Bus\Handlers;
use AwdStudio\Bus\HandlerLocator;
use AwdStudio\Bus\Middleware;

final class MiddlewareChain implements Middleware
{
/**
* @var \AwdStudio\Bus\Handlers
* @var \AwdStudio\Bus\HandlerLocator
*
* @psalm-var Handlers<callable(callable $next, object $message, mixed ...$extraParams): mixed>
* @phpstan-var Handlers<callable(callable $next, object $message, mixed ...$extraParams): mixed>
* @psalm-var HandlerLocator<callable(callable $next, object $message, mixed ...$extraParams): mixed>
* @phpstan-var HandlerLocator<callable(callable $next, object $message, mixed ...$extraParams): mixed>
*/
private $middleware;

/**
* @param \AwdStudio\Bus\Handlers $handlers
* @param \AwdStudio\Bus\HandlerLocator $handlers
*
* @psalm-param Handlers<callable(callable $next, object $message, mixed ...$extraParams): mixed> $handlers
* @phpstan-param Handlers<callable(callable $next, object $message, mixed ...$extraParams): mixed> $handlers
* @psalm-param HandlerLocator<callable(callable $next, object $message, mixed ...$extraParams): mixed> $handlers
* @phpstan-param HandlerLocator<callable(callable $next, object $message, mixed ...$extraParams): mixed> $handlers
*/
public function __construct(Handlers $handlers)
public function __construct(HandlerLocator $handlers)
{
$this->middleware = $handlers;
}
Expand Down
Loading

0 comments on commit 469f7a7

Please sign in to comment.