| @@ -16,13 +16,13 @@ | ||
| 16 | 16 | */ | 
| 17 | 17 | class SimpleDispatcher implements Dispatcher | 
| 18 | 18 |  { | 
| 19 | - public function __construct( | |
| 20 | - private HandlerProvider $handlerProvider | |
| 21 | -    ) { | |
| 22 | - } | |
| 19 | + public function __construct( | |
| 20 | + private HandlerProvider $handlerProvider | |
| 21 | +	) { | |
| 22 | + } | |
| 23 | 23 | |
| 24 | - public function dispatch(object $message) | |
| 25 | -    { | |
| 26 | - return $this->handlerProvider->getHandlerForMessage($message)($message); | |
| 27 | - } | |
| 24 | + public function dispatch(object $message) | |
| 25 | +	{ | |
| 26 | + return $this->handlerProvider->getHandlerForMessage($message)($message); | |
| 27 | + } | |
| 28 | 28 | } | 
| @@ -16,19 +16,19 @@ | ||
| 16 | 16 | */ | 
| 17 | 17 | final class SimpleHandlerProvider implements HandlerProvider | 
| 18 | 18 |  { | 
| 19 | - /** | |
| 20 | - * @param array<string, callable> $handlers | |
| 21 | - */ | |
| 22 | - public function __construct( | |
| 23 | - private array $handlers | |
| 24 | -    ) { | |
| 25 | - } | |
| 19 | + /** | |
| 20 | + * @param array<string, callable> $handlers | |
| 21 | + */ | |
| 22 | + public function __construct( | |
| 23 | + private array $handlers | |
| 24 | +	) { | |
| 25 | + } | |
| 26 | 26 | |
| 27 | - public function getHandlerForMessage(object $message): callable | |
| 28 | -    { | |
| 29 | - $class = get_class($message); | |
| 27 | + public function getHandlerForMessage(object $message): callable | |
| 28 | +	{ | |
| 29 | + $class = get_class($message); | |
| 30 | 30 | |
| 31 | - return $this->handlers[$class] | |
| 32 | -            ?? throw new NotFound("No handler for messages of type `$class`."); | |
| 33 | - } | |
| 31 | + return $this->handlers[$class] | |
| 32 | +			?? throw new NotFound("No handler for messages of type `$class`."); | |
| 33 | + } | |
| 34 | 34 | } | 
| @@ -20,31 +20,31 @@ | ||
| 20 | 20 | |
| 21 | 21 | class ContainerHandlerProvider implements HandlerProvider | 
| 22 | 22 |  { | 
| 23 | - /** | |
| 24 | - * @param array<string, string> $handlers | |
| 25 | - * Where _key_ is a message class and _value_ the service identifier of its handler. | |
| 26 | - */ | |
| 27 | - public function __construct( | |
| 28 | - private ContainerInterface $container, | |
| 29 | - private array $handlers | |
| 30 | -    ) { | |
| 31 | - $this->container = $container; | |
| 32 | - $this->handlers = $handlers; | |
| 33 | - } | |
| 34 | - | |
| 35 | - public function getHandlerForMessage(object $message): callable | |
| 36 | -    { | |
| 37 | - $class = get_class($message); | |
| 38 | - $id = $this->handlers[$class] ?? null; | |
| 39 | - | |
| 40 | -        if (!$id) { | |
| 41 | -            throw new NotFound("No handler for messages of type `$class`."); | |
| 42 | - } | |
| 43 | - | |
| 44 | -        try { | |
| 45 | - return $this->container->get($id); // @phpstan-ignore-line | |
| 46 | -        } catch (NotFoundExceptionInterface $e) { | |
| 47 | -            throw new NotFound("No handler for messages of type `$class`.", $e); | |
| 48 | - } | |
| 49 | - } | |
| 23 | + /** | |
| 24 | + * @param array<string, string> $handlers | |
| 25 | + * Where _key_ is a message class and _value_ the service identifier of its handler. | |
| 26 | + */ | |
| 27 | + public function __construct( | |
| 28 | + private ContainerInterface $container, | |
| 29 | + private array $handlers | |
| 30 | +	) { | |
| 31 | + $this->container = $container; | |
| 32 | + $this->handlers = $handlers; | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function getHandlerForMessage(object $message): callable | |
| 36 | +	{ | |
| 37 | + $class = get_class($message); | |
| 38 | + $id = $this->handlers[$class] ?? null; | |
| 39 | + | |
| 40 | +		if (!$id) { | |
| 41 | +			throw new NotFound("No handler for messages of type `$class`."); | |
| 42 | + } | |
| 43 | + | |
| 44 | +		try { | |
| 45 | + return $this->container->get($id); // @phpstan-ignore-line | |
| 46 | +		} catch (NotFoundExceptionInterface $e) { | |
| 47 | +			throw new NotFound("No handler for messages of type `$class`.", $e); | |
| 48 | + } | |
| 49 | + } | |
| 50 | 50 | } | 
| @@ -27,70 +27,70 @@ | ||
| 27 | 27 | */ | 
| 28 | 28 | class HandlerProviderPass implements CompilerPassInterface | 
| 29 | 29 |  { | 
| 30 | - public const DEFAULT_SERVICE_ID = HandlerProvider::class; | |
| 31 | - public const DEFAULT_HANDLER_TAG = 'message_dispatcher.handler'; | |
| 32 | - public const DEFAULT_MESSAGE_PROPERTY = 'message'; | |
| 33 | - public const DEFAULT_PROVIDER_CLASS = ContainerHandlerProvider::class; | |
| 34 | - | |
| 35 | - public function __construct( | |
| 36 | - private string $serviceId = self::DEFAULT_SERVICE_ID, | |
| 37 | - private string $handlerTag = self::DEFAULT_HANDLER_TAG, | |
| 38 | - private string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, | |
| 39 | - private string $providerClass = self::DEFAULT_PROVIDER_CLASS | |
| 40 | -    ) { | |
| 41 | - } | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * @inheritdoc | |
| 45 | - */ | |
| 46 | - public function process(ContainerBuilder $container): void | |
| 47 | -    { | |
| 48 | - [ $mapping, $refMap ] = $this->collectHandlers($container); | |
| 49 | - | |
| 50 | - $container | |
| 51 | - ->register($this->serviceId, $this->providerClass) | |
| 52 | - ->setArguments([ | |
| 53 | - ServiceLocatorTagPass::register($container, $refMap), | |
| 54 | - $mapping | |
| 55 | - ]); | |
| 56 | - } | |
| 57 | - | |
| 58 | - /** | |
| 59 | -     * @return array{0: array<string, string>, 1: array<string, TypedReference>} | |
| 60 | - */ | |
| 61 | - private function collectHandlers(ContainerBuilder $container): array | |
| 62 | -    { | |
| 63 | - $handlers = $container->findTaggedServiceIds($this->handlerTag, true); | |
| 64 | - $messageProperty = $this->messageProperty; | |
| 65 | - $mapping = []; | |
| 66 | - $refMap = []; | |
| 67 | - | |
| 68 | -        foreach ($handlers as $id => $tags) { | |
| 69 | - assert(is_string($id)); | |
| 70 | - | |
| 71 | - $command = $tags[0][$messageProperty] | |
| 72 | - ?? throw new InvalidArgumentException( | |
| 73 | - "The `$messageProperty` property is required for service `$id`." | |
| 74 | - ); | |
| 75 | - | |
| 76 | - assert(is_string($command)); | |
| 77 | - | |
| 78 | -            if (isset($mapping[$command])) { | |
| 79 | - throw new LogicException( | |
| 80 | -                    "The command `$command` already has an handler: `{$mapping[$command]}`." | |
| 81 | - ); | |
| 82 | - } | |
| 83 | - | |
| 84 | - $class = $container->getDefinition($id)->getClass(); | |
| 85 | - | |
| 86 | -            if (!$class) { | |
| 87 | -                throw new LogicException("Unable to get class of service `$id`."); | |
| 88 | - } | |
| 89 | - | |
| 90 | - $mapping[$command] = $id; | |
| 91 | - $refMap[$id] = new TypedReference($id, $class); | |
| 92 | - } | |
| 93 | - | |
| 94 | - return [ $mapping, $refMap ]; | |
| 95 | - } | |
| 30 | + public const DEFAULT_SERVICE_ID = HandlerProvider::class; | |
| 31 | + public const DEFAULT_HANDLER_TAG = 'message_dispatcher.handler'; | |
| 32 | + public const DEFAULT_MESSAGE_PROPERTY = 'message'; | |
| 33 | + public const DEFAULT_PROVIDER_CLASS = ContainerHandlerProvider::class; | |
| 34 | + | |
| 35 | + public function __construct( | |
| 36 | + private string $serviceId = self::DEFAULT_SERVICE_ID, | |
| 37 | + private string $handlerTag = self::DEFAULT_HANDLER_TAG, | |
| 38 | + private string $messageProperty = self::DEFAULT_MESSAGE_PROPERTY, | |
| 39 | + private string $providerClass = self::DEFAULT_PROVIDER_CLASS | |
| 40 | +	) { | |
| 41 | + } | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @inheritdoc | |
| 45 | + */ | |
| 46 | + public function process(ContainerBuilder $container): void | |
| 47 | +	{ | |
| 48 | + [ $mapping, $refMap ] = $this->collectHandlers($container); | |
| 49 | + | |
| 50 | + $container | |
| 51 | + ->register($this->serviceId, $this->providerClass) | |
| 52 | + ->setArguments([ | |
| 53 | + ServiceLocatorTagPass::register($container, $refMap), | |
| 54 | + $mapping | |
| 55 | + ]); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | +	 * @return array{0: array<string, string>, 1: array<string, TypedReference>} | |
| 60 | + */ | |
| 61 | + private function collectHandlers(ContainerBuilder $container): array | |
| 62 | +	{ | |
| 63 | + $handlers = $container->findTaggedServiceIds($this->handlerTag, true); | |
| 64 | + $messageProperty = $this->messageProperty; | |
| 65 | + $mapping = []; | |
| 66 | + $refMap = []; | |
| 67 | + | |
| 68 | +		foreach ($handlers as $id => $tags) { | |
| 69 | + assert(is_string($id)); | |
| 70 | + | |
| 71 | + $command = $tags[0][$messageProperty] | |
| 72 | + ?? throw new InvalidArgumentException( | |
| 73 | + "The `$messageProperty` property is required for service `$id`." | |
| 74 | + ); | |
| 75 | + | |
| 76 | + assert(is_string($command)); | |
| 77 | + | |
| 78 | +			if (isset($mapping[$command])) { | |
| 79 | + throw new LogicException( | |
| 80 | +					"The command `$command` already has an handler: `{$mapping[$command]}`." | |
| 81 | + ); | |
| 82 | + } | |
| 83 | + | |
| 84 | + $class = $container->getDefinition($id)->getClass(); | |
| 85 | + | |
| 86 | +			if (!$class) { | |
| 87 | +				throw new LogicException("Unable to get class of service `$id`."); | |
| 88 | + } | |
| 89 | + | |
| 90 | + $mapping[$command] = $id; | |
| 91 | + $refMap[$id] = new TypedReference($id, $class); | |
| 92 | + } | |
| 93 | + | |
| 94 | + return [ $mapping, $refMap ]; | |
| 95 | + } | |
| 96 | 96 | } | 
| @@ -16,32 +16,32 @@ | ||
| 16 | 16 | */ | 
| 17 | 17 | class AssertingDispatcher implements Dispatcher | 
| 18 | 18 |  { | 
| 19 | - /** | |
| 20 | - * @var callable | |
| 21 | - */ | |
| 22 | - private $assertion; | |
| 19 | + /** | |
| 20 | + * @var callable | |
| 21 | + */ | |
| 22 | + private $assertion; | |
| 23 | 23 | |
| 24 | - /** | |
| 25 | - * @param callable $assertion | |
| 26 | - * A callable that should throw an exception if the message shouldn't be dispatched. | |
| 27 | - */ | |
| 28 | - public function __construct( | |
| 29 | - private Dispatcher $dispatcher, | |
| 30 | - callable $assertion | |
| 31 | -    ) { | |
| 32 | - $this->dispatcher = $dispatcher; | |
| 33 | - $this->assertion = $assertion; | |
| 34 | - } | |
| 24 | + /** | |
| 25 | + * @param callable $assertion | |
| 26 | + * A callable that should throw an exception if the message shouldn't be dispatched. | |
| 27 | + */ | |
| 28 | + public function __construct( | |
| 29 | + private Dispatcher $dispatcher, | |
| 30 | + callable $assertion | |
| 31 | +	) { | |
| 32 | + $this->dispatcher = $dispatcher; | |
| 33 | + $this->assertion = $assertion; | |
| 34 | + } | |
| 35 | 35 | |
| 36 | - /** | |
| 37 | - * @param object $message | |
| 38 | - * | |
| 39 | - * @return mixed | |
| 40 | - */ | |
| 41 | - public function dispatch(object $message) | |
| 42 | -    { | |
| 43 | - ($this->assertion)($message); | |
| 36 | + /** | |
| 37 | + * @param object $message | |
| 38 | + * | |
| 39 | + * @return mixed | |
| 40 | + */ | |
| 41 | + public function dispatch(object $message) | |
| 42 | +	{ | |
| 43 | + ($this->assertion)($message); | |
| 44 | 44 | |
| 45 | - return $this->dispatcher->dispatch($message); | |
| 46 | - } | |
| 45 | + return $this->dispatcher->dispatch($message); | |
| 46 | + } | |
| 47 | 47 | } |