From f849e7d20df6435b84268e9c9687b81b7a4dffd8 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Sun, 4 Aug 2024 05:30:33 -0400 Subject: [PATCH] improving code design with readonly models --- src/Envelope/Envelope.php | 6 +++--- src/Envelope/Message.php | 7 +++++++ src/Envelope/Stamp/HandledStamp.php | 2 +- src/Envelope/Stamp/Stamp.php | 7 +++++++ src/Envelope/Stamp/Stamps.php | 18 +++++++++--------- src/LazyMessageMessageBus.php | 4 +++- src/MessageBus.php | 4 +++- src/NativeLazyMessageBus.php | 3 ++- src/NativeMessageBus.php | 3 ++- tests/Fixtures/MyMessage.php | 4 +++- 10 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 src/Envelope/Message.php create mode 100644 src/Envelope/Stamp/Stamp.php diff --git a/src/Envelope/Envelope.php b/src/Envelope/Envelope.php index 0e2fbff..d27226a 100644 --- a/src/Envelope/Envelope.php +++ b/src/Envelope/Envelope.php @@ -7,10 +7,10 @@ final readonly class Envelope { - public object $message; + public Message $message; public Stamps $stamps; - public static function wrap(object $message, array $stamps = []): self + public static function wrap(Message $message, array $stamps = []): self { return new self($message, $stamps); } @@ -28,7 +28,7 @@ public function unwrap(): mixed }; } - private function __construct(object $message, array $stamps) + private function __construct(Message $message, array $stamps) { $this->message = $message; $this->stamps = new Stamps($stamps); diff --git a/src/Envelope/Message.php b/src/Envelope/Message.php new file mode 100644 index 0000000..7947b37 --- /dev/null +++ b/src/Envelope/Message.php @@ -0,0 +1,7 @@ +> $collection + * @param array> $collection */ private array $collection = []; /** - * @param array $stamps + * @param array $stamps */ public function __construct(array $stamps = []) { @@ -19,7 +19,7 @@ public function __construct(array $stamps = []) } } - public function add(object $stamp): self + public function add(Stamp $stamp): self { $this->collection[$stamp::class][] = $stamp; @@ -27,25 +27,25 @@ public function add(object $stamp): self } /** - * @template T of object + * @template T of Stamp * * @param class-string $class * * @return T|null */ - public function first(string $class): ?object + public function first(string $class): ?Stamp { return $this->collection[$class][0] ?? null; } /** - * @template T of object + * @template T of Stamp * * @param class-string $class * * @return T|null */ - public function last(string $class): ?object + public function last(string $class): ?Stamp { if ([] === $stamps = $this->collection[$class] ?? []) { return null; @@ -55,7 +55,7 @@ public function last(string $class): ?object } /** - * @template T of object + * @template T of Stamp * * @param class-string $class * @param \Closure(T): bool $fn @@ -69,7 +69,7 @@ public function filter(string $class, \Closure $fn): self } /** - * @template T of object + * @template T of Stamp * * @param class-string $class * @param \Closure(T): mixed $fn diff --git a/src/LazyMessageMessageBus.php b/src/LazyMessageMessageBus.php index 816f047..9a8b049 100644 --- a/src/LazyMessageMessageBus.php +++ b/src/LazyMessageMessageBus.php @@ -2,11 +2,13 @@ namespace OpenSolid\Bus; +use OpenSolid\Bus\Envelope\Message; + /** * A bus responsible for dispatching messages lazily to their handlers. * The messages are stored in an internal queue and dispatched when the bus is flushed. */ interface LazyMessageMessageBus extends MessageBus, FlushableMessageBus { - public function dispatch(object $message): null; + public function dispatch(Message $message): null; } diff --git a/src/MessageBus.php b/src/MessageBus.php index 11edc33..49ec7d9 100644 --- a/src/MessageBus.php +++ b/src/MessageBus.php @@ -2,11 +2,13 @@ namespace OpenSolid\Bus; +use OpenSolid\Bus\Envelope\Message; + /** * A bus responsible for dispatching messages to their handlers * and returning a result. */ interface MessageBus { - public function dispatch(object $message): mixed; + public function dispatch(Message $message): mixed; } diff --git a/src/NativeLazyMessageBus.php b/src/NativeLazyMessageBus.php index 9055a71..2e455b5 100644 --- a/src/NativeLazyMessageBus.php +++ b/src/NativeLazyMessageBus.php @@ -2,6 +2,7 @@ namespace OpenSolid\Bus; +use OpenSolid\Bus\Envelope\Message; use Symfony\Contracts\Service\ResetInterface; final class NativeLazyMessageBus implements LazyMessageMessageBus, ResetInterface @@ -13,7 +14,7 @@ public function __construct( ) { } - public function dispatch(object $message): null + public function dispatch(Message $message): null { $this->messages[] = $message; diff --git a/src/NativeMessageBus.php b/src/NativeMessageBus.php index 3f3a504..49c68d9 100644 --- a/src/NativeMessageBus.php +++ b/src/NativeMessageBus.php @@ -3,6 +3,7 @@ namespace OpenSolid\Bus; use OpenSolid\Bus\Envelope\Envelope; +use OpenSolid\Bus\Envelope\Message; use OpenSolid\Bus\Middleware\Middleware; use OpenSolid\Bus\Middleware\MiddlewareStack; @@ -18,7 +19,7 @@ public function __construct(iterable $middlewares) $this->middlewares = new MiddlewareStack($middlewares); } - public function dispatch(object $message): mixed + public function dispatch(Message $message): mixed { $envelope = Envelope::wrap($message); diff --git a/tests/Fixtures/MyMessage.php b/tests/Fixtures/MyMessage.php index 23b9c07..61641e7 100644 --- a/tests/Fixtures/MyMessage.php +++ b/tests/Fixtures/MyMessage.php @@ -2,6 +2,8 @@ namespace OpenSolid\Tests\Bus\Fixtures; -class MyMessage +use OpenSolid\Bus\Envelope\Message; + +readonly class MyMessage extends Message { }