@@ -18,8 +18,8 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class QueryDispatcher extends SimpleDispatcher |
| 20 | 20 | { |
| 21 | - public function __construct(QueryHandlerProvider $handlerProvider) |
|
| 22 | - { |
|
| 23 | - parent::__construct($handlerProvider); |
|
| 24 | - } |
|
| 21 | + public function __construct(QueryHandlerProvider $handlerProvider) |
|
| 22 | + { |
|
| 23 | + parent::__construct($handlerProvider); |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -18,8 +18,8 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class CommandDispatcher extends SimpleDispatcher |
| 20 | 20 | { |
| 21 | - public function __construct(CommandHandlerProvider $handlerProvider) |
|
| 22 | - { |
|
| 23 | - parent::__construct($handlerProvider); |
|
| 24 | - } |
|
| 21 | + public function __construct(CommandHandlerProvider $handlerProvider) |
|
| 22 | + { |
|
| 23 | + parent::__construct($handlerProvider); |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -16,8 +16,8 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | interface Voter |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Returns `true` when permission is granted, ending the voting process positively. |
|
| 21 | - */ |
|
| 22 | - public function isGranted(object $message, Context $context): bool; |
|
| 19 | + /** |
|
| 20 | + * Returns `true` when permission is granted, ending the voting process positively. |
|
| 21 | + */ |
|
| 22 | + public function isGranted(object $message, Context $context): bool; |
|
| 23 | 23 | } |
@@ -16,30 +16,30 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class VoterWithPermissions implements Voter |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @param array<class-string, string[]> $permissionsByMessage |
|
| 21 | - * Where _key_ is a command class and _value_ the permissions for that message. |
|
| 22 | - */ |
|
| 23 | - public function __construct( |
|
| 24 | - private VoterProvider $voters, |
|
| 25 | - private array $permissionsByMessage, |
|
| 26 | - ) { |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * @param array<class-string, string[]> $permissionsByMessage |
|
| 21 | + * Where _key_ is a command class and _value_ the permissions for that message. |
|
| 22 | + */ |
|
| 23 | + public function __construct( |
|
| 24 | + private VoterProvider $voters, |
|
| 25 | + private array $permissionsByMessage, |
|
| 26 | + ) { |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - public function isGranted(object $message, Context $context): bool |
|
| 30 | - { |
|
| 31 | - $permissions = $this->permissionsByMessage[$message::class] ?? null; |
|
| 29 | + public function isGranted(object $message, Context $context): bool |
|
| 30 | + { |
|
| 31 | + $permissions = $this->permissionsByMessage[$message::class] ?? null; |
|
| 32 | 32 | |
| 33 | - if (!$permissions) { |
|
| 34 | - return true; |
|
| 35 | - } |
|
| 33 | + if (!$permissions) { |
|
| 34 | + return true; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - foreach ($permissions as $permission) { |
|
| 38 | - if ($this->voters->getVoterForPermission($permission)?->isGranted($message, $context)) { |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 41 | - } |
|
| 37 | + foreach ($permissions as $permission) { |
|
| 38 | + if ($this->voters->getVoterForPermission($permission)?->isGranted($message, $context)) { |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - return false; |
|
| 44 | - } |
|
| 43 | + return false; |
|
| 44 | + } |
|
| 45 | 45 | } |
@@ -16,20 +16,20 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class HandlerProviderWithHandlers implements HandlerProvider |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @param array<class-string, callable> $handlers |
|
| 21 | - * Where _key_ is a message class and _value_ a handler for that message class. |
|
| 22 | - */ |
|
| 23 | - public function __construct( |
|
| 24 | - private array $handlers |
|
| 25 | - ) { |
|
| 26 | - } |
|
| 19 | + /** |
|
| 20 | + * @param array<class-string, callable> $handlers |
|
| 21 | + * Where _key_ is a message class and _value_ a handler for that message class. |
|
| 22 | + */ |
|
| 23 | + public function __construct( |
|
| 24 | + private array $handlers |
|
| 25 | + ) { |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - public function getHandlerForMessage(object $message): callable |
|
| 29 | - { |
|
| 30 | - $class = $message::class; |
|
| 28 | + public function getHandlerForMessage(object $message): callable |
|
| 29 | + { |
|
| 30 | + $class = $message::class; |
|
| 31 | 31 | |
| 32 | - return $this->handlers[$class] |
|
| 33 | - ?? throw new HandlerNotFound("No handler for messages of type `$class`."); |
|
| 34 | - } |
|
| 32 | + return $this->handlers[$class] |
|
| 33 | + ?? throw new HandlerNotFound("No handler for messages of type `$class`."); |
|
| 34 | + } |
|
| 35 | 35 | } |
@@ -29,70 +29,70 @@ |
||
| 29 | 29 | */ |
| 30 | 30 | final class MessageBusPass implements CompilerPassInterface |
| 31 | 31 | { |
| 32 | - public const DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER = 'message_bus.message_to_handler'; |
|
| 33 | - public const DEFAULT_TAG_FOR_HANDLER = 'message_bus.handler'; |
|
| 34 | - public const DEFAULT_ATTRIBUTE_FOR_MESSAGE = 'message'; |
|
| 35 | - public const DEFAULT_PARAMETER_FOR_PERMISSIONS_BY_MESSAGE = 'message_bus.permissions_by_message'; |
|
| 36 | - public const DEFAULT_TAG_FOR_PERMISSION = 'message_bus.permission'; |
|
| 37 | - public const DEFAULT_ATTRIBUTE_FOR_PERMISSION = 'permission'; |
|
| 38 | - public const DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER = 'message_bus.permission_to_voter'; |
|
| 39 | - public const DEFAULT_TAG_FOR_VOTER = 'message_bus.voter'; |
|
| 32 | + public const DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER = 'message_bus.message_to_handler'; |
|
| 33 | + public const DEFAULT_TAG_FOR_HANDLER = 'message_bus.handler'; |
|
| 34 | + public const DEFAULT_ATTRIBUTE_FOR_MESSAGE = 'message'; |
|
| 35 | + public const DEFAULT_PARAMETER_FOR_PERMISSIONS_BY_MESSAGE = 'message_bus.permissions_by_message'; |
|
| 36 | + public const DEFAULT_TAG_FOR_PERMISSION = 'message_bus.permission'; |
|
| 37 | + public const DEFAULT_ATTRIBUTE_FOR_PERMISSION = 'permission'; |
|
| 38 | + public const DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER = 'message_bus.permission_to_voter'; |
|
| 39 | + public const DEFAULT_TAG_FOR_VOTER = 'message_bus.voter'; |
|
| 40 | 40 | |
| 41 | - public function __construct( |
|
| 42 | - private string $parameterForMessageToHandler = self::DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER, |
|
| 43 | - private string $tagForHandler = self::DEFAULT_TAG_FOR_HANDLER, |
|
| 44 | - private string $attributeForMessage = self::DEFAULT_ATTRIBUTE_FOR_MESSAGE, |
|
| 45 | - private string $parameterForPermissionsByMessage = self::DEFAULT_PARAMETER_FOR_PERMISSIONS_BY_MESSAGE, |
|
| 46 | - private string $tagForPermission = self::DEFAULT_TAG_FOR_PERMISSION, |
|
| 47 | - private string $attributeForPermission = self::DEFAULT_ATTRIBUTE_FOR_PERMISSION, |
|
| 48 | - private string $parameterForPermissionToVoter = self::DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER, |
|
| 49 | - private string $tagForVoter = self::DEFAULT_TAG_FOR_VOTER, |
|
| 50 | - ) { |
|
| 51 | - } |
|
| 41 | + public function __construct( |
|
| 42 | + private string $parameterForMessageToHandler = self::DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER, |
|
| 43 | + private string $tagForHandler = self::DEFAULT_TAG_FOR_HANDLER, |
|
| 44 | + private string $attributeForMessage = self::DEFAULT_ATTRIBUTE_FOR_MESSAGE, |
|
| 45 | + private string $parameterForPermissionsByMessage = self::DEFAULT_PARAMETER_FOR_PERMISSIONS_BY_MESSAGE, |
|
| 46 | + private string $tagForPermission = self::DEFAULT_TAG_FOR_PERMISSION, |
|
| 47 | + private string $attributeForPermission = self::DEFAULT_ATTRIBUTE_FOR_PERMISSION, |
|
| 48 | + private string $parameterForPermissionToVoter = self::DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER, |
|
| 49 | + private string $tagForVoter = self::DEFAULT_TAG_FOR_VOTER, |
|
| 50 | + ) { |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - public function process(ContainerBuilder $container): void |
|
| 54 | - { |
|
| 55 | - $this->processHandlersAndPermissions($container); |
|
| 56 | - $this->processVoters($container); |
|
| 57 | - } |
|
| 53 | + public function process(ContainerBuilder $container): void |
|
| 54 | + { |
|
| 55 | + $this->processHandlersAndPermissions($container); |
|
| 56 | + $this->processVoters($container); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Builds a map of message class to handler service identifier, |
|
| 61 | - * and another of permissions by message class. |
|
| 62 | - */ |
|
| 63 | - private function processHandlersAndPermissions(ContainerBuilder $container): void |
|
| 64 | - { |
|
| 65 | - $messageToHandler = []; |
|
| 66 | - $permissionsByMessage = []; |
|
| 59 | + /** |
|
| 60 | + * Builds a map of message class to handler service identifier, |
|
| 61 | + * and another of permissions by message class. |
|
| 62 | + */ |
|
| 63 | + private function processHandlersAndPermissions(ContainerBuilder $container): void |
|
| 64 | + { |
|
| 65 | + $messageToHandler = []; |
|
| 66 | + $permissionsByMessage = []; |
|
| 67 | 67 | |
| 68 | - foreach ($container->findTaggedServiceIds($this->tagForHandler) as $id => $hTags) { |
|
| 69 | - $message = $hTags[0][$this->attributeForMessage] |
|
| 70 | - ?? throw new InvalidArgumentException( |
|
| 71 | - "Missing attribute `$this->attributeForMessage` for service `$id`" |
|
| 72 | - ); |
|
| 68 | + foreach ($container->findTaggedServiceIds($this->tagForHandler) as $id => $hTags) { |
|
| 69 | + $message = $hTags[0][$this->attributeForMessage] |
|
| 70 | + ?? throw new InvalidArgumentException( |
|
| 71 | + "Missing attribute `$this->attributeForMessage` for service `$id`" |
|
| 72 | + ); |
|
| 73 | 73 | |
| 74 | - $messageToHandler[$message] = $id; |
|
| 74 | + $messageToHandler[$message] = $id; |
|
| 75 | 75 | |
| 76 | - foreach ($container->findDefinition($id)->getTag($this->tagForPermission) as $pTags) { |
|
| 77 | - $permissionsByMessage[$message][] = $pTags[$this->attributeForPermission]; |
|
| 78 | - } |
|
| 79 | - } |
|
| 76 | + foreach ($container->findDefinition($id)->getTag($this->tagForPermission) as $pTags) { |
|
| 77 | + $permissionsByMessage[$message][] = $pTags[$this->attributeForPermission]; |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - $container->setParameter($this->parameterForMessageToHandler, $messageToHandler); |
|
| 82 | - $container->setParameter($this->parameterForPermissionsByMessage, $permissionsByMessage); |
|
| 83 | - } |
|
| 81 | + $container->setParameter($this->parameterForMessageToHandler, $messageToHandler); |
|
| 82 | + $container->setParameter($this->parameterForPermissionsByMessage, $permissionsByMessage); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * Builds a map of permission to voter service identifier. |
|
| 87 | - */ |
|
| 88 | - private function processVoters(ContainerBuilder $container): void |
|
| 89 | - { |
|
| 90 | - $permissionToVoter = []; |
|
| 85 | + /** |
|
| 86 | + * Builds a map of permission to voter service identifier. |
|
| 87 | + */ |
|
| 88 | + private function processVoters(ContainerBuilder $container): void |
|
| 89 | + { |
|
| 90 | + $permissionToVoter = []; |
|
| 91 | 91 | |
| 92 | - foreach ($container->findTaggedServiceIds($this->tagForVoter) as $id => $tags) { |
|
| 93 | - $permissionToVoter[$tags[0][$this->attributeForPermission]] = $id; |
|
| 94 | - } |
|
| 92 | + foreach ($container->findTaggedServiceIds($this->tagForVoter) as $id => $tags) { |
|
| 93 | + $permissionToVoter[$tags[0][$this->attributeForPermission]] = $id; |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - $container->setParameter($this->parameterForPermissionToVoter, $permissionToVoter); |
|
| 97 | - } |
|
| 96 | + $container->setParameter($this->parameterForPermissionToVoter, $permissionToVoter); |
|
| 97 | + } |
|
| 98 | 98 | } |
@@ -16,15 +16,15 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | interface Dispatcher |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * @param object $message |
|
| 21 | - * The message to dispatch. |
|
| 22 | - * |
|
| 23 | - * @return mixed |
|
| 24 | - * Result type depends on the handler. |
|
| 25 | - * |
|
| 26 | - * @throws HandlerNotFound |
|
| 27 | - * The handler for the message cannot the found. |
|
| 28 | - */ |
|
| 29 | - public function dispatch(object $message); |
|
| 19 | + /** |
|
| 20 | + * @param object $message |
|
| 21 | + * The message to dispatch. |
|
| 22 | + * |
|
| 23 | + * @return mixed |
|
| 24 | + * Result type depends on the handler. |
|
| 25 | + * |
|
| 26 | + * @throws HandlerNotFound |
|
| 27 | + * The handler for the message cannot the found. |
|
| 28 | + */ |
|
| 29 | + public function dispatch(object $message); |
|
| 30 | 30 | } |
@@ -16,13 +16,13 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class DispatcherWithHandlerProvider 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 | } |
@@ -22,25 +22,25 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | final class VoterProviderWithContainer implements VoterProvider |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @param array<string, string> $permissionToVoter |
|
| 27 | - * Where _key_ is a permission and _value_ a voter service identifier. |
|
| 28 | - */ |
|
| 29 | - public function __construct( |
|
| 30 | - private ContainerInterface $container, |
|
| 31 | - private array $permissionToVoter, |
|
| 32 | - ) { |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * @param array<string, string> $permissionToVoter |
|
| 27 | + * Where _key_ is a permission and _value_ a voter service identifier. |
|
| 28 | + */ |
|
| 29 | + public function __construct( |
|
| 30 | + private ContainerInterface $container, |
|
| 31 | + private array $permissionToVoter, |
|
| 32 | + ) { |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - public function getVoterForPermission(string $permission): ?Voter |
|
| 36 | - { |
|
| 37 | - $id = $this->permissionToVoter[$permission] |
|
| 38 | - ?? throw new VoterNotFound($permission); |
|
| 35 | + public function getVoterForPermission(string $permission): ?Voter |
|
| 36 | + { |
|
| 37 | + $id = $this->permissionToVoter[$permission] |
|
| 38 | + ?? throw new VoterNotFound($permission); |
|
| 39 | 39 | |
| 40 | - try { |
|
| 41 | - return $this->container->get($id); // @phpstan-ignore-line |
|
| 42 | - } catch (Throwable $e) { |
|
| 43 | - throw new VoterNotFound($permission, $e); |
|
| 44 | - } |
|
| 45 | - } |
|
| 40 | + try { |
|
| 41 | + return $this->container->get($id); // @phpstan-ignore-line |
|
| 42 | + } catch (Throwable $e) { |
|
| 43 | + throw new VoterNotFound($permission, $e); |
|
| 44 | + } |
|
| 45 | + } |
|
| 46 | 46 | } |