From b54ec9d7d9eb1142787cf7d38ebb836d1c95d3ab Mon Sep 17 00:00:00 2001 From: Joey Smith Date: Thu, 26 Feb 2026 02:48:04 -0600 Subject: [PATCH] Signed-off-by: Joey Smith --- src/{MutableEvent.php => Event.php} | 30 ++++++++---- src/EventDispatcherAwareTrait.php | 3 ++ src/EventInterface.php | 15 +++++- src/EventPropagationInterface.php | 12 +++++ src/EventPropagationTrait.php | 24 ++++++++++ src/ImmutableEvent.php | 71 ----------------------------- src/ImmutableEventInterface.php | 27 ----------- src/MutableEventInterface.php | 27 ----------- src/PostHandleEvent.php | 2 +- src/PreHandleEvent.php | 2 +- 10 files changed, 75 insertions(+), 138 deletions(-) rename src/{MutableEvent.php => Event.php} (72%) create mode 100644 src/EventPropagationInterface.php create mode 100644 src/EventPropagationTrait.php delete mode 100644 src/ImmutableEvent.php delete mode 100644 src/ImmutableEventInterface.php delete mode 100644 src/MutableEventInterface.php diff --git a/src/MutableEvent.php b/src/Event.php similarity index 72% rename from src/MutableEvent.php rename to src/Event.php index 82ebbba..4f20708 100644 --- a/src/MutableEvent.php +++ b/src/Event.php @@ -16,17 +16,24 @@ use Override; -class MutableEvent implements MutableEventInterface +class Event implements EventInterface, EventPropagationInterface { + use EventPropagationTrait; + /** * @param array|null $params */ public function __construct( private ?string $name = null, private ?object $target = null, - private ?array $params = null, + private array $params = [], ) {} + #[Override] + public function setName(string $name): void + { + $this->name = $name; + } #[Override] public function getName(): string { @@ -34,11 +41,10 @@ public function getName(): string } #[Override] - public function setName(string $name): void + public function setTarget(object $target): void { - $this->name = $name; + $this->target = $target; } - #[Override] public function getTarget(): ?object { @@ -46,15 +52,15 @@ public function getTarget(): ?object } #[Override] - public function setTarget(object $target): void + public function setParam(string $name, mixed $value): void { - $this->target = $target; + $this->params[$name] = $value; } #[Override] - public function getParams(): array + public function getParam(string $name, mixed $default = null): mixed { - return $this->params ?? []; + return $this->params[$name] ?? $default; } #[Override] @@ -62,4 +68,10 @@ public function setParams(array $params): void { $this->params = $params; } + + #[Override] + public function getParams(): array + { + return $this->params; + } } diff --git a/src/EventDispatcherAwareTrait.php b/src/EventDispatcherAwareTrait.php index 340a583..dd0af84 100644 --- a/src/EventDispatcherAwareTrait.php +++ b/src/EventDispatcherAwareTrait.php @@ -14,6 +14,7 @@ namespace Webware\CommandBus\Event; +use Override; use Psr\EventDispatcher\EventDispatcherInterface; /** @@ -23,11 +24,13 @@ trait EventDispatcherAwareTrait { protected EventDispatcherInterface $eventDispatcher; + #[Override] public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void { $this->eventDispatcher = $eventDispatcher; } + #[Override] public function getEventDispatcher(): EventDispatcherInterface { return $this->eventDispatcher; diff --git a/src/EventInterface.php b/src/EventInterface.php index a31fdbe..04ce112 100644 --- a/src/EventInterface.php +++ b/src/EventInterface.php @@ -16,12 +16,23 @@ interface EventInterface { + public function setName(string $name): void; + public function getName(): string; public function getTarget(): ?object; + public function setTarget(object $target): void; + + public function setParam(string $name, mixed $value): void; + + public function getParam(string $name, mixed $default = null): mixed; + + public function setParams(array $params): void; + /** - * @return array|null + * @return array */ - public function getParams(): ?array; + public function getParams(): array; + } diff --git a/src/EventPropagationInterface.php b/src/EventPropagationInterface.php new file mode 100644 index 0000000..daafe30 --- /dev/null +++ b/src/EventPropagationInterface.php @@ -0,0 +1,12 @@ +propagationStopped = $flag; + } + + #[Override] + public function isPropagationStopped(): bool + { + return $this->propagationStopped; + } +} diff --git a/src/ImmutableEvent.php b/src/ImmutableEvent.php deleted file mode 100644 index 8992c81..0000000 --- a/src/ImmutableEvent.php +++ /dev/null @@ -1,71 +0,0 @@ - - * and contributors. - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Webware\CommandBus\Event; - -use Override; - -readonly class ImmutableEvent implements ImmutableEventInterface -{ - /** - * @param array|null $params - */ - public function __construct( - private ?string $name = self::class, - private ?object $target = null, - private ?array $params = null, - ) {} - - #[Override] - public function getName(): string - { - return $this->name ?? self::class; - } - - #[Override] - public function withName(string $name): self - { - return new self($name, $this->target, $this->params); - } - - #[Override] - public function getTarget(): ?object - { - return $this->target; - } - - #[Override] - public function withTarget(object $target): self - { - return new self($this->name, $target, $this->params); - } - - /** - * @return array - */ - #[Override] - public function getParams(): array - { - return $this->params ?? []; - } - - /** - * @param array $params - */ - #[Override] - public function withParams(array $params): self - { - return new self($this->name, $this->target, $params); - } -} diff --git a/src/ImmutableEventInterface.php b/src/ImmutableEventInterface.php deleted file mode 100644 index aafc280..0000000 --- a/src/ImmutableEventInterface.php +++ /dev/null @@ -1,27 +0,0 @@ - - * and contributors. - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Webware\CommandBus\Event; - -interface ImmutableEventInterface extends EventInterface -{ - public function withName(string $name): self; - - public function withTarget(object $target): self; - - /** - * @param array $params - */ - public function withParams(array $params): self; -} diff --git a/src/MutableEventInterface.php b/src/MutableEventInterface.php deleted file mode 100644 index 26072d8..0000000 --- a/src/MutableEventInterface.php +++ /dev/null @@ -1,27 +0,0 @@ - - * and contributors. - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Webware\CommandBus\Event; - -interface MutableEventInterface extends EventInterface -{ - public function setName(string $name): void; - - public function setTarget(object $target): void; - - /** - * @param array $params - */ - public function setParams(array $params): void; -} diff --git a/src/PostHandleEvent.php b/src/PostHandleEvent.php index 0bbfffc..55ac5e5 100644 --- a/src/PostHandleEvent.php +++ b/src/PostHandleEvent.php @@ -16,7 +16,7 @@ use Webware\CommandBus\CommandInterface; -final class PostHandleEvent extends MutableEvent +final class PostHandleEvent extends Event { public function __construct( private CommandInterface $command, diff --git a/src/PreHandleEvent.php b/src/PreHandleEvent.php index d7118ff..a08370a 100644 --- a/src/PreHandleEvent.php +++ b/src/PreHandleEvent.php @@ -16,7 +16,7 @@ use Webware\CommandBus\CommandInterface; -final class PreHandleEvent extends MutableEvent +final class PreHandleEvent extends Event { public function __construct( private readonly CommandInterface $command,