@@ -16,19 +16,19 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | final class RestrictedDispatcherWithVoter implements RestrictedDispatcher |
| 18 | 18 | { |
| 19 | - public function __construct( |
|
| 20 | - private Dispatcher $innerDispatcher, |
|
| 21 | - private Voter $voter, |
|
| 22 | - ) { |
|
| 23 | - } |
|
| 19 | + public function __construct( |
|
| 20 | + private Dispatcher $innerDispatcher, |
|
| 21 | + private Voter $voter, |
|
| 22 | + ) { |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * @throws PermissionNotGranted |
|
| 27 | - */ |
|
| 28 | - public function dispatch(object $message, Context $context): mixed |
|
| 29 | - { |
|
| 30 | - $this->voter->isGranted($message, $context) or throw new PermissionNotGranted($message, $context); |
|
| 25 | + /** |
|
| 26 | + * @throws PermissionNotGranted |
|
| 27 | + */ |
|
| 28 | + public function dispatch(object $message, Context $context): mixed |
|
| 29 | + { |
|
| 30 | + $this->voter->isGranted($message, $context) or throw new PermissionNotGranted($message, $context); |
|
| 31 | 31 | |
| 32 | - return $this->innerDispatcher->dispatch($message); |
|
| 33 | - } |
|
| 32 | + return $this->innerDispatcher->dispatch($message); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -19,13 +19,13 @@ |
||
| 19 | 19 | */ |
| 20 | 20 | class NotInContext extends LogicException implements Exception |
| 21 | 21 | { |
| 22 | - /** |
|
| 23 | - * @param class-string $class |
|
| 24 | - */ |
|
| 25 | - public function __construct( |
|
| 26 | - public string $class, |
|
| 27 | - ?Throwable $previous = null |
|
| 28 | - ) { |
|
| 29 | - parent::__construct("Unable to find object matching: $class", 0, $previous); |
|
| 30 | - } |
|
| 22 | + /** |
|
| 23 | + * @param class-string $class |
|
| 24 | + */ |
|
| 25 | + public function __construct( |
|
| 26 | + public string $class, |
|
| 27 | + ?Throwable $previous = null |
|
| 28 | + ) { |
|
| 29 | + parent::__construct("Unable to find object matching: $class", 0, $previous); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -13,8 +13,8 @@ |
||
| 13 | 13 | |
| 14 | 14 | interface RestrictedDispatcher |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * @throws PermissionNotGranted |
|
| 18 | - */ |
|
| 19 | - public function dispatch(object $message, Context $context): mixed; |
|
| 16 | + /** |
|
| 17 | + * @throws PermissionNotGranted |
|
| 18 | + */ |
|
| 19 | + public function dispatch(object $message, Context $context): mixed; |
|
| 20 | 20 | } |
@@ -28,69 +28,69 @@ |
||
| 28 | 28 | */ |
| 29 | 29 | final class MessageBusPass implements CompilerPassInterface |
| 30 | 30 | { |
| 31 | - public const DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER = 'message_bus.message_to_handler'; |
|
| 32 | - public const DEFAULT_TAG_FOR_HANDLER = 'message_bus.handler'; |
|
| 33 | - public const DEFAULT_ATTRIBUTE_FOR_MESSAGE = 'message'; |
|
| 34 | - public const DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER = 'message_bus.permissions_by_message'; |
|
| 35 | - public const DEFAULT_TAG_FOR_PERMISSION = 'message_bus.permission'; |
|
| 36 | - public const DEFAULT_ATTRIBUTE_FOR_PERMISSION = 'permission'; |
|
| 37 | - public const DEFAULT_PARAMETER_FOR_VOTERS = 'message_bus.permission_to_voter'; |
|
| 38 | - public const DEFAULT_TAG_FOR_VOTER = 'message_bus.voter'; |
|
| 31 | + public const DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER = 'message_bus.message_to_handler'; |
|
| 32 | + public const DEFAULT_TAG_FOR_HANDLER = 'message_bus.handler'; |
|
| 33 | + public const DEFAULT_ATTRIBUTE_FOR_MESSAGE = 'message'; |
|
| 34 | + public const DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER = 'message_bus.permissions_by_message'; |
|
| 35 | + public const DEFAULT_TAG_FOR_PERMISSION = 'message_bus.permission'; |
|
| 36 | + public const DEFAULT_ATTRIBUTE_FOR_PERMISSION = 'permission'; |
|
| 37 | + public const DEFAULT_PARAMETER_FOR_VOTERS = 'message_bus.permission_to_voter'; |
|
| 38 | + public const DEFAULT_TAG_FOR_VOTER = 'message_bus.voter'; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @param string $parameterForMessageToHandler |
|
| 42 | - * @param string $tagForHandler |
|
| 43 | - * @param string $attributeForMessage |
|
| 44 | - * @param string $parameterForPermissionToVoter |
|
| 45 | - * @param string $tagForPermission |
|
| 46 | - * @param string $attributeForPermission |
|
| 47 | - * @param string $parameterForVoter |
|
| 48 | - * @param string $tagForVoter |
|
| 49 | - */ |
|
| 50 | - public function __construct( |
|
| 51 | - private string $parameterForMessageToHandler = self::DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER, |
|
| 52 | - private string $tagForHandler = self::DEFAULT_TAG_FOR_HANDLER, |
|
| 53 | - private string $attributeForMessage = self::DEFAULT_ATTRIBUTE_FOR_MESSAGE, |
|
| 54 | - private string $parameterForPermissionToVoter = self::DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER, |
|
| 55 | - private string $tagForPermission = self::DEFAULT_TAG_FOR_PERMISSION, |
|
| 56 | - private string $attributeForPermission = self::DEFAULT_ATTRIBUTE_FOR_PERMISSION, |
|
| 57 | - private string $parameterForVoter = self::DEFAULT_PARAMETER_FOR_VOTERS, |
|
| 58 | - private string $tagForVoter = self::DEFAULT_TAG_FOR_VOTER, |
|
| 59 | - ) { |
|
| 60 | - } |
|
| 40 | + /** |
|
| 41 | + * @param string $parameterForMessageToHandler |
|
| 42 | + * @param string $tagForHandler |
|
| 43 | + * @param string $attributeForMessage |
|
| 44 | + * @param string $parameterForPermissionToVoter |
|
| 45 | + * @param string $tagForPermission |
|
| 46 | + * @param string $attributeForPermission |
|
| 47 | + * @param string $parameterForVoter |
|
| 48 | + * @param string $tagForVoter |
|
| 49 | + */ |
|
| 50 | + public function __construct( |
|
| 51 | + private string $parameterForMessageToHandler = self::DEFAULT_PARAMETER_FOR_MESSAGE_TO_HANDLER, |
|
| 52 | + private string $tagForHandler = self::DEFAULT_TAG_FOR_HANDLER, |
|
| 53 | + private string $attributeForMessage = self::DEFAULT_ATTRIBUTE_FOR_MESSAGE, |
|
| 54 | + private string $parameterForPermissionToVoter = self::DEFAULT_PARAMETER_FOR_PERMISSION_TO_VOTER, |
|
| 55 | + private string $tagForPermission = self::DEFAULT_TAG_FOR_PERMISSION, |
|
| 56 | + private string $attributeForPermission = self::DEFAULT_ATTRIBUTE_FOR_PERMISSION, |
|
| 57 | + private string $parameterForVoter = self::DEFAULT_PARAMETER_FOR_VOTERS, |
|
| 58 | + private string $tagForVoter = self::DEFAULT_TAG_FOR_VOTER, |
|
| 59 | + ) { |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - public function process(ContainerBuilder $container): void |
|
| 63 | - { |
|
| 64 | - $this->processHandlersAndPermissions($container); |
|
| 65 | - $this->processVoters($container); |
|
| 66 | - } |
|
| 62 | + public function process(ContainerBuilder $container): void |
|
| 63 | + { |
|
| 64 | + $this->processHandlersAndPermissions($container); |
|
| 65 | + $this->processVoters($container); |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - public function processHandlersAndPermissions(ContainerBuilder $container): void |
|
| 69 | - { |
|
| 70 | - $messageToHandler = []; |
|
| 71 | - $permissionsByMessage = []; |
|
| 68 | + public function processHandlersAndPermissions(ContainerBuilder $container): void |
|
| 69 | + { |
|
| 70 | + $messageToHandler = []; |
|
| 71 | + $permissionsByMessage = []; |
|
| 72 | 72 | |
| 73 | - foreach ($container->findTaggedServiceIds($this->tagForHandler) as $id => $hTags) { |
|
| 74 | - $message = $hTags[0][$this->attributeForMessage]; |
|
| 75 | - $messageToHandler[$message] = $id; |
|
| 73 | + foreach ($container->findTaggedServiceIds($this->tagForHandler) as $id => $hTags) { |
|
| 74 | + $message = $hTags[0][$this->attributeForMessage]; |
|
| 75 | + $messageToHandler[$message] = $id; |
|
| 76 | 76 | |
| 77 | - foreach ($container->findDefinition($id)->getTag($this->tagForPermission) as $pTags) { |
|
| 78 | - $permissionsByMessage[$message][] = $pTags[$this->attributeForPermission]; |
|
| 79 | - } |
|
| 80 | - } |
|
| 77 | + foreach ($container->findDefinition($id)->getTag($this->tagForPermission) as $pTags) { |
|
| 78 | + $permissionsByMessage[$message][] = $pTags[$this->attributeForPermission]; |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - $container->setParameter($this->parameterForMessageToHandler, $messageToHandler); |
|
| 83 | - $container->setParameter($this->parameterForPermissionToVoter, $permissionsByMessage); |
|
| 84 | - } |
|
| 82 | + $container->setParameter($this->parameterForMessageToHandler, $messageToHandler); |
|
| 83 | + $container->setParameter($this->parameterForPermissionToVoter, $permissionsByMessage); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - public function processVoters(ContainerBuilder $container): void |
|
| 87 | - { |
|
| 88 | - $permissionToVoter = []; |
|
| 86 | + public function processVoters(ContainerBuilder $container): void |
|
| 87 | + { |
|
| 88 | + $permissionToVoter = []; |
|
| 89 | 89 | |
| 90 | - foreach ($container->findTaggedServiceIds($this->tagForVoter) as $id => $tags) { |
|
| 91 | - $permissionToVoter[$tags[0][$this->attributeForPermission]] = $id; |
|
| 92 | - } |
|
| 90 | + foreach ($container->findTaggedServiceIds($this->tagForVoter) as $id => $tags) { |
|
| 91 | + $permissionToVoter[$tags[0][$this->attributeForPermission]] = $id; |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - $container->setParameter($this->parameterForVoter, $permissionToVoter); |
|
| 95 | - } |
|
| 94 | + $container->setParameter($this->parameterForVoter, $permissionToVoter); |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -15,14 +15,14 @@ |
||
| 15 | 15 | |
| 16 | 16 | class PermissionNotGranted extends \Exception implements Exception |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @param Context $context |
|
| 20 | - */ |
|
| 21 | - public function __construct( |
|
| 22 | - public object $dispatched_message, |
|
| 23 | - public Context $context, |
|
| 24 | - ?Throwable $previous = null |
|
| 25 | - ) { |
|
| 26 | - parent::__construct("Permission not granted for message: " . $dispatched_message::class, 0, $previous); |
|
| 27 | - } |
|
| 18 | + /** |
|
| 19 | + * @param Context $context |
|
| 20 | + */ |
|
| 21 | + public function __construct( |
|
| 22 | + public object $dispatched_message, |
|
| 23 | + public Context $context, |
|
| 24 | + ?Throwable $previous = null |
|
| 25 | + ) { |
|
| 26 | + parent::__construct("Permission not granted for message: " . $dispatched_message::class, 0, $previous); |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -23,6 +23,6 @@ |
||
| 23 | 23 | public Context $context, |
| 24 | 24 | ?Throwable $previous = null |
| 25 | 25 | ) { |
| 26 | - parent::__construct("Permission not granted for message: " . $dispatched_message::class, 0, $previous); |
|
| 26 | + parent::__construct("Permission not granted for message: ".$dispatched_message::class, 0, $previous); |
|
| 27 | 27 | } |
| 28 | 28 | } |
@@ -18,50 +18,50 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | final class Context |
| 20 | 20 | { |
| 21 | - /** |
|
| 22 | - * @var object[] |
|
| 23 | - */ |
|
| 24 | - private array $objects = []; |
|
| 21 | + /** |
|
| 22 | + * @var object[] |
|
| 23 | + */ |
|
| 24 | + private array $objects = []; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @param iterable<object> $objects |
|
| 28 | - */ |
|
| 29 | - public function __construct(iterable $objects = []) |
|
| 30 | - { |
|
| 31 | - foreach ($objects as $object) { |
|
| 32 | - $this->add($object); |
|
| 33 | - } |
|
| 34 | - } |
|
| 26 | + /** |
|
| 27 | + * @param iterable<object> $objects |
|
| 28 | + */ |
|
| 29 | + public function __construct(iterable $objects = []) |
|
| 30 | + { |
|
| 31 | + foreach ($objects as $object) { |
|
| 32 | + $this->add($object); |
|
| 33 | + } |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Adds an object to the context. |
|
| 38 | - */ |
|
| 39 | - public function add(object $object): self |
|
| 40 | - { |
|
| 41 | - array_unshift($this->objects, $object); |
|
| 36 | + /** |
|
| 37 | + * Adds an object to the context. |
|
| 38 | + */ |
|
| 39 | + public function add(object $object): self |
|
| 40 | + { |
|
| 41 | + array_unshift($this->objects, $object); |
|
| 42 | 42 | |
| 43 | - return $this; |
|
| 44 | - } |
|
| 43 | + return $this; |
|
| 44 | + } |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Returns the object matching the specified class. |
|
| 48 | - * |
|
| 49 | - * @template T of object |
|
| 50 | - * |
|
| 51 | - * @param class-string<T> $class |
|
| 52 | - * |
|
| 53 | - * @return T |
|
| 54 | - * |
|
| 55 | - * @throws NotInContext |
|
| 56 | - */ |
|
| 57 | - public function get(string $class): object |
|
| 58 | - { |
|
| 59 | - foreach ($this->objects as $object) { |
|
| 60 | - if ($object instanceof $class) { |
|
| 61 | - return $object; |
|
| 62 | - } |
|
| 63 | - } |
|
| 46 | + /** |
|
| 47 | + * Returns the object matching the specified class. |
|
| 48 | + * |
|
| 49 | + * @template T of object |
|
| 50 | + * |
|
| 51 | + * @param class-string<T> $class |
|
| 52 | + * |
|
| 53 | + * @return T |
|
| 54 | + * |
|
| 55 | + * @throws NotInContext |
|
| 56 | + */ |
|
| 57 | + public function get(string $class): object |
|
| 58 | + { |
|
| 59 | + foreach ($this->objects as $object) { |
|
| 60 | + if ($object instanceof $class) { |
|
| 61 | + return $object; |
|
| 62 | + } |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - throw new NotInContext($class); |
|
| 66 | - } |
|
| 65 | + throw new NotInContext($class); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -22,25 +22,25 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | final class VoterProviderWithContainer implements VoterProvider |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @param array<string, string> $permissionToService |
|
| 27 | - * Where _key_ is a permission and _value_ a service. |
|
| 28 | - */ |
|
| 29 | - public function __construct( |
|
| 30 | - private ContainerInterface $container, |
|
| 31 | - private array $permissionToService, |
|
| 32 | - ) { |
|
| 33 | - } |
|
| 25 | + /** |
|
| 26 | + * @param array<string, string> $permissionToService |
|
| 27 | + * Where _key_ is a permission and _value_ a service. |
|
| 28 | + */ |
|
| 29 | + public function __construct( |
|
| 30 | + private ContainerInterface $container, |
|
| 31 | + private array $permissionToService, |
|
| 32 | + ) { |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - public function getVoterForPermission(string $permission): ?Voter |
|
| 36 | - { |
|
| 37 | - $id = $this->permissionToService[$permission] |
|
| 38 | - ?? throw new VoterNotFound($permission); |
|
| 35 | + public function getVoterForPermission(string $permission): ?Voter |
|
| 36 | + { |
|
| 37 | + $id = $this->permissionToService[$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 | } |
@@ -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 | } |