chore: complete update
This commit is contained in:
58
src/Framework/CommandBus/CommandHandlersCollection.php
Normal file
58
src/Framework/CommandBus/CommandHandlersCollection.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user