59 lines
1.3 KiB
PHP
59 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Framework\CommandBus;
|
|
|
|
use ArrayIterator;
|
|
use Countable;
|
|
use IteratorAggregate;
|
|
use Traversable;
|
|
|
|
final readonly class CommandHandlersCollection implements IteratorAggregate, Countable
|
|
{
|
|
private array $handlers;
|
|
public function __construct(
|
|
CommandHandlerDescriptor ...$handlers
|
|
#private array $handlers = []
|
|
){
|
|
$handlerArray = [];
|
|
foreach ($handlers as $handler) {
|
|
|
|
$handlerArray[$handler->command] = $handler;
|
|
}
|
|
$this->handlers = $handlerArray;
|
|
}
|
|
|
|
/*private function add(string $messageClass, object $handler): void
|
|
{
|
|
$this->handlers[$messageClass] = $handler;
|
|
}*/
|
|
|
|
/**
|
|
* @param class-string $commandClass
|
|
* @return CommandHandlerDescriptor|null
|
|
*/
|
|
public function get(string $commandClass): ?CommandHandlerDescriptor
|
|
{
|
|
return $this->handlers[$commandClass] ?? null;
|
|
}
|
|
|
|
public function all(): array
|
|
{
|
|
return $this->toArray();
|
|
}
|
|
|
|
public function getIterator(): Traversable
|
|
{
|
|
return new ArrayIterator($this->handlers);
|
|
}
|
|
|
|
public function count(): int
|
|
{
|
|
return count($this->handlers);
|
|
}
|
|
|
|
public function toArray(): array
|
|
{
|
|
return $this->handlers;
|
|
}
|
|
}
|