Skip to content

Commit

Permalink
improving code design with readonly models
Browse files Browse the repository at this point in the history
  • Loading branch information
yceruto committed Aug 4, 2024
1 parent 19ccde3 commit f849e7d
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/Envelope/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/Envelope/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace OpenSolid\Bus\Envelope;

readonly abstract class Message
{
}
2 changes: 1 addition & 1 deletion src/Envelope/Stamp/HandledStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OpenSolid\Bus\Envelope\Stamp;

final readonly class HandledStamp
final readonly class HandledStamp extends Stamp
{
public function __construct(
public mixed $result,
Expand Down
7 changes: 7 additions & 0 deletions src/Envelope/Stamp/Stamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace OpenSolid\Bus\Envelope\Stamp;

readonly abstract class Stamp
{
}
18 changes: 9 additions & 9 deletions src/Envelope/Stamp/Stamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
final class Stamps implements \Countable
{
/**
* @param array<class-string, array<object>> $collection
* @param array<class-string, array<Stamp>> $collection
*/
private array $collection = [];

/**
* @param array<object> $stamps
* @param array<Stamp> $stamps
*/
public function __construct(array $stamps = [])
{
Expand All @@ -19,33 +19,33 @@ public function __construct(array $stamps = [])
}
}

public function add(object $stamp): self
public function add(Stamp $stamp): self
{
$this->collection[$stamp::class][] = $stamp;

return $this;
}

/**
* @template T of object
* @template T of Stamp
*
* @param class-string<T> $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<T> $class
*
* @return T|null
*/
public function last(string $class): ?object
public function last(string $class): ?Stamp
{
if ([] === $stamps = $this->collection[$class] ?? []) {
return null;
Expand All @@ -55,7 +55,7 @@ public function last(string $class): ?object
}

/**
* @template T of object
* @template T of Stamp
*
* @param class-string<T> $class
* @param \Closure(T): bool $fn
Expand All @@ -69,7 +69,7 @@ public function filter(string $class, \Closure $fn): self
}

/**
* @template T of object
* @template T of Stamp
*
* @param class-string<T> $class
* @param \Closure(T): mixed $fn
Expand Down
4 changes: 3 additions & 1 deletion src/LazyMessageMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 3 additions & 1 deletion src/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/NativeLazyMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OpenSolid\Bus;

use OpenSolid\Bus\Envelope\Message;
use Symfony\Contracts\Service\ResetInterface;

final class NativeLazyMessageBus implements LazyMessageMessageBus, ResetInterface
Expand All @@ -13,7 +14,7 @@ public function __construct(
) {
}

public function dispatch(object $message): null
public function dispatch(Message $message): null
{
$this->messages[] = $message;

Expand Down
3 changes: 2 additions & 1 deletion src/NativeMessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion tests/Fixtures/MyMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OpenSolid\Tests\Bus\Fixtures;

class MyMessage
use OpenSolid\Bus\Envelope\Message;

readonly class MyMessage extends Message
{
}

0 comments on commit f849e7d

Please sign in to comment.