@@ -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 | } |
@@ -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 | } |
@@ -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,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 | } |
@@ -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 | } |